index.js
8.9 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
/**
* @description 登录
* @author TaoHuang
* @time 2016/7/14
*/
var $ = require('yoho-jquery');
var EventProxy = require('../common/eventproxy');
var $phoneNumInput = $('#account'),
$passwordInput = $('#password'),
$imgCaptchaInput = $('#captcha'),
$loginBtn = $('#login-btn'),
$phone = $('#phone');
var $loginTip = $loginBtn.siblings('.login-fail-tip'),
ep = new EventProxy();
var $regionCodeText = $('#country-code'),
$regionSelectCtrl = $('#country-list');
var $emailAutoComplete = $('#email-autocomplete');
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 $captchaWrap = $('.captcha-wrap'),
$captchaImg = $captchaWrap.find('#captcha-img');
// checkbox status unicode
var checkbox = {
checked: '',
unchecked: ''
};
var $errTip = $('.tips');
var $errMsg = $errTip.find('.rectangle');
require('yoho-jquery-placeholder');
function errTip(ele, msg) {
var topLeft = ele.offset();
$errMsg.text(msg);
return $errTip.css({
top: topLeft.top + ele.height() - 2,
left: topLeft.left,
width: ele.width() + 2,
height: ele.height
}).removeClass('hide');
}
// 验证账户名
function validateAccountLocal() {
var phoneNum = $.trim($phoneNumInput.val()),
regionCode = $regionCodeText.text();
if (phoneNum !== '') {
if (/^[0-9]+$/.test(phoneNum)) {
// 不是11位
if (phoneNum.length !== 11) {
errTip($phone, '手机号码不正确,请重新输入');
return false;
}
// 如果是纯数字,则作为手机号码处理
if (regionCode !== '+86' ||
mailPhoneRegx.phoneRegx[regionCode].test(phoneNum)) {
return true;
} else {
errTip($phone, '手机号码不正确,请重新输入');
return false;
}
} else {
// 邮箱验证
if (mailPhoneRegx.emailRegx.test(phoneNum)) {
return true;
} else {
errTip($phone, '邮箱格式不正确,请重新输入');
return false;
}
}
} else {
errTip($phone, '请输入账号');
return false;
}
}
function validateAccountAsync() {
return $.ajax({
type: 'POST',
url: '/passport/login/user',
data: {
phoneNum: $phoneNumInput.val(),
area: $regionCodeText.text().replace('+', '')
}
}).then(function(data) {
if (data.code && data.code === 200) {
return true;
} else {
errTip($phone, '账号不存在');
return false;
}
});
}
function validateAccount() {
function validate() {
var defer = $.Deferred(); // eslint-disable-line
if (validateAccountLocal()) {
validateAccountAsync().then(function(result) {
if (result) {
defer.resolve(result);
} else {
defer.reject(result);
}
});
} else {
defer.reject(false);
}
return defer.promise();
}
return validate().then(function() {
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;
if ($captchaWrap.hasClass('hide')) {
ep.emit('captcha', true);
return;
}
switch (length) {
case 0:
errTip($imgCaptchaInput, '请输入验证码');
ep.emit('captcha', false);
break;
case 4:
ep.emit('captcha', true);
break;
default:
errTip($imgCaptchaInput, '请输入长度为4字符的验证码');
ep.emit('captcha', false);
break;
}
}
// 密码错误次数,超过三次显示验证码
function showAccountErrTimes() {
$captchaWrap.removeClass('hide');
$captchaImg.attr('src', captchaUrl + $.now());
$imgCaptchaInput.val('');
}
// 登录
function login() {
$.ajax({
url: '/passport/login/auth',
type: 'POST',
data: {
areaCode: $regionCodeText.text().replace('+', ''),
account: $.trim($phoneNumInput.val()),
password: $.trim($passwordInput.val()),
captcha: $.trim($imgCaptchaInput.val()),
isRemember: $remember.hasClass('checked') ? true : false
},
success: function(res) {
if (res.code === 200) {
if (res.data) {
// 防止data.data为undefined时下行语句执行出错而导致脚本不能走到complete去处理authing
location.href = res.data.session;
}
} 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();
}
}
}
});
}
mailAc($phoneNumInput, function() {
function throttle() {
return $.ajax({
url: '/passport/login/account',
type: 'GET',
data: {
account: $.trim($phoneNumInput.val())
}
});
}
validateAccount().then(function(result) {
if (result) {
return throttle();
} else {
return false;
}
}).then(function(res) {
if (!res) {
return;
}
if (res.data && res.data.needCaptcha) {
showAccountErrTimes();
}
});
});
$phoneNumInput.on('focus', function() {
$phone.addClass('focus');
}).on('blur', function() {
$phone.removeClass('focus');
});
$('[placeholder]').placeholder();
// 选择地点
$regionSelectCtrl.change(function() {
var $this = $(this);
$regionCodeText.text($this.val());
});
// 密码
$passwordInput.on('blur', function() {
$passwordInput.removeClass('focus');
validatePasswordLocal();
}).on('focus', function() {
$passwordInput.addClass('focus');
});
// 验证码
$imgCaptchaInput.on('blur', function() {
$imgCaptchaInput.removeClass('focus');
validateCaptchaLocal();
}).on('focus', function() {
$imgCaptchaInput.addClass('focus');
});
// 邮箱自动完成列表项点击
$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.on('phone', function(auth) {
if (auth) {
$errTip.addClass('hide');
}
});
ep.on('password', function(auth) {
if (auth) {
$errTip.addClass('hide');
}
});
ep.on('captcha', function(auth) {
if (auth && !$captchaWrap.hasClass('hide')) {
$errTip.addClass('hide');
}
});
// 登录
$loginBtn.on('click', function() {
if ($loginBtn.hasClass('auth_ok')) {
return;
}
login();
});
// Enter登录
$('input.va').on('keypress', function(e) {
if (e.which === 13) {
login();
}
});
// 首次触发
$imgCaptchaInput.trigger('blur');