reg.js
15 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
/*
* @description 注册
* @author TaoHuang
* @time 2016/7/14
*/
var $ = require('yoho-jquery'),
regValidate = require('../common/mail-phone-regx'),
computeComplex = require('../common/pwd-strength'),
EventProxy = require('yoho-eventproxy');
var $registerPage = $('.register-page');
var $phoneNumInput = $('#phone-num'),
$smsCaptchaInput = $('#msg-captcha'),
$smsCaptchaCtrl = $('#send-captcha'),
$passwordInput = $('#pwd'),
$imgCaptchaInput = $('#captcha'),
$agreeTermsInput = $('.agree-terms'),
$regionSelectHeader = $('#country-select-header'),
$regionSelectList = $('#country-select-list'),
$regionCodeText = $('#country-code'),
$registerBtn = $('#register-btn');
var $phone = $phoneNumInput;
var second = 60;
var ep = new EventProxy();
var $pwdIntensity = $('.pwd-intensity'),
$pwdParent = $pwdIntensity.closest('.pwd-intensity-container');
var pwdReg = regValidate.pwdValidateRegx;
var checkboxIcon = {
checked: '',
unchecked: ''
};
var upDown = {
up: '',
down: ''
};
var selectedIcon = '';
require('yoho-jquery-placeholder');
function errTip(ele, msg) {
var $errTip = ele.next('.tips');
var $errMsg = $errTip.find('.content');
$errMsg.text(msg);
return $errTip.removeClass('hide');
}
function hideTip(ele) {
return ele.next('.tips').addClass('hide');
}
setTimeout(function() {
$smsCaptchaInput.val('');
$passwordInput.val('');
$imgCaptchaInput.val('');
}, 0);
// IE8 placeholder
$('[placeholder]').placeholder();
// 刷新图形验证码
function refreshImgCaptcha() {
var time = new Date(),
$captchaImg = $('.captcha-img'),
captchaImgSrc = $captchaImg.attr('src').split('?')[0];
$captchaImg.attr('src', captchaImgSrc + '?t=' + time.getTime());
}
// 手机号ajax校验
function validatePhoneNumAsync() {
return $.ajax({
url: '/passport/reg/checkmobile',
type: 'POST',
data: {
mobile: $phoneNumInput.val(),
area: $regionCodeText.text().split('+')[1]
}
}).then(function(result) {
if (!result) {
return false;
}
switch (result.code) {
case 200:
return true;
case 400:
refreshImgCaptcha();
errTip($phone, result.message);
return false;
default:
return false;
}
});
}
function validateImgCaptchaLocal() {
var length = $imgCaptchaInput.val().length;
if (length === 0) {
errTip($imgCaptchaInput, '请输入验证码');
return false;
} else if (length !== 4) {
errTip($imgCaptchaInput, '验证码不正确');
return false;
} else {
return true;
}
}
// 图形验证码ajax校验
function validateImgCaptchaAsync() {
return $.ajax({
type: 'POST',
url: '/passport/reg/piccaptcha',
data: {
verifyCode: $imgCaptchaInput.val()
}
}).then(function(data) {
if (!data) {
return false;
}
switch (data.code) {
case 200:
return true;
case 400:
refreshImgCaptcha();
errTip($imgCaptchaInput, '图形验证码错误');
return false;
default:
return false;
}
});
}
function validateImgCaptcha() {
return (function() {
var defer = $.Deferred(); // eslint-disable-line
if (validateImgCaptchaLocal()) {
validateImgCaptchaAsync().then(function(result) {
if (result) {
defer.resolve(true);
} else {
defer.reject(false);
}
});
} else {
defer.reject(false);
}
return defer.promise();
}()).then(function() {
hideTip($imgCaptchaInput);
ep.emit('captcha-img', true);
}).fail(function() {
ep.emit('captcha-img', false);
});
}
function validateSMSCaptchaLocal() {
var length = $smsCaptchaInput.val().length;
if (length === 0) {
errTip($smsCaptchaInput, '请输入短信验证码');
return false;
} else if (length !== 4) {
errTip($smsCaptchaInput, '短信验证码不正确');
return false;
} else {
return true;
}
}
// 短信验证码ajax验证
function validateSMSCaptchaAsync() {
return $.ajax({
type: 'POST',
url: '/passport/reg/msgcaptcha',
data: {
code: $.trim($smsCaptchaInput.val()),
mobile: $.trim($phoneNumInput.val()),
area: $regionCodeText.text().split('+')[1]
}
}).then(function(data) {
if (!data) {
return false;
}
switch (data.code) {
case 200:
return true;
case 400:
errTip($smsCaptchaInput, '短信验证码错误');
return false;
default:
return false;
}
});
}
function validateSMSCaptcha() {
return (function() {
var defer = $.Deferred(); // eslint-disable-line
if (validateSMSCaptchaLocal()) {
validateSMSCaptchaAsync().then(function(result) {
if (result) {
defer.resolve(true);
} else {
defer.reject(false);
}
});
} else {
defer.reject(false);
}
return defer.promise();
}()).then(function() {
hideTip($smsCaptchaInput);
ep.emit('captcha-sms', true);
}).fail(function() {
ep.emit('captcha-sms', false);
});
}
function validatePhoneNumLocal() {
var phoneNum = $.trim($phoneNumInput.val()),
length = phoneNum.length,
regionCode = $regionCodeText.text();
if (length === 0) {
errTip($phone, '请输入手机号码');
return false;
}
if (/^[0-9]+$/.test(phoneNum)) {
// 这里只做中国区验证
if (regionCode === '+86') {
if (length === 11 && regValidate.phoneRegx['+86'].test(phoneNum)) {
return true;
} else {
errTip($phone, '手机号码不正确,请重新输入');
return false;
}
}
return true;
} else {
return false;
}
}
function validatePhoneNum() {
return (function() {
var defer = $.Deferred(); // eslint-disable-line
if (validatePhoneNumLocal()) {
validatePhoneNumAsync().then(function(result) {
if (result) {
defer.resolve(true);
} else {
defer.reject(false);
}
});
} else {
defer.reject(false);
}
return defer.promise();
}()).then(function() {
hideTip($phone);
ep.emit('phone-num', true);
}).fail(function() {
ep.emit('phone-num', false);
});
}
function validatePasswordLocal() {
var val = $passwordInput.val(),
length = val.length;
if (length === 0) {
errTip($passwordInput, '请输入密码');
return false;
} else if (length < 6 || val.length > 20) {
errTip($passwordInput, '密码只支持6-20位字符');
return false;
} else if (!pwdReg.test(val)) {
errTip($passwordInput, '密码只支持 6-20 位字符,建议字母+数字的组合');
return false;
} else {
return true;
}
}
// 验证密码合法性
function validatePassword() {
return (function() {
var defer = $.Deferred(); // eslint-disable-line
if (validatePasswordLocal()) {
defer.resolve(true);
} else {
defer.reject(false);
}
return defer.promise();
}()).then(function() {
hideTip($passwordInput);
ep.emit('password', true);
}).fail(function() {
ep.emit('password', false);
});
}
// 验证同意服务条款
function validateAgreeLocal() {
return $agreeTermsInput.hasClass('checked');
}
// 验证同意异步调用
function validateAgree() {
var defer = $.Deferred(); // eslint-disable-line
if (validateAgreeLocal()) {
defer.resolve(true);
} else {
defer.reject(false);
}
return defer.promise();
}
// 密码强度验证
function validatePasswordComplexLocal($obj) {
var pwd = $obj.val(),
level = computeComplex(pwd);
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;
}
}
/**
* 检查所有的条件
*/
function validate() {
return $.when(validatePhoneNum(),
validateImgCaptcha(),
validateSMSCaptcha(),
validatePassword(),
validateAgree()
);
}
// 注册
function registerAsync() {
return $.ajax({
type: 'POST',
url: '/passport/reg/mobileregister',
data: {
area: $regionCodeText.text().split('+')[1],
mobile: $.trim($phoneNumInput.val()),
verifyCode: $imgCaptchaInput.val(),
code: $smsCaptchaInput.val(),
password: $passwordInput.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 {
console.log('网络超时~');
}
});
}
/**
* 增加验证注册
*/
function register() {
return validate().then(function() {
return registerAsync();
});
}
// 发送短信验证码
function sendSMSCaptcha() {
return $.ajax({
type: 'POST',
url: '/passport/reg/sendBindMsg',
data: {
area: $regionCodeText.text().split('+')[1],
mobile: $.trim($phoneNumInput.val()),
verifyCode: $imgCaptchaInput.val()
}
});
}
// 短信验证码重新发送
function disableSMSBtn() {
second -= 1;
if (second < 0) {
second = 60;
$smsCaptchaCtrl.text('获取短信验证码').removeClass('disable').removeClass('second-progress');
} else {
$smsCaptchaCtrl.addClass('second-progress').text(second + '秒后可重新操作');
window.setTimeout(disableSMSBtn, 1000);
}
}
exports.init = function() {
// 验证手机输入
$phoneNumInput.on('blur', function() {
validatePhoneNum();
$('#phone').removeClass('focus');
}).on('focus', function() {
hideTip($phoneNumInput);
$('#phone').addClass('focus');
});
// 验证图形输入
$imgCaptchaInput.on('blur', function() {
validateImgCaptcha();
$imgCaptchaInput.removeClass('focus');
}).on('focus', function() {
hideTip($imgCaptchaInput);
$imgCaptchaInput.addClass('focus');
});
// 手机号验证通过,发送短信按钮可点击
ep.tail('phone-num', function(phoneAuth) {
if (phoneAuth && !$smsCaptchaCtrl.hasClass('second-progress')) {
$smsCaptchaCtrl.removeClass('disable');
} else {
$smsCaptchaCtrl.addClass('disable');
}
});
// 验证短信输入
$smsCaptchaInput.on('blur', function() {
validateSMSCaptcha();
$smsCaptchaInput.removeClass('focus');
}).on('focus', function() {
hideTip($smsCaptchaInput);
$smsCaptchaInput.addClass('focus');
});
// 验证密码输入
$passwordInput.on('keyup', function() {
validatePasswordComplexLocal($passwordInput);
}).on('focus', function() {
hideTip($passwordInput);
$passwordInput.addClass('focus');
}).on('blur', function() {
$passwordInput.removeClass('focus');
validatePassword();
});
// 服务条款
$agreeTermsInput.on('click', function() {
var $this = $(this);
$this.toggleClass('checked');
if ($this.hasClass('checked')) {
// 勾选
$this.children('span').html(checkboxIcon.checked);
ep.emit('agree', true);
} else {
// 取消勾选
$this.children('span').html(checkboxIcon.unchecked);
ep.emit('agree', false);
}
});
// 修改
function changeHeader() {
var $indicator = $regionSelectHeader.find('.iconfont');
if ($regionSelectList.hasClass('hide')) {
$indicator.html(upDown.up);
} else {
$indicator.html(upDown.down);
}
}
// 选择区域代码,验证手机号
// 选择国家列表
$regionSelectList.on('click', '.option', function() {
var $clickItem = $(this);
var areaCode = $clickItem.data('code');
var name = $clickItem.data('value');
var $selectedItem = $clickItem.siblings('.selected');
$selectedItem.find('.iconfont').html('').end().removeClass('selected');
$clickItem.find('.iconfont').html(selectedIcon).end().addClass('selected');
$regionSelectHeader.find('.name').html(name);
$regionCodeText.text(areaCode);
$regionSelectList.addClass('hide');
changeHeader();
});
// 选择国家头
$regionSelectHeader.on('click', function() {
$regionSelectList.toggleClass('hide');
changeHeader();
validatePhoneNum();
});
// 点击发送验证码
$smsCaptchaCtrl.on('click', function() {
if ($smsCaptchaCtrl.hasClass('disable') || $smsCaptchaCtrl.hasClass('second-progress')) {
return;
}
$smsCaptchaCtrl.addClass('disable');
disableSMSBtn();
sendSMSCaptcha();
});
// 点击注册提交
$registerBtn.on('click', function() {
register();
});
// 按回车键提交
$registerPage.on('keypress', function(e) {
var key = e.which;
if (key === 13) {
$registerBtn.triggerHandler('click');
}
});
// 换图形验证码
$('.change-captcha').click(function() {
refreshImgCaptcha();
});
};