controller.js
8.37 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import $ from 'yoho-jquery';
import tip from 'js/plugin/tip';
import Page from 'js/yoho-page';
class ConponController extends Page {
constructor() {
super();
this.couponType = 'notuse';
this.couponFilter = 0;
this.page = 1;
this.view = {
page: $('.coupon-new-page'),
filterBtn: $('.filter-btn'),
filterItem: $('.filter-item'),
showFilterBtn: $('.show-filter-btn'),
couponList: $('#couponList'),
couponSection: $('.coupon-section'),
noConponNow: $('.no-conpon-now'),
exchangeCouponBtn: $('#exchangeCouponBtn'),
couponCodeInput: $('input[name=couponCodeInput]'),
usedTip: $('.used-tip'),
exchangeBox: $('.exchange-box')
};
if ($('#yoho-header').hasClass('hide')) {
this.view.page.removeClass('cpage-padding284');
}
this.view.filterBtn.on('click', this.tabChange.bind(this));
this.view.showFilterBtn.on('click', this.showFilter.bind(this));
this.view.filterItem.on('click', 'button', this.filterCoupons.bind(this));
this.view.couponSection.on('click', '.show-intro-btn', this.showIntro.bind(this));
this.view.exchangeCouponBtn.on('click', this.exchangeCoupon.bind(this));
this.view.couponCodeInput.on('input', this.changeExchangeBtnStatus.bind(this));
this.loading = false;
this.loadEnd = false;
this.beforeScroll = $(window).scrollTop(); // 滚动前位置记录
this.afterScroll; // 滚动后的位置
window.onscroll = () => {
if (this.loading || this.loadEnd) {
return;
}
setTimeout(() => {
this.afterScroll = $(window).scrollTop();
if (this.afterScroll - this.beforeScroll > 0) {
window.requestAnimationFrame(() => {
this.scrollHandler();
});
}
this.beforeScroll = this.afterScroll;
}, 100);
};
}
/**
* 改变兑换按钮状态
*/
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: '/cart/index/new/useCouponCode',
data: {
couponCode: couponCode
}
}).then(res => {
if (res.message) {
tip.show(res.message);
}
if (res.code === 200) {
setTimeout(() => {
location.reload();
}, 500);
}
});
}
/**
* 滚动处理
*/
scrollHandler() {
let conponListHeight = this.view.couponList.height();
if ($(window).scrollTop() > conponListHeight * 0.6) {
this.renderCoupons(true);
}
}
/**
* 筛选优惠券
*/
filterCoupons(evt) {
let currentTarget = $(evt.currentTarget);
let currentTargetData = currentTarget.data();
this.couponType = 'notuse';
this.page = 0;
this.couponFilter = currentTargetData.id;
this.view.filterItem.find('button').removeClass('active');
currentTarget.addClass('active');
this.view.filterItem.addClass('hide');
this.view.showFilterBtn.removeClass('icon-up').addClass('icon-down');
this.renderCoupons();
}
/**
* 获取优惠券
*/
renderCoupons(scroll) {
if (this.loading) {
return;
}
this.loading = true;
this.page++;
this.ajax({
type: 'POST',
url: '/home/couponsList',
data: {
type: this.couponType,
filter: this.couponFilter,
page: this.page
},
}).then(result => {
this.loading = false;
let noResult = !result || !result.length;
if (scroll && noResult) {
this.loadEnd = true;
return;
}
if (noResult) {
this.view.noConponNow.removeClass('hide');
} else {
this.view.noConponNow.addClass('hide');
}
let couponValidHbsHtml = $(result);
couponValidHbsHtml.on('click', '.show-intro-btn', this.showIntro.bind(this));
if (scroll) {
this.view.couponList.append(couponValidHbsHtml);
} else {
this.view.couponList.html(couponValidHbsHtml);
}
});
}
/**
* tab 切换
*/
tabChange(event) {
this.page = 0;
this.couponFilter = 0;
this.loadEnd = false;
let itemClicked = $(event.currentTarget);
let itemCouponNum = itemClicked.data('num');
// 筛选参数更改
if (itemClicked.hasClass('no-used')) {
this.couponType = 'notuse';
this.view.usedTip.addClass('hide');
this.view.usedTip.find('span').addClass('hide');
} else if (itemClicked.hasClass('used')) {
this.couponType = 'use';
if (itemCouponNum !== 0) {
this.view.usedTip.removeClass('hide');
this.view.usedTip.find('.used').removeClass('hide');
this.view.usedTip.find('.invalid').addClass('hide');
} else {
this.view.usedTip.addClass('hide');
}
} else if (itemClicked.hasClass('invalid')) {
this.couponType = 'overtime';
if (itemCouponNum !== 0) {
this.view.usedTip.removeClass('hide');
this.view.usedTip.find('.invalid').removeClass('hide');
this.view.usedTip.find('.used').addClass('hide');
} else {
this.view.usedTip.addClass('hide');
}
} else {
tip.show('未知券类型');
}
// 筛选框控制按钮状态、优惠券码兑换输入框状态管理
if (itemClicked.hasClass('no-used')) {
this.view.page.addClass('cpage-padding284').removeClass('cpage-padding194');
this.view.showFilterBtn.addClass('active');
this.view.exchangeBox.removeClass('hide');
} else {
this.view.page.addClass('cpage-padding194').removeClass('cpage-padding284');
this.view.showFilterBtn.removeClass('icon-up').addClass('icon-down');
this.view.showFilterBtn.removeClass('active');
this.view.exchangeBox.addClass('hide');
}
// 筛选项面板和筛选项状态重置
this.view.filterItem.addClass('hide');
this.view.filterItem.find('button').removeClass('active');
if (!itemClicked.hasClass('active')) {
this.view.filterBtn.removeClass('active');
itemClicked.addClass('active');
this.renderCoupons();
}
}
/**
* 展示筛选器
*/
showFilter(e) {
let currentTarget = $(e.currentTarget);
if (!currentTarget.hasClass('active')) {
return;
}
if (this.view.filterItem.hasClass('hide')) {
this.view.filterItem.removeClass('hide');
this.view.showFilterBtn.removeClass('icon-down').addClass('icon-up');
} else {
this.view.filterItem.addClass('hide');
this.view.showFilterBtn.removeClass('icon-up').addClass('icon-down');
}
}
/**
* 展示优惠券介绍
*/
showIntro(e) {
let delegateTarget = $(e.delegateTarget);
let couponIntro = delegateTarget.find('.coupon-intro');
let showIntroIcon = delegateTarget.find('.show-intro-btn');
if (couponIntro.hasClass('hide')) {
couponIntro.removeClass('hide');
showIntroIcon.eq(1).removeClass('icon-down').addClass('icon-up');
} else {
couponIntro.addClass('hide');
showIntroIcon.eq(1).removeClass('icon-up').addClass('icon-down');
}
}
}
export default ConponController;