select-coupons.js
6.51 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import $ from 'yoho-jquery';
import qs from 'yoho-qs';
import Page from 'js/yoho-page';
import tip from 'js/plugin/tip';
import cookie from 'yoho-cookie';
import cookieOption from './cookie-option';
class SelectCouponController extends Page {
constructor(params) {
super();
this.linkUrl = document.referrer;
this.orderInfo = params.orderInfo;
this.couponListUrl = params.couponListUrl;
this.useCouponCodeUrl = params.useCouponCodeUrl;
this.selectCouponCodes = [];
this.isBuyNowPage = params.isBuyNowPage || false;
this.closeCouponRuleTipbyUser = cookie.get('close_coupon_rule_tip_by_user') || 'N';
if (this.orderInfo('coupon_code')) {
this.selectCouponCodes = this.orderInfo('coupon_code').split(',');
}
this.page = $('.select-coupons-page');
this.renderPage();
}
/**
* 渲染页面
*/
renderPage() {
let couponCodeStr = this.selectCouponCodes.length > 0 ? this.selectCouponCodes.join(',') : '';
let postData = {
delivery_way: this.orderInfo('deliveryId'),
coupon_code: couponCodeStr
};
if (this.isBuyNowPage) {
postData.product_sku = qs.product_sku;
postData.buy_number = qs.buy_number;
}
this.ajax({
type: 'POST',
url: this.couponListUrl,
data: postData
}).then(result => {
this.orderInfo('coupon_code', couponCodeStr);
let resultHtml = $(result);
this.bindEvents(resultHtml);
this.page.html(resultHtml);
});
}
/**
* 事件绑定处理
*/
bindEvents(resultHtml) {
this.view = {
filterBtn: resultHtml.find('.filter-btn'),
coupon: resultHtml.find('.coupon'),
checkbox: resultHtml.find('.checkbox'),
tipBox: resultHtml.find('.tip-box'),
tipClose: resultHtml.find('.tip-close'),
usableCouponList: resultHtml.find('.usable'),
unusableCouponList: resultHtml.find('.unusable'),
useNowBtn: resultHtml.find('#useNowBtn'),
exchangeCouponBtn: resultHtml.find('#exchangeCouponBtn'),
couponCodeInput: resultHtml.find('input[name=couponCodeInput]'),
exchangeBox: resultHtml.find('.exchange-box'),
useNowBox: resultHtml.find('#useNowBox')
};
this.view.filterBtn.on('click', this.tabChange.bind(this));
this.view.coupon.on('click', '.checkbox', this.check.bind(this));
this.view.tipClose.on('click', this.closeTip.bind(this));
this.view.useNowBtn.on('click', this.useCoupon.bind(this));
this.view.exchangeCouponBtn.on('click', this.exchangeCoupon.bind(this));
this.view.couponCodeInput.on('input', this.changeExchangeBtnStatus.bind(this));
if (this.closeCouponRuleTipbyUser === 'Y') {
this.view.tipBox.addClass('hide');
}
}
/**
* 改变兑换按钮状态
*/
changeExchangeBtnStatus() {
if (this.view.couponCodeInput.val().length > 0) {
this.view.exchangeCouponBtn.addClass('active');
} else {
this.view.exchangeCouponBtn.removeClass('active');
}
}
/**
* 使用优惠券码
*/
exchangeCoupon() {
let couponCode = this.view.couponCodeInput.val();
if (!couponCode) {
tip.show('请输入优惠券码');
return false;
}
this.ajax({
method: 'POST',
url: this.useCouponCodeUrl,
data: {
couponCode: couponCode
}
}).then(res => {
if (res.message) {
tip.show(res.message);
}
if (res.code === 200) {
this.selectCouponCodes.push(couponCode);
setTimeout(() => {
location.reload();
}, 500);
}
});
}
/**
* 使用优惠券
*/
useCoupon() {
if (this.linkUrl) {
window.location.href = this.linkUrl;
} else {
history.go(-1);
}
}
/**
* tab 切换
*/
tabChange(event) {
let itemClicked = $(event.currentTarget);
// Tab 按钮状态
if (!itemClicked.hasClass('active')) {
this.view.filterBtn.removeClass('active');
itemClicked.addClass('active');
}
// 优惠券列表切换,兑换模块、立即使用模块显示控制
if (itemClicked.hasClass('valid')) {
this.view.unusableCouponList.addClass('hide');
this.view.usableCouponList.removeClass('hide');
this.view.exchangeBox.removeClass('hide');
if (itemClicked.data('num') === 0) {
this.view.useNowBox.addClass('hide');
} else {
this.view.useNowBox.removeClass('hide');
}
} else {
this.view.usableCouponList.addClass('hide');
this.view.unusableCouponList.removeClass('hide');
this.view.exchangeBox.addClass('hide');
this.view.useNowBox.addClass('hide');
}
// Tip 控制
if (this.closeCouponRuleTipbyUser === 'Y' || itemClicked.hasClass('invalid')) {
this.view.tipBox.addClass('hide');
} else {
if (itemClicked.data('num') === 0) {
this.view.tipBox.addClass('hide');
} else {
this.view.tipBox.removeClass('hide');
}
}
}
/**
* 优惠券勾选处理
*/
check(e) {
let theCouponCheck = $(e.currentTarget);
let theCoupon = $(e.delegateTarget);
let theCouponData = theCoupon.data();
if (!this.orderInfo('user_check_coupon')) {
this.orderInfo('user_check_coupon', 'Y');
}
if (theCouponCheck.hasClass('icon-cb-radio')) {
this.selectCouponCodes.splice($.inArray(theCouponData.code, this.selectCouponCodes), 1);
theCouponCheck.removeClass('icon-cb-radio').addClass('icon-radio');
} else {
this.selectCouponCodes.push(theCouponData.code);
theCouponCheck.removeClass('icon-radio').addClass('icon-cb-radio');
}
this.renderPage();
}
/**
* 关闭提示框
*/
closeTip() {
this.closeCouponRuleTipbyUser = 'Y';
cookie.set('close_coupon_rule_tip_by_user', 'Y', cookieOption);
this.view.tipBox.hide();
}
}
export default SelectCouponController;