reg.js
19 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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/*
* @description 注册页js
* @time 2015/12/14
*/
var $ = require('yoho-jquery'),
regValidate = require('../common/mail-phone-regx'),
computeComplex = require('../common/pwd-strength');
var $registerPage = $('.register-page'),
$pwdTips = $('#pwd-tips'),
$pwdTip1 = $pwdTips.find('#pwd-tip1'),
$errTip = $('#err-tip'),
$registerBtn = $('#register-btn'),
$countDown = $('#count-down'),
$successBtn = $('.success-btn'),
countDown = 5,
clearT;
var $sendCaptcha = $('#send-captcha'),
caCount = 4,
validateResult = [];
var $pn = $('#phone-num'),
$mc = $('#msg-captcha'),
$pwd = $('#pwd'),
$repwd = $('#repwd'),
$ca = $('#captcha'),
timeResidue = 0;
// 密码强度验证
var $pwdIntensity = $('.pwd-intensity'),
$pwdParent = $pwdIntensity.closest('.pwd-intensity-container');
// signup验证
var $region = $('#country-code'),
$regionSelect = $('#region');
var isPwd = false,
pwdVal;
var pwdReg = regValidate.pwdValidateRegx;
var $curErrContainer;
var oldPhone = ''; // 如果手机号改变时刷新验证码
require('../../simple-header');
require('yoho-jquery-placeholder');
// 验证码位数
$ca.attr('maxlength', caCount);
// 密码规则提示
$pwd.focus(function() {
$pwdTips.removeClass('hide');
isPwd = true;
pwdVal = $(this).val();
}).blur(function() {
$pwdTips.addClass('hide');
isPwd = false;
});
// IE8 placeholder
$('[placeholder]').placeholder();
// 存储校验信息
validateResult = [
{
id: 'phone-num',
message: '', // 错误信息
status: false // 当前的状态
},
{
id: 'captcha',
message: '',
status: false
},
{
id: 'msg-captcha',
message: '',
status: false
},
{
id: 'pwd',
message: '',
status: false
},
{
id: 'repwd',
message: '',
status: false
}
];
// 刷新图形验证码
function refreshPic() {
var time = new Date(),
$captchaImg = $('.captcha-img'),
captchaImgSrc = $captchaImg.attr('src').split('?')[0];
$captchaImg.attr('src', captchaImgSrc + '?t=' + time.getTime());
}
// 手机号ajax校验
function phoneAjaxFn(page, callback) {
var url,
data;
if (page === 'reg') {
url = '/passport/reg/checkmobile';
data = {
mobile: $pn.val(),
area: $region.text().split('+')[1]
};
} else if (page === 'third') {
url = '/passport/autouserinfo/bindCheck';
data = {
mobile: $pn.val(),
area: $region.text().split('+')[1],
openId: $('#open-id').val(),
sourceType: $('#source-type').val()
};
}
validateResult[0].message = 'err';
validateResult[0].status = false;
$.ajax({
url: url,
type: 'POST',
data: data
}).then(function(res) {
if (!res) {
return;
}
switch (res.code) {
case 200:
if ($pn.val() !== oldPhone && oldPhone !== '') {
// 如果手机号发生改变则刷新验证码并且更新状态
refreshPic();
$sendCaptcha.addClass('disable').attr('disabled', 'disabled');
validateResult[1].message = '图形验证码错误';
validateResult[1].status = false;
}
oldPhone = $pn.val();
validateResult[0].message = '';
validateResult[0].status = true;
break;
case 400:
refreshPic();
validateResult[0].message = res.message;
validateResult[0].status = false;
break;
default:
break;
}
callback();
});
}
// 图形验证码ajax校验
function picCaptchaAjaxFn(page, callback) {
var url;
if (page === 'reg') {
url = '/passport/reg/piccaptcha';
} else if (page === 'third') {
url = '/passport/autouserinfo/checkPicCode';
}
$.ajax({
type: 'POST',
url: url,
data: {
verifyCode: $ca.val()
// mobile: $pn.val(),
// area: $region.text().split('+')[1]
}
}).then(function(data) {
if (!data) {
return;
}
switch (data.code) {
case 200:
validateResult[1].message = '';
validateResult[1].status = true;
break;
case 400:
refreshPic();
validateResult[1].message = '图形验证码错误';
validateResult[1].status = false;
break;
default:
break;
}
callback();
});
}
// 短信验证码ajax校验
function msgCaptchaAjaxFn(page, callback) {
var url;
if (page === 'reg') {
url = '/passport/reg/msgcaptcha';
} else if (page === 'third') {
url = '/passport/autouserinfo/checkBindMsg';
}
$.ajax({
type: 'POST',
url: url,
data: {
code: $mc.val(),
mobile: $pn.val(),
area: $region.text().split('+')[1]
}
}).then(function(data) {
if (!data) {
return;
}
switch (data.code) {
case 200:
validateResult[2].message = '';
validateResult[2].status = true;
break;
case 400:
validateResult[2].message = '短信验证码错误';
validateResult[2].status = false;
break;
default:
break;
}
callback();
});
}
// 验证
function validateRule(page, $element, callback) {
var val = $.trim($element.val()),
regionCode;
// 根据需求http://redmine.yoho.cn/issues/3117改成上边的正则
// 对应的错误提示语也改了,感觉要不了多久就会改回来
// pwdReg = /^([a-zA-Z0-9\-\+_!@\#$%\^&\*\(\)\:\;\.=\[\]\\\',\?]){6,20}$/gi;
// 手机号校验
if ($element.hasClass('phone-num')) {
regionCode = $region.text();
if (val === '') {
validateResult[0].message = '请输入手机号码';
validateResult[0].status = false;
return callback();
} else {
if (!/^[0-9]+$/.test(val) ||
(regionCode === '+86' && !regValidate.phoneRegx[regionCode].test(val))) {
validateResult[0].message = '手机号码格式不正确,请重新输入';
validateResult[0].status = false;
return callback();
} else {
phoneAjaxFn(page, callback);
}
}
// 图形验证码校验
} else if ($element.hasClass('captcha')) {
if (val === '') {
validateResult[1].message = '请输入图形验证码';
validateResult[1].status = false;
return callback();
} else if (val.length <= 3) {
validateResult[1].message = '图形验证码为4位';
validateResult[1].status = false;
return callback();
} else {
// 并且手机号正确
if (validateResult[0].status) {
picCaptchaAjaxFn(page, callback);
} else {
validateResult[1].message = '图形验证码错误';
validateResult[1].status = false;
return callback();
}
}
// 短信验证码校验
} else if ($element.hasClass('msg-captcha')) {
if (val === '') {
validateResult[2].message = '请输入短信验证码';
validateResult[2].status = false;
return callback();
} else if (val.length <= 3) {
validateResult[2].message = '短信验证码错误';
validateResult[2].status = false;
return callback();
} else {
// 并且图形验证码正确
if (validateResult[1].status) {
msgCaptchaAjaxFn(page, callback);
} else {
validateResult[2].message = '短信验证码错误';
validateResult[2].status = false;
return callback();
}
}
// 密码校验
} else if ($element.hasClass('pwd')) {
if (val === '') {
validateResult[3].message = '请输入密码';
validateResult[3].status = false;
} else if (val.length < 6 || val.length > 20) {
validateResult[3].message = '密码只支持6-20位字符';
validateResult[3].status = false;
} else if (!pwdReg.test($element.val())) {
validateResult[3].message = '密码须字母和数字组合';
validateResult[3].status = false;
} else {
validateResult[3].message = '';
validateResult[3].status = true;
}
return callback();
// 二次密码校验
} else if ($element.hasClass('repwd')) {
if (val === '') {
validateResult[4].message = '请输入密码确认';
validateResult[4].status = false;
} else if ($pwd.val() !== val) {
validateResult[4].message = '与密码不一致,请重新输入';
validateResult[4].status = false;
} else {
validateResult[4].message = '';
validateResult[4].status = true;
}
return callback();
}
}
function posErrTip() {
var tipPos = $curErrContainer.offset();
return $errTip.css({
top: tipPos.top - 40,
left: tipPos.left
});
}
// 显示提示信息
function showErrTip() {
var show = false,
i,
validateResultLen = validateResult.length;
for (i = 0; i < validateResultLen; i++) {
if (!show) {
// 不可以通过status判断
if (!!validateResult[i].message && validateResult[i].message !== 'err') {
// 显示错误提示
$errTip.find('span').text(validateResult[i].message);
$curErrContainer = $('#' + validateResult[i].id);
posErrTip().removeClass('hide');
show = true; // 停止判断
} else {
$errTip.addClass('hide');
}
}
}
}
// 显示红色边框
function showBorder() {
var $errInput,
i,
validateResultLen = validateResult.length;
for (i = 0; i < validateResultLen; i++) {
if (validateResult[i].message) {
// 显示红色边框
$errInput = $('#' + validateResult[i].id);
$errInput.addClass('error');
} else {
// 去掉红色边框
$errInput = $('#' + validateResult[i].id);
$errInput.removeClass('error');
}
}
}
// 密码强度验证
function pwdFn($obj) {
var pwd = $obj.val(),
pwdStrength = computeComplex(pwd),
level = 0;
if (pwdStrength === 0) {
level = 0;
} else if (pwdStrength <= 10) {
level = 1;
} else if (pwdStrength <= 20) {
level = 2;
} else {
level = 3;
}
switch (level) {
case 0:
$pwdParent.removeClass('red yellow green');
$pwdIntensity.removeClass('color');
break;
case 1:
$pwdParent.addClass('red').removeClass('yellow green');
$pwdIntensity.filter('.low').addClass('color');
$pwdIntensity.filter('.mid,.high').removeClass('color');
break;
case 2:
$pwdParent.addClass('yellow').removeClass('red green');
$pwdIntensity.filter('.low,.mid').addClass('color');
$pwdIntensity.filter('.high').removeClass('color');
break;
case 3:
$pwdParent.addClass('green').removeClass('yellow red');
$pwdIntensity.addClass('color');
break;
default:
break;
}
// 提示框
if (pwd === '') {
$pwdTip1.removeClass('red yes no').addClass('default');
} else if (pwd.length < 6 || pwd.length > 20) {
$pwdTip1.removeClass('default yes').addClass('no red');
} else {
$pwdTip1.removeClass('default no red').addClass('yes');
}
}
// 重新定位错误提示
$(window).resize(function() {
if ($errTip.hasClass('hide')) {
return;
}
posErrTip();
});
// 失去焦点时开始校验
// Tips: 不可以在获得焦点的时候验证,获得焦点和失去焦点的间隔太小,如果中间存在ajax校验的话会出现问题
// ( ▼-▼ )注册页和信息完善页面接口不同
exports.init = function(page) {
$('#agree-terms').click(function() {
var $this = $(this),
i,
passI = 0;
if ($this.attr('notchecked')) {
$this.removeAttr('notchecked');
// 勾选
for (i = 0; i < validateResult.length; i++) {
if (validateResult[i].status) {
passI++;
}
}
if (passI === 4) {
$registerBtn.removeClass('disable').removeAttr('disabled');
}
} else {
// 取消勾选
$registerBtn.addClass('disable').attr('disabled', 'disabled');
$this.attr('notchecked', 'notchecked');
}
});
// 按回车键提交
$registerPage.on('keydown', function(e) {
var key = e.which;
if (key === 13) {
$registerBtn.click();
}
});
$registerPage.find('.va').keyup(function() {
var j,
statusLen = 0,
vLen = validateResult.length,
$that = $(this);
validateRule(page, $(this), function() {
showErrTip(); // 显示错误提示
showBorder(); // 显示红色边框
// 如果validateResult中有4个status为true表示验证通过
for (j = 0; j < vLen; j++) {
if (validateResult[j].status) {
statusLen++;
}
}
if (statusLen === 4 && !$('#agree-terms').attr('notchecked')) {
$registerBtn.removeClass('disable').removeAttr('disabled');
} else {
$registerBtn.addClass('disable').attr('disabled', 'disabled');
}
// 图形验证通过时,发送短信按钮可点击
if (validateResult[1].status && timeResidue <= 0) {
$sendCaptcha.removeClass('disable').removeAttr('disabled');
} else {
$sendCaptcha.addClass('disable').attr('disabled', 'disabled');
}
// 图形验证通过时,发送短信按钮可点击 end
});
// 如果是密码则校验强度
if (($that).hasClass('pwd')) {
pwdFn($that);
}
}).blur(function() {
/* validateRule($(this), function() {
showErrTip();
showBorder(); // 显示红色边框
});*/
});
$regionSelect.change(function() {
$region.text($('#region').val());
validateRule(page, $pn, showErrTip); // 验证
});
// 点击发送验证码
$sendCaptcha.click(function() {
var t,
url;
if ($(this).hasClass('disable')) {
return;
}
if (page === 'reg') {
url = '/passport/reg/sendBindMsg';
} else if (page === 'third') {
url = '/passport/autouserinfo/sendBindMsg';
}
// todo ajax 发送验证码
$.ajax({
type: 'POST',
url: url,
data: {
area: $region.text().split('+')[1],
mobile: $pn.val(),
verifyCode: $ca.val()
}
}).then(function(result) {
if (result.code !== 200) {
validateResult[0].message = result.message;
validateResult[0].status = false;
showErrTip();
return;
}
$('#msg-tip').removeClass('hide');
timeResidue = 60;
$sendCaptcha.addClass('disable').attr('disabled', 'disabled');
$sendCaptcha.val('60秒可重新发送');
t = setInterval(function() {
if (timeResidue <= 0) {
$sendCaptcha.removeClass('disable').removeAttr('disabled').val('获取短信验证码');
clearInterval(t);
return;
}
$sendCaptcha.val(timeResidue-- + '秒可重新发送');
}, 1000);
});
});
// 防止粘贴密码
if ($('.success-box').length <= 0) {
$pwd[0].onpaste = function() {
return false;
};
}
// ajax表单提交
$registerBtn.click(function() {
var url;
if (page === 'reg') {
url = '/passport/reg/mobileregister';
} else if (page === 'third') {
url = '/passport/autouserinfo/bindMobile';
}
if ($(this).hasClass('disable')) {
return;
} else {
// ajax提交
$.ajax({
type: 'POST',
url: url,
data: {
area: $region.text().split('+')[1],
mobile: $pn.val(),
verifyCode: $ca.val(),
code: $mc.val(),
password: $pwd.val()
}
}).then(function(data) {
var time = 0,
t,
refer;
if (data.code === 200) {
refer = data.data.href;
t = setTimeout(function() {
time++;
}, 1000);
$.ajax({
type: 'GET',
url: data.data.session
}).then(function(res) {
if (res.code === 200 && t < 3) {
clearInterval(t);
location.href = refer;
}
});
if (t >= 3) {
clearInterval(t);
location.href = refer;
}
} else {
// todo
console.log('网络超时~');
}
});
}
});
$('.change-captcha').click(function() {
refreshPic();
});
// 注册成功页面5秒后跳转
if ($('.success-box').length > 0) {
clearT = setInterval(function() {
if (countDown === 0) {
window.location.href = $successBtn.attr('data-url');
clearInterval(clearT);
}
$countDown.text(countDown--);
}, 1000);
}
// 设置 refer 信息
(function() {
var refer = queryString().refer || ''; // eslint-disable-line
if (refer) {
setCookie('refer', refer); // eslint-disable-line
} else {
setCookie('refer', ''); // eslint-disable-line
}
}());
};