coupon.js
3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
let $ = require('yoho-jquery'),
tip = require('js/plugin/tip');
require('js/common');
let shopCoupon = {};
shopCoupon = {
common: {
appVersion: window.queryString.app_version || window.queryString.appVersion,
shopId: parseInt($('.shop-id').val() || $('.shopId').val() || $('#shopId').val(), 10) || null,
couponTemplate: require('hbs/product/shop/coupon.hbs')
},
init: function() {
// 如果为空,则不请求
if (!this.common.shopId) {
return true;
}
setTimeout(function() {
this.loginCoupon();
this.getShopCouponsList();
}.bind(shopCoupon), 400);
},
getShopCouponsList: function() {
let that = this,
_url = location.protocol + '//m.yohobuy.com/product/index/getShopCouponsList';
if ($('.coupon-group').length <= 0) {
return false;
}
$.ajax({
method: 'GET',
url: _url,
data: {
shopId: that.common.shopId
},
xhrFields: {
withCredentials: true
},
success: function(result) {
$('.coupon-group').html(that.common.couponTemplate(result));
that.userCoupon();
}
});
},
userCoupon: function() {
let $self = this;
$('.coupon-group .receive-btn').closest('.coupon-center').on('click', function() {
let that = this,
code = $(this).data('id') || '';
if (that.isCouponClick === false) {
return false;
}
that.isCouponClick = false;
$.ajax({
method: 'POST',
url: '/product/index/userCoupon',
data: {
appVersion: $self.common.appVersion,
couponID: code,
uid: $self.common.uid
},
xhrFields: {
withCredentials: true
},
success: function(data) {
that.isCouponClick = true;
if (data.code === 200) {
tip.show('领取成功');
$(that).find('.receive-btn').html('已领取');
return true;
} else if (data.code === 4401) {
$('#shop-login').remove();
$('body').append('<a href=\'' + data.url + '\'><span id="shop-login"><span></a>');
$('#shop-login').click();
// 未登录状态下
window.setCookie('shopBrandCoupon', code);
} else if (data.code === 401) {
tip.show('已领取过');
} else {
tip.show(data.message);
}
},
error: function() {
tip.show('网络断开连接了~');
that.isCouponClick = true;
}
});
});
},
// 登录状态下,领取优惠券
loginCoupon: function() {
let $self = this,
code = window.cookie('shopBrandCoupon') || false;
window.setCookie('shopBrandCoupon', null);
if (!code) {
return false;
}
$.ajax({
method: 'POST',
url: '/product/index/userCoupon',
data: {
couponID: code,
uid: $self.common.uid
},
xhrFields: {
withCredentials: true
}
});
}
};
$(function() {
shopCoupon.init();
});