me-gift.page.js
10.6 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
var $ = require('yoho-jquery'),
Hbs = require('yoho-handlebars'),
dialog = require('../common/dialog');
var meGift;
require('yoho-jquery-placeholder');
require('../common');
meGift = {
isFlag: false, // 防止用户频繁点击
// 验证邮箱模板
emailTpl: Hbs.compile($('#verify-email-tpl').html()),
// 绑定手机模板
mobileTpl: Hbs.compile($('#verify-mobile-tpl').html()),
// 激活礼品卡模板
giftTpl: Hbs.compile($('#activate-gift-tpl').html()),
// 消费明细模板
detailGiftTpl: Hbs.compile($('#detail-gift-tpl').html()),
// 添加礼品卡按钮
$addGift: $('.add-gift'),
// 是否绑定手机
isBinMobile: +$('.me-content').data('is-bin-mobile'),
// 用户邮箱
userEmail: $('.me-content').data('email'),
init: function() {
var that = this;
// 添加礼品卡
this.$addGift.click(function() {
if (that.isBinMobile) {
that.activateGift();
} else {
that.getMobileCode();
}
});
// 显示手机区号或类型下拉列表
$('body').on('click', '.mobile-area,.gift-filter', function() {
$(this).find('.ul-list').toggle();
});
// 选择手机区号
$('body').on('click', '.mobile-area-list', function(event) {
var $li = $(event.target).closest('li');
$('.mobile-area').find('em').text($li.data('cc'));
});
// 消费明细
$('.me-gift-table').on('click', '.info-list', function() {
new dialog.Dialog({
content: that.detailGiftTpl({}),
className: 'me-page me-gift-page me-gift-confirm'
}).show();
that.infoList('?' + $.param({
cardCode: $(this).closest('.me-gift-tr').data('card-code')
}));
});
// 消费明细-选择类型筛选
$('body').on('click', '.gift-filter ul', function(event) {
var $li = $(event.target).closest('li');
$('.gift-filter').find('em').text($li.text());
that.infoList('?' + $.param({
cardCode: $('.info-gift-header').data('card-code'),
type: $li.data('cc')
}));
});
// 消费明细翻页
$('body').on('click', '.detail-gift-content .pager a', function() {
if ($(this).hasClass('cur')) {
return false;
}
that.infoList($(this).attr('href'));
return false;
});
},
postAjax: function(url, data, verifyData) {
var deferred = $.Deferred(); // eslint-disable-line
var that = this;
var x;
if (that.isFlag) {
return deferred.resolve({
code: 401,
message: '数据请求中...'
});
}
if (verifyData) {
for (x in verifyData) {
if (!data[x]) {
that.meAlert(verifyData[x], false);
return deferred.resolve({
code: 401,
message: verifyData[x]
});
}
}
}
that.isFlag = true;
return $.ajax({
url: url,
type: 'post',
data: data
}).then(function(res) {
if (res.code !== 200) {
that.meAlert(res.message, false);
}
that.isFlag = false;
return res;
}, function() {
that.isFlag = false;
});
},
intTimer: function($dom) {
var intTime = 60;
var intval = setInterval(function() {
if (intTime-- <= 0) {
clearInterval(intval);
$dom.removeClass('int-timer').html('获取短信验证码');
return false;
}
$dom.html(intTime + 's');
}, 1000);
$dom.addClass('int-timer');
$dom.html(intTime + 's');
},
// 获取邮箱验证码
getEmailCode: function() {
var that = this;
this.postAjax('/home/megift/sendEmailCode', {
email: this.userEmail
}, {}).then(function(res) {
if (res.code === 200) {
that.intTimer($('.email-btn'));
}
});
},
// 效验邮箱验证码
verifyEmailCode: function() {
var that = this;
var dg = new dialog.Dialog({
closeIcon: false,
content: that.emailTpl({}),
className: 'me-gift-confirm',
btns: [{
id: 'confirm-email-btn',
name: '绑定手机号',
btnClass: ['alert-sure'],
cb: function() {
var emailCode = $('.email-code').val();
var verifyData = {code: '验证码不能为空'};
that.postAjax('/home/megift/verifyEmail', {
email: that.userEmail,
code: emailCode
}, verifyData).then(function(res) {
if (res.code === 200) {
dg.close();
that.getMobileCode();
}
});
}
}]
}).show();
setTimeout(function() {
$('[placeholder]', dg.$el).placeholder(); // ie8 兼容 placeholder
}, 10);
// 发送邮箱验证码
$('.me-gift-confirm').find('.email-btn').unbind('click').bind('click', function() {
if (!$(this).hasClass('int-timer')) {
that.getEmailCode();
}
});
},
// 获取手机验证码
getMobileCode: function() {
var that = this;
var dg = new dialog.Dialog({
closeIcon: false,
content: that.mobileTpl({}),
className: 'me-gift-confirm',
btns: [{
id: 'confirm-mobile-cancel',
name: '取消',
btnClass: ['confirm-cancel'],
cb: function() {
dg.close();
}
}, {
id: 'confirm-mobile-sure',
name: '激活',
btnClass: ['confirm-sure'],
cb: function() {
var verifyData = {
area: '请选择手机区号',
mobile: '手机号不能为空',
code: '手机验证码不能为空'
};
that.postAjax('/home/megift/changeMobile', {
area: ($('.mobile-area').find('em').text() || '').replace(/^\+/, ''),
mobile: $('input.mobile').val(),
code: $('input.mobile-code').val()
}, verifyData).then(function(res) {
if (res.code === 200) {
that.isBinMobile = 1;
dg.close();
that.activateGift();
}
});
}
}]
}).show();
setTimeout(function() {
$('[placeholder]', dg.$el).placeholder(); // ie8 兼容 placeholder
}, 10);
// 发送邮箱验证码
$('.me-gift-confirm').find('.mobile-btn').unbind('click').bind('click', function() {
if (!$(this).hasClass('int-timer')) {
that.smsBind();
}
});
},
// 发送手机验证码
smsBind: function() {
var that = this;
var area = ($('.mobile-area').find('em').text() || '').replace(/^\+/, '');
var mobile = $('input.mobile').val();
var verifyData = {
area: '请选择手机区号',
mobile: '手机号不能为空'
};
this.postAjax('/home/megift/smsBind', {
area: area,
mobile: mobile
}, verifyData).then(function(res) {
if (res.code === 200) {
that.intTimer($('.mobile-btn'));
}
});
},
// 激活礼品卡
activateGift: function() {
var that = this;
var dg = new dialog.Dialog({
closeIcon: false,
content: that.giftTpl({}),
className: 'me-gift-confirm',
btns: [{
id: 'confirm-gift-cancel',
name: '取消',
btnClass: ['confirm-cancel'],
cb: function() {
dg.close();
}
}, {
id: 'confirm-gift-sure',
name: '激活',
btnClass: ['confirm-sure'],
cb: function() {
var $confirm = $('.me-gift-confirm');
var verifyData = {
cardCode: '礼品卡卡号不能为空',
cardPwd: '礼品卡卡密不能为空'
};
that.postAjax('/home/meGift/activateGift', {
cardCode: $confirm.find('.card-code').val(),
cardPwd: $confirm.find('.card-pwd').val()
}, verifyData).then(function(res) {
if (res.code === 200) {
dg.close();
that.meAlert('<p>您的礼品卡激活成功</p>', false, function() {
location.reload();
});
}
});
}
}]
}).show();
setTimeout(function() {
$('[placeholder]', dg.$el).placeholder(); // ie8 兼容 placeholder
}, 10);
},
// 消费明细
infoList: function(url) {
url = url || '';
$.get('/home/megift/detail' + url).then(function(res) {
if (!res) {
return false;
}
$('.detail-gift-content').html(res);
});
},
// 我的弹框
meAlert: function(content, mask, callback) {
var dg;
var meAlertDialog = $('.me-gift-confirm');
meAlertDialog.addClass('hide');
dg = new dialog.Dialog({
closeIcon: false,
content: content,
className: 'me-gift-alert',
mask: !!mask,
btns: [{
name: '我知道了',
id: 'alert-gift-cancel',
btnClass: ['alert-sure'],
cb: function() {
dg.close();
meAlertDialog.removeClass('hide');
callback && callback();
}
}]
}).show();
}
};
$(function() {
meGift.init();
});