third-login.js
10.2 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
/**
* 第三方登录首页
* @author: TaoHuang
* @date: 2016/7/12
*/
var $ = require('yoho-jquery');
var phoneRegx = require('../common/mail-phone-regx').phoneRegx;
var EventProxy = require('yoho-eventproxy');
var $regionCodeText = $('#region-code'),
$regionSelectList = $('#country-select-list'),
$regionSelectHeader = $('#country-select-header'),
$phoneNumInput = $('#phone-num'),
$imgCaptchaInput = $('#verifyCode'),
$imgCaptchaCtrl = $('.img-captcha-refresh'),
$smsCaptchaInput = $('#sms-captcha-input'),
$smsCaptchaCtrl = $('.sms-captcha-send'),
$nextBtn = $('#validate-phone-next');
var $bindsetpwdForm = $('#bindsetpwd'),
$bindConfirmForm = $('#bindConfirm'),
$relateConfirmForm = $('#relateConfirm'),
$bindedForm = $('#bindedPage');
var $openId = $('#openId');
var $sourceType = $('#sourceType');
var $refer = $('#refer');
var second = 60,
ep = new EventProxy();
var upDown = {
up: '',
down: ''
};
var selectedIcon = '';
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');
}
// 短信验证码60s时间
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);
}
}
// 刷新图形验证码
function refreshImgCaptcha() {
var time = new Date(),
$captchaImg = $('.img-captcha'),
captchaImgSrc = $captchaImg.attr('src').split('?')[0];
$captchaImg.attr('src', captchaImgSrc + '?t=' + time.getTime());
}
// 发送短信验证码
function sendSMSCaptcha() {
return $.ajax({
type: 'POST',
url: '/passport/autouserinfo/sendBindMsg',
data: {
mobile: $phoneNumInput.val(),
area: $regionCodeText.text().replace('+', '')
}
}).then(function(ret) {
if (ret && ret.code === 400) {
errTip($imgCaptchaInput, ret.message);
refreshImgCaptcha();
ep.emit('img-captcha', false);
}
});
}
// 异步验证图形码
function validateImgCaptchaAsync() {
return $.ajax({
type: 'POST',
url: '/passport/images/check',
data: {
verifyCode: $imgCaptchaInput.val()
}
});
}
// 异步验证短信码
function validateSMSCaptchaAsync() {
return $.ajax({
type: 'POST',
url: '/passport/autouserinfo/checkBindMsg',
data: {
code: $smsCaptchaInput.val(),
mobile: $phoneNumInput.val(),
area: $regionCodeText.text().replace('+', '')
}
});
}
// 同时监听三个事件
ep.tail('phoneNum', 'img-captcha', 'sms-captcha', function(phoneAuth, imgAuth, smsAuth) {
if (phoneAuth && imgAuth && smsAuth) {
$nextBtn.removeClass('disable');
} else {
$nextBtn.addClass('disable');
}
});
// 选择国家,变换图标
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();
});
// 确定事件都验证通过
ep.tail('phoneNum', 'img-captcha', function(phoneAuth, imgAuth) {
if (phoneAuth && imgAuth && !$smsCaptchaCtrl.hasClass('second-progress')) {
$smsCaptchaCtrl.removeClass('disable');
} else {
$smsCaptchaCtrl.addClass('disable');
}
});
$phoneNumInput.on('blur', function() {
var length = $phoneNumInput.val().length;
$('#phone').removeClass('focus');
if (length === 0) {
errTip($phoneNumInput, '请输入手机号码');
ep.emit('phoneNum', false);
return;
}
if (/^[0-9]+$/.test($phoneNumInput.val())) {
// 这里只做中国区验证
if ($regionCodeText.text() === '+86') {
if (length === 11 && phoneRegx['+86'].test($phoneNumInput.val())) {
ep.emit('phoneNum', true);
return;
} else {
errTip($phoneNumInput, '手机号码不正确,请重新输入');
ep.emit('phoneNum', false);
return;
}
}
ep.emit('phoneNum', true);
return;
} else {
ep.emit('phoneNum', false);
return;
}
}).on('focus', function() {
hideTip($phoneNumInput);
$('#phone').addClass('focus');
});
$imgCaptchaInput.on('blur', function() {
var length = $imgCaptchaInput.val().length;
$imgCaptchaInput.removeClass('focus');
switch (length) {
case 4 :
break;
case 0:
errTip($imgCaptchaInput, '请输入验证码');
refreshImgCaptcha();
ep.emit('img-captcha', false);
break;
default:
errTip($imgCaptchaInput, '验证码不正确');
refreshImgCaptcha();
ep.emit('img-captcha', false);
break;
}
validateImgCaptchaAsync().then(function(result) {
if (result.code === 200) {
ep.emit('img-captcha', true);
} else {
ep.emit('img-captcha', false);
refreshImgCaptcha();
}
});
}).on('focus', function() {
hideTip($imgCaptchaInput);
$imgCaptchaInput.addClass('focus');
});
$imgCaptchaCtrl.on('click', function() {
refreshImgCaptcha();
});
$smsCaptchaInput.on('blur', function() {
var length = $smsCaptchaInput.val().length;
$smsCaptchaInput.removeClass('focus');
switch (length) {
case 4:
break;
case 0:
errTip($smsCaptchaInput, '请输入短信验证码');
ep.emit('sms-captcha', false);
return;
default:
errTip($smsCaptchaInput, '验证码不正确');
ep.emit('sms-captcha', false);
return;
}
validateSMSCaptchaAsync().then(function(result) {
if (result.code === 200) {
ep.emit('sms-captcha', true);
} else {
ep.emit('sms-captcha', false);
errTip($smsCaptchaInput, '验证码不正确');
}
});
}).on('focus', function() {
hideTip($smsCaptchaInput);
$smsCaptchaInput.addClass('focus');
});
$smsCaptchaCtrl.on('click', function() {
if ($smsCaptchaCtrl.hasClass('disable') || $smsCaptchaCtrl.hasClass('second-progress')) {
return;
}
validateImgCaptchaAsync().then(function(result) {
if (result.code === 200) {
$smsCaptchaCtrl.addClass('disable');
disableSMSBtn();
sendSMSCaptcha();
} else {
ep.emit('img-captcha', false);
errTip($imgCaptchaInput, '图形验证码错误');
refreshImgCaptcha();
}
});
});
// 统一设置用户信息
function setUserInfo(user) {
$('.username').val(user.username);
$('.headImg').val(user.headImg);
}
// 统一设置第三方的信息
function setThirdPartInfo(third) {
$('.openId').val(third.openId);
$('.sourceType').val(third.sourceType);
$('.refer').val(third.refer);
$('.mobile').val(third.mobile);
$('.area').val(third.area);
$('.verifyCode').val(third.verifyCode);
$('.code').val(third.code);
}
// 发送数据到设置密码页面
function setPwdPage(thirdPart) {
setThirdPartInfo(thirdPart);
$bindsetpwdForm.submit();
}
// 发送数据到绑定确认页面
function bindConfirmPage(thirdPart, user) {
setThirdPartInfo(thirdPart);
setUserInfo(user);
$bindConfirmForm.submit();
}
// 发送数据到关联页面
function relateConfirmPage(thirdPart, user) {
setThirdPartInfo(thirdPart);
setUserInfo(user);
$relateConfirmForm.submit();
}
// 发送数据到已绑定页面
function bindedPage(thirdPart, user) {
setThirdPartInfo(thirdPart);
setUserInfo(user);
$bindedForm.submit();
}
// 下一步
function nextPage() {
var thirdPart = {
mobile: $phoneNumInput.val(),
area: $regionCodeText.text().replace('+', ''),
openId: $openId.val(),
sourceType: $sourceType.val(),
verifyCode: $imgCaptchaInput.val(),
code: $smsCaptchaInput.val(),
refer: $refer.val()
};
return $.ajax({
type: 'post',
url: '/passport/autouserinfo/bindCheck',
data: {
mobile: thirdPart.mobile,
area: thirdPart.area,
openId: thirdPart.openId,
sourceType: thirdPart.sourceType
}
}).then(function(result) {
switch (result.code) {
case 200:
// 未注册,可绑定,设定密码页面;
setPwdPage(thirdPart);
break;
case 201:
// 已注册绑定过其他的第三方,绑定确定页面
bindConfirmPage(thirdPart, result.data.user);
break;
case 203:
// 关联帐号,关联页面
relateConfirmPage(thirdPart, result.data.user);
break;
case 205:
// 已经绑定过同类型第三方的页面
bindedPage(thirdPart, result.data.user);
break;
default:
// 出错
errTip($nextBtn, '输入错误,请重新输入!');
break;
}
});
}
$nextBtn.on('click', function() {
if ($nextBtn.hasClass('disable')) {
return;
}
nextPage();
});