students.page.js
11.4 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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/**
* stu页面
* @author: da<hongwei.gao@yoho.cn>
* @date: 2016/08/8
*/
var $ = require('yoho-jquery'),
lazyLoad = require('yoho-jquery-lazyload'),
handlebars = require('yoho-handlebars');
var Dialog = require('../common/dialog').Dialog;
var $sortItem = $('.sort-item'),
$agreenShow = $('#agreen-show'),
$stuProv = $('#stu-province-show'),
$stuProvWrap = $stuProv.parent(),
$stuSchool = $('#stu-school-show'),
$stuSchoolWrap = $stuSchool.parent(),
$stuEdu = $('#stu-edu-show'),
$stuEduWrap = $stuEdu.parent(),
$stuYear = $('#stu-entrance-year-show'),
$stuYearWrap = $stuYear.parent(),
$identityForm = $('#identity-form');
// $stuYearUl = $stuYear.next();
var $rightDia = $('#rights-dia'),
$couponDia = $('#coupon-dia');
var provFn;
var alertConfig,
makeAlert;
// j面跳转对象
var redirect = {
// 去逛逛跳转链接
gunangSrc: null,
// 查看优惠券跳转链接
checkCouponSrc: null,
goToGuang: function() {
window.location.href = this.gunangSrc;
},
goToCheck: function() {
window.location.href = this.checkCouponSrc;
},
// 处理网络返回
403: function(url) {
window.location.href = url;
},
200: function(url) {
this.checkCouponSrc = url;
}
};
// handlebars模板
provFn = handlebars.compile($('#stu-select').html() || '');
require('../plugins/slider');
function createStuDialog(cont) {
var opt = {
className: 'stu-dialog',
myCloseIcon: 'close-icon',
content: cont
};
var myDialog = new Dialog(opt);
myDialog.show();
}
// 关掉所有下拉框
function closeStuSelect() {
$('.ident-select-wrap').removeClass('active');
$('.ident-select-wrap .select-ul').empty();
}
/**
* 清除加载的下拉数据,还原点击状态,获取点击数据
* @param
* @param
*/
function clearSelectUl($li, code) {
var $a = $li.find('a'),
$ul = $li.parent();
var $show = $ul.prev(),
$wrap = $ul.parent(),
$input = $wrap.prev();
$show.html($a.html());
if (arguments.length === 2) {
$show.attr(code, $a.attr(code));
$input.val($a.attr(code));
} else {
$input.val($a.html());
}
$ul.empty();
$wrap.removeClass('active');
}
/**
* 最终提交表单
*/
function submitIdentity(json) {
$.ajax({
type: 'GET',
url: '/product/students/verify',
data: json
}).then(function() {
});
}
lazyLoad($('img.lazy'));
// 根据配置执行展示弹窗
function couponAlert(opt) {
var newAlert = new Dialog(opt);
newAlert.show();
}
// 配置弹窗
alertConfig = {
success: {
content: '恭喜您,成功领取优惠券',
subContents: ['特殊情况下到账有延时', '请耐心等待'],
className: 'subcontent-dialog',
refreshOnClose: true,
btns: [
{
id: 1,
name: '去购物啦',
btnClass: ['black', 'btn-close'],
cb: function() {
redirect.goToGuang();
}
},
{
id: 2,
name: '查看优惠券',
btnClass: ['btn-close'],
cb: function() {
redirect.goToCheck();
}
}
]
},
alreadyGot: {
content: '您已领取过优惠券',
subContent: '快去选购心仪的潮品吧',
className: 'subcontent-dialog',
btns: [
{
id: 1,
name: '去使用',
btnClass: ['btn-close'],
cb: function() {
redirect.goToGuang();
}
}
]
},
expired: {
content: '优惠券已过期',
subContent: '去领最新的优惠券吧',
className: 'subcontent-dialog',
btns: [
{
id: 1,
name: '关闭',
btnClass: ['btn-close']
}
]
},
failed: {
content: '领取失败',
subContents: ['请刷新重试,', '多次无效请联系客服'],
className: 'subcontent-dialog',
btns: [
{
id: 1,
name: '刷新',
btnClass: ['btn-close'],
cb: function() {
window.location.reload();
}
}
]
}
};
// 对应不同的网络返回码展示不同的弹窗
makeAlert = {
200: function() {
couponAlert(alertConfig.success);
},
401: function() {
couponAlert(alertConfig.alreadyGot);
},
315: function() {
couponAlert(alertConfig.expired);
},
400: function() {
couponAlert(alertConfig.failed);
},
403: function() {
// do not make alert when user are not login in
}
};
function requestCoupon(id) {
$.ajax({
type: 'GET',
url: '/coupon/sendcoupon',
data: {
id: id
},
success: function(res) {
var code = res.code;
// 如果返回的数据里有url,则执行redirect里的方法
res.url && res.url.length > 0 && redirect[code] &&
redirect[code](res.url + '?' + location.href.split('?')[1]);
// 如果能找到对应code则执行相应方法,如果没有的对应code则执行error提示
makeAlert[code] ? makeAlert[code]() : makeAlert['400']();
},
error: function() {
var Alert = require('../common/dialog').Alert;
new Alert('网络异常').show();
}
});
}
$('.slide-container').slider({
orient: true
});
$sortItem.on('click', function() {
$(this).siblings('.active').removeClass('active');
$(this).addClass('active');
});
// 加载学校地区数据
$stuProv.on('click', function() {
var $provUl = $(this).next();
$provUl.empty();
if ($stuProvWrap.hasClass('active')) {
$stuProvWrap.removeClass('active');
} else {
closeStuSelect();
$stuProvWrap.addClass('active');
$.ajax({
type: 'GET',
url: '/product/students/schoolArea'
}).then(function(data) {
if (data.code === 200) {
$provUl.html(provFn({
provData: data.data
}));
} else if (data.code === 403) {
location.href = data.data.url;
}
});
}
});
$('#stu-province-ul').delegate('li', 'click', function() {
clearSelectUl($(this), 'areacode');
});
$('#stu-school-ul').delegate('li', 'click', function() {
clearSelectUl($(this), 'schoolCode');
});
$('#stu-edu-ul').delegate('li', 'click', function() {
clearSelectUl($(this));
});
$('#stu-entrance-year-ul').delegate('li', 'click', function() {
clearSelectUl($(this));
});
//
$stuSchool.on('click', function() {
var $ul = $(this).next();
$ul.empty();
if ($stuSchoolWrap.hasClass('active')) {
$stuSchoolWrap.removeClass('active');
} else {
closeStuSelect();
$stuSchoolWrap.addClass('active');
$.ajax({
type: 'GET',
url: '/product/students/schoolList',
data: {
areaCode: $stuProv.attr('areaCode')
}
}).then(function(data) {
if (data.code === 200) {
$ul.html(provFn({
schoolData: data.data
}));
} else if (data.code === 403) {
location.href = data.data.url;
}
});
}
});
//
$stuEdu.on('click', function() {
var $ul = $(this).next();
$ul.empty();
if ($stuEduWrap.hasClass('active')) {
$stuEduWrap.removeClass('active');
} else {
closeStuSelect();
$stuEduWrap.addClass('active');
$.ajax({
type: 'GET',
url: '/product/students/eduLevel'
}).then(function(data) {
if (data.code === 200) {
$ul.html(provFn({
eduData: data.data
}));
} else if (data.code === 403) {
location.href = data.data.url;
}
});
}
});
//
$stuYear.on('click', function() {
var $ul = $(this).next();
$ul.empty();
if ($stuYearWrap.hasClass('active')) {
$stuYearWrap.removeClass('active');
} else {
closeStuSelect();
$stuYearWrap.addClass('active');
$ul.html(provFn({
yearData: ['2008', '2009', '2010', '2011', '2012', '2013', '2014', '2015', '2016']
}));
}
});
$couponDia.on('click', function() {
var cont = '<h3 class="dia-title">认证协议</h3>' +
'<p>(1) 全日制大学生及硕士博士研究生;</p>' +
'<p>(2) 学校在可选的范围内,有部分学校可能暂未收录,后期尽快增加;</p>' +
'<p>(3) 每个学号只能认证一个账户;</p>';
createStuDialog(cont);
});
$rightDia.on('click', function() {
var cont = '<h3 class="dia-title">特权详细说明</h3>' +
'<p>(1) 学生购买原价商品,可享受9折优惠,与vip折扣不可同时享受,但取低;</p>' +
'<p>(2) 学生购物每1元可赠送1个有货币;</p>' +
'<p>(3) 学生所在学校购物金额当月累计最高,可获得3个免单名额,每月抽奖;</p>' +
'<p>(4) 不定期学生专享活动;</p>';
createStuDialog(cont);
});
$agreenShow.on('click', function() {
var $ck = $(this);
if ($ck.hasClass('active')) {
$ck.removeClass('active');
} else {
$ck.addClass('active');
}
});
$('.enable .info').on('click', function(e) {
e.preventDefault();
requestCoupon($(this).closest('a').data('id'));
redirect.gunangSrc = $(this).closest('a').get(0).href;
});
$('#identity-Btn').on('click', function() {
var identData = {
name: $('#stu-name').val(),
certNo: $('#stu-idNum').val(),
collegeName: $('#stu-school').val(),
educationDegree: $('#stu-edu').val(),
enrollmentYear: $('#stu-entrance-year').val()
};
var $prompt = $('#prompt'),
$agreen = $('#agreen-show');
if (identData.name === '') {
$prompt.html('真实姓名不能为空');
return;
}
if (!/^[\u4e00-\u9fa5a-zA-Z]{2,12}$/.test(identData.name)) {
$prompt.html('真实姓名为2~12个汉字或者英文');
return;
}
if (identData.certNo === '') {
$prompt.html('身份证号不能为空');
return;
}
if (!/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/.test(identData.certNo)) {
$prompt.html('身份证号格式错误');
return;
}
if (identData.collegeName === '') {
$prompt.html('学校名称不能为空');
return;
}
if (identData.educationDegree === '') {
$prompt.html('学历程度不能为空');
return;
}
if (identData.enrollmentYear === '') {
$prompt.html('入学年份不能为空');
return;
}
if (!$agreen.hasClass('active')) {
$prompt.html('未同意Yoho!有货学生认证协议');
return;
}
$prompt.html('');
console.info(identData);
submitIdentity(identData);
});
// 重新验证
$('#backForm').on('click', function() {
$identityForm.removeClass('hide').siblings().addClass('hide');
});
// 首次点击去掉灰色
$('.ident-select-wrap').one('click', function() {
$(this).removeClass('gray-p');
});