index.js
18.3 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
/**
* @description 登录
* @author TaoHuang
* @time 2016/7/14
*/
var $ = require('yoho-jquery');
var EventProxy = require('yoho-eventproxy');
var $phoneNumInput = $('#account'),
$passwordInput = $('#password'),
$imgCaptchaInput = $('#captcha'),
$smsCaptchaInput = $('#captcha-sms'),
$smsCodeWrap = $('.sms-code-wrap'),
$pwdWrap = $('.pwd-wrap'),
$captchaSmsTokenHideInput = $('#captcha-sms-token-hide'),
$loginBtn = $('#login-btn'),
$phone = $('#phone'),
$smsBtn = $('#sms-btn'),
$regionCodeText = $('#country-code'),
$regionSelectList = $('#country-select-list'),
$regionSelectHeader = $('#country-select-header'),
$emailAutoComplete = $('#email-autocomplete');
var $loginTip = $loginBtn.siblings('.login-fail-tip'),
ep = new EventProxy();
var mailPhoneRegx = require('../common/mail-phone-regx');
var mailAc = require('../common/ac-email'); // 邮箱自动完成
var $remember = $('.remember-me');
var captchaUrl = '/passport/images?t='; // /passport/images?t=1454464125
var showCaptcha = false;
var $captchaWrap = $('.captcha-wrap'),
$captchaImg = $captchaWrap.find('#captcha-img');
// checkbox status unicode
var checkbox = {
checked: '',
unchecked: ''
};
var upDown = {
up: '',
down: ''
};
var selectedIcon = '';
// 短信验证码的计数器,60s
var secondCount = 60;
// 短信验证码只能验证一次
var isSmsCheckedSuccessFlag = false;
var currLoginType = 'PasswordLogin';
require('yoho-jquery-placeholder');
function refreshCaptcha() {
$captchaImg.attr('src', captchaUrl + $.now());
$imgCaptchaInput.val('');
}
function errTip(ele, msg) {
var $errTip = ele.next('.tips');
var $errMsg = $errTip.find('.content');
$errMsg.html(msg);
return $errTip.removeClass('hide');
}
function hideTip(ele) {
return ele.next('.tips').addClass('hide');
}
// 获取区域号
function getArea() {
return $regionCodeText.text().replace('+', '');
}
// 获取用户输入的手机号
function getMoblie() {
return $.trim($phoneNumInput.val());
}
// 验证账户名
function validateAccountLocal() {
var phoneNum = getMoblie(),
regionCode = $regionCodeText.text();
if (phoneNum !== '') {
if (/^[0-9]+$/.test(phoneNum)) {
// 这里只做中国区验证
if (regionCode === '+86') {
if (phoneNum.length === 11 && mailPhoneRegx.phoneRegx['+86'].test(phoneNum)) {
return true;
} else {
errTip($phoneNumInput, '手机号码不正确,请重新输入');
return false;
}
}
return true;
} else {
// 邮箱验证
if (mailPhoneRegx.emailRegx.test(phoneNum)) {
return true;
} else {
errTip($phoneNumInput, '邮箱格式不正确,请重新输入');
return false;
}
}
} else {
errTip($phoneNumInput, '请输入账号');
return false;
}
}
// 异步验证帐号是否存在
function validateAccountAsync(url) {
return $.ajax({
type: 'POST',
url: url,
data: {
phoneNum: getMoblie(),
area: getArea()
}
}).then(function(data) {
if (data.code && data.code === 200) {
return true;
} else {
errTip($phoneNumInput, data.message);
return false;
}
});
}
// 整合本地和异步验证信息
function validateAccount() {
return (function() {
var defer = $.Deferred(); // eslint-disable-line
var url = '/passport/login/user';
if (currLoginType === 'SMSLogin') {
url = '/passport/login/sms/checkuser';
}
if (validateAccountLocal()) {
validateAccountAsync(url).then(function(result) {
if (result) {
defer.resolve(result);
} else {
defer.reject(result);
}
});
} else {
defer.reject(false);
}
return defer.promise();
}()).then(function() {
hideTip($phoneNumInput);
ep.emit('phone', true);
}).fail(function() {
ep.emit('phone', false);
});
}
// 验证密码
function validatePasswordLocal() {
var password = $.trim($passwordInput.val());
var length = password.length;
if (length !== 0) {
if (length < 6) {
ep.emit('password', false);
errTip($passwordInput, '请输入长度为6-20字符的密码');
return false;
} else {
ep.emit('password', true);
return true;
}
} else {
errTip($passwordInput, '请输入密码');
ep.emit('password', false);
return false;
}
}
// 图形验证验证码
function validateCaptchaLocal() {
var captcha = $.trim($imgCaptchaInput.val());
var length = captcha.length;
switch (length) {
case 0:
errTip($imgCaptchaInput, '请输入验证码');
ep.emit('captcha', false);
return false;
case 4:
ep.emit('captcha', true);
return true;
default:
errTip($imgCaptchaInput, '请输入长度为4字符的验证码');
ep.emit('captcha', false);
return false;
}
}
// 异步验证图片验证码
function validateCaptchaImgAsync() {
return $.ajax({
url: '/passport/images/check',
type: 'POST',
data: {
verifyCode: $.trim($imgCaptchaInput.val())
}
}).then(function(result) {
if (result.code === 200) {
return true;
} else {
errTip($imgCaptchaInput, '验证码不正确');
return false;
}
});
}
// 整合本地和异步验证信息
function validateCaptchaImg() {
if ($captchaWrap.hasClass('hide')) {
return true;
}
return (function() {
var defer = $.Deferred(); // eslint-disable-line
if (validateCaptchaLocal()) {
validateCaptchaImgAsync().then(function(result) {
if (result) {
defer.resolve(result);
} else {
defer.reject(result);
}
});
} else {
defer.reject(false);
}
return defer.promise();
}()).then(function() {
hideTip($imgCaptchaInput);
ep.emit('captcha', true);
}).fail(function() {
ep.emit('captcha', false);
refreshCaptcha();
});
}
// 密码错误次数,超过三次显示验证码
function showAccountErrTimes() {
$captchaWrap.removeClass('hide');
showCaptcha = true;
$captchaImg.attr('src', captchaUrl + $.now());
$imgCaptchaInput.val('');
}
// 登录次数限制接口
function throttle() {
return $.ajax({
url: '/passport/login/account',
type: 'GET',
data: {
account: getMoblie()
}
});
}
// 验证帐号系统
function validate() {
return validateAccount().then(function(result) {
if (result) {
return throttle();
} else {
return false;
}
}).then(function(res) {
var defer = $.Deferred(); // eslint-disable-line
if (!res) {
defer.reject(false);
}
if (res.data && res.data.needCaptcha) {
showAccountErrTimes();
defer.reject(false);
}
defer.resolve(true);
return defer.promise();
});
}
// 邮箱自动补全
mailAc($phoneNumInput, function() {
});
$phoneNumInput.on('focus', function() {
hideTip($phoneNumInput);
$phone.addClass('focus');
}).on('blur', function() {
$phone.removeClass('focus');
validate();
});
$('[placeholder]').placeholder();
// 改变选择图标
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();
});
// 密码失去焦点后校验
$passwordInput.on('blur', function() {
$passwordInput.removeClass('focus');
validatePasswordLocal();
}).on('focus', function() {
$passwordInput.addClass('focus');
hideTip($passwordInput);
});
// 图形验证码失去焦点|获取焦点的校验
$imgCaptchaInput.on('blur', function() {
$imgCaptchaInput.removeClass('focus');
validateCaptchaImg();
}).on('focus', function() {
$imgCaptchaInput.addClass('focus');
hideTip($imgCaptchaInput);
});
// 邮箱自动完成列表项点击
$emailAutoComplete.on('click', 'li', function() {
$phoneNumInput.val($(this).text()).focus();
$emailAutoComplete.addClass('hide');
});
// 记住登录状态
$remember.on('click', function() {
var $this = $(this);
$this.toggleClass('checked');
if ($this.hasClass('checked')) {
$this.children('span').html(checkbox.checked);
} else {
$this.children('span').html(checkbox.unchecked);
}
});
// 验证码刷新
$captchaWrap.on('click', '.change-captcha, .captcha-img', function() {
$captchaImg.attr('src', captchaUrl + $.now());
});
// 初始:只带账户名的页面,密码输入获得焦点
if (($phoneNumInput.val() !== '' || $phoneNumInput.val() === $phoneNumInput.attr('placeholder')) &&
$passwordInput.val() === '') {
$passwordInput.focus();
}
// 同时监听三个事件-- 密码登录
ep.tail('phone', 'password', 'captcha', function(phoneAuth, passwordAuth, captchaAuth) {
if (phoneAuth && passwordAuth && captchaAuth) {
$loginBtn.removeClass('auth_ok');
} else {
$loginBtn.addClass('auth_ok');
}
});
// 同时监听三个事件-- 短信验证码登录
ep.tail('phone', 'captcha', 'smsCode', function(phoneAuth, captchaAuth, smsCodeAuth) {
if (phoneAuth && captchaAuth && smsCodeAuth) {
$loginBtn.removeClass('auth_ok');
} else {
$loginBtn.addClass('auth_ok');
}
});
// 如果密码登录的图形验证码隐藏状态就直接通过校验
if (currLoginType === 'PasswordLogin') {
if ($captchaWrap.hasClass('hide')) {
ep.emit('captcha', true);
}
}
ep.on('phone', function(auth) {
if (auth) {
hideTip($phoneNumInput);
}
});
ep.on('password', function(auth) {
if (auth) {
hideTip($passwordInput);
}
});
ep.on('captcha', function(auth) {
if (auth && !$captchaWrap.hasClass('hide')) {
hideTip($imgCaptchaInput);
}
});
ep.on('smsCode', function(auth) {
if (auth && !$smsCodeWrap.hasClass('hide')) {
hideTip($smsCaptchaInput);
}
});
/** ***********************************************手机验证码登录*********************************************/
// 倒计时 60秒
function disable60sSendSmsBtn() {
secondCount -= 1;
if (secondCount < 0) {
secondCount = 60;
$smsBtn.val('获取短信验证码')
.removeClass('second-progress')
.removeClass('disable');
} else {
$smsBtn.addClass('disable')
.addClass('second-progress')
.val(secondCount + '秒后可重新操作');
window.setTimeout(disable60sSendSmsBtn, 1000);
}
}
// 发送短信验证码
function sendCaptchaSmsAsync() {
return $.ajax({
type: 'POST',
url: '/passport/login/sms/send',
data: {
area: getArea(),
mobile: getMoblie()
}
});
}
// 验证码输入本地校验
function validateCaptchaSmsLocal() {
var smsCaptcha = $.trim($smsCaptchaInput.val());
var length = smsCaptcha.length;
switch (length) {
case 0:
errTip($smsCaptchaInput, '请输入短信验证码');
return false;
case 4:
return true;
default:
errTip($smsCaptchaInput, '请输入长度为4字符的验证码');
return false;
}
}
// 异步校验短信验证码是否存在 Note: 这里的验证码只能成功验证一次,十分钟内有效
function validateCaptchaSmsAsync() {
var smsToken = $.trim($captchaSmsTokenHideInput.val());
if (isSmsCheckedSuccessFlag && smsToken !== '') {
return $.Deferred().resolve(true).promise(); // eslint-disable-line
}
return $.ajax({
url: '/passport/login/sms/auth',
type: 'POST',
data: {
area: getArea(),
mobile: getMoblie(),
code: $.trim($smsCaptchaInput.val())
}
}).then(function(data) {
if (data.code && data.code === 200) {
$captchaSmsTokenHideInput.val(data.token);
isSmsCheckedSuccessFlag = true;
return true;
} else if (data.code && data.code === 501) {
errTip($smsCaptchaInput, data.message);
$captchaSmsTokenHideInput.val('');
return false;
} else {
errTip($smsCaptchaInput, '验证码不正确');
$captchaSmsTokenHideInput.val('');
return false;
}
});
}
// 整合本地和异步验证信息--短信验证
function validateCaptchaSms() {
return (function() {
var defer = $.Deferred(); // eslint-disable-line
if (validateCaptchaSmsLocal()) {
validateCaptchaSmsAsync().then(function(result) {
if (result) {
defer.resolve(result);
} else {
defer.reject(result);
}
});
} else {
defer.reject(false);
}
return defer.promise();
}()).then(function() {
hideTip($smsCaptchaInput);
ep.emit('smsCode', true);
}).fail(function() {
ep.emit('smsCode', false);
});
}
// 切换登录方式
function changeLoginType() {
var type = $(this).data('type');
if ($(this).hasClass('selected')) {
return false;
}
currLoginType = type;
if (type === 'PasswordLogin') {
$pwdWrap.removeClass('hide');
$smsCodeWrap.addClass('hide');
if (!showCaptcha) {
$captchaWrap.addClass('hide');
ep.emit('captcha', true);
} else {
refreshCaptcha();
}
hideTip($smsCaptchaInput);
} else if (type === 'SMSLogin') {
$pwdWrap.addClass('hide');
$smsCodeWrap.removeClass('hide');
$captchaWrap.removeClass('hide');
ep.emit('captcha', false);
$smsCaptchaInput.val('');
hideTip($passwordInput);
refreshCaptcha();
}
hideTip($phoneNumInput);
hideTip($imgCaptchaInput);
$(this).siblings().removeClass('selected');
$(this).addClass('selected');
}
$('.login-page .switch > div').off().on('click', changeLoginType);
// 短信验证码发送按钮
$smsBtn.on('click', function() {
// 已在发送短信时间范围内
if ($smsBtn.hasClass('second-progress')) {
return;
}
validateAccount()
.then(validateCaptchaImg)
.then(function() {
disable60sSendSmsBtn();
return sendCaptchaSmsAsync();
});
});
// 短信验证码
$smsCaptchaInput.on('blur', function() {
$smsCaptchaInput.removeClass('focus');
validateCaptchaSms();
}).on('focus', function() {
$smsCaptchaInput.addClass('focus');
hideTip($smsCaptchaInput);
});
/** **************************************************登录事件********************************************/
// 登录
function login() {
return $.ajax({
url: '/passport/login/auth',
type: 'POST',
data: {
areaCode: getArea(),
account: getMoblie(),
password: currLoginType === 'PasswordLogin' ? $.trim($passwordInput.val()) : $captchaSmsTokenHideInput.val(), // eslint-disable-line
captcha: $.trim($imgCaptchaInput.val()),
isRemember: $remember.hasClass('checked') ? true : false,
loginType: currLoginType
},
success: function(res) {
if (res.code === 200) {
if (res.data) {
// 防止data.data为undefined时下行语句执行出错而导致脚本不能走到complete去处理authing
location.href = res.data.refer;
}
} else {
if (res.data.errorType === 'captcha') {
$imgCaptchaInput.val('');
} else {
$loginTip.removeClass('hide').children('em').html(res.message);
$passwordInput.val('');
}
// 验证错误次数
if (res.data && res.data.needCaptcha) {
showAccountErrTimes();
}
}
}
});
}
// 全部的本地验证-- 密码登录
function loginAsync() {
return validateAccount()
.then(validateCaptchaImg)
.then(function() {
var defer = $.Deferred(); // eslint-disable-line
if (validatePasswordLocal()) {
defer.resolve(true);
} else {
defer.reject(false);
}
return defer.promise();
}).then(function() {
return login();
});
}
// 全部的本地验证-- 短信验证登录
function smsLoginAsync() {
return validateAccount()
.then(validateCaptchaImg)
.then(validateCaptchaSms)
.then(function() {
return login();
});
}
// 登录
$loginBtn.on('click', function() {
if ($loginBtn.hasClass('auth_ok')) {
return;
}
if (currLoginType === 'PasswordLogin') {
loginAsync();
} else {
smsLoginAsync();
}
});
// Enter登录
$('input.va').on('keypress', function(e) {
if (e.which === 13) {
if (currLoginType === 'PasswordLogin') {
loginAsync();
} else {
smsLoginAsync();
}
}
});