third-login.js
7.74 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
/**
* 第三方登录首页
* @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'),
$phoneNumInput = $('#phone-num'),
$imgCaptchaInput = $('#verifyCode'),
$imgCaptchaCtrl = $('.img-captcha-refresh'),
$smsCaptchaInput = $('#sms-captcha-input'),
$smsCaptchaCtrl = $('.sms-captcha-send'),
$phone = $('#phone'),
$nextBtn = $('#validate-phone-next');
var $bindsetpwdForm = $('#bindsetpwd'),
$bindConfirmForm = $('#bindConfirm'),
$relateConfirmForm = $('#relateConfirm');
var $openId = $('#openId');
var $sourceType = $('#sourceType');
var $refer = $('#refer');
var second = 60,
ep = new EventProxy();
var $errTip = $('.tips'),
$errMsg = $errTip.find('.rectangle');
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 disableSMSBtn() {
second -= 1;
if (second < 0) {
second = 60;
$smsCaptchaCtrl.text('获取短信验证码');
$smsCaptchaCtrl.removeClass('disable');
$smsCaptchaCtrl.removeClass('progress');
} else {
$smsCaptchaCtrl.text(second + '秒后可重新操作');
window.setTimeout(disableSMSBtn, 1000);
}
}
function sendSMSCaptcha() {
return $.ajax({
type: 'POST',
url: '/passport/autouserinfo/sendBindMsg',
data: {
mobile: $phoneNumInput.val(),
area: $regionCodeText.text().replace('+', '')
}
});
}
function refreshImgCaptcha() {
var time = new Date(),
$captchaImg = $('.img-captcha'),
captchaImgSrc = $captchaImg.attr('src').split('?')[0];
$captchaImg.attr('src', captchaImgSrc + '?t=' + time.getTime());
}
function validateImgCaptcha() {
return $.ajax({
type: 'POST',
url: '/passport/images/check',
data: {
verifyCode: $imgCaptchaInput.val()
}
});
}
function validateSMSCaptcha() {
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');
}
});
$('#region').change(function() {
var $this = $(this);
$regionCodeText.text($this.val());
});
ep.tail('phoneNum', 'img-captcha', function(phoneAuth, imgAuth) {
if (phoneAuth && imgAuth && !$smsCaptchaCtrl.hasClass('progress')) {
$smsCaptchaCtrl.removeClass('disable');
} else {
$smsCaptchaCtrl.addClass('disable');
}
});
$phoneNumInput.on('blur', function() {
var length = $phoneNumInput.val().length;
if (length === 0) {
errTip($phone, '请输入手机号码');
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($phone, '手机号码不正确,请重新输入');
ep.emit('phoneNum', false);
return;
}
}
ep.emit('phoneNum', true);
return;
} else {
ep.emit('phoneNum', false);
return;
}
});
$imgCaptchaInput.on('blur', function() {
var length = $imgCaptchaInput.val().length;
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;
}
validateImgCaptcha().then(function(result) {
if (result.code === 200) {
ep.emit('img-captcha', true);
} else {
ep.emit('img-captcha', false);
refreshImgCaptcha();
}
});
});
$imgCaptchaCtrl.on('click', function() {
refreshImgCaptcha();
});
$smsCaptchaInput.on('blur', function() {
var length = $smsCaptchaInput.val().length;
switch (length) {
case 4:
break;
case 0:
errTip($smsCaptchaInput, '请输入短信验证码');
ep.emit('sms-captcha', false);
return;
default:
errTip($smsCaptchaInput, '验证码不正确');
ep.emit('sms-captcha', false);
return;
}
validateSMSCaptcha().then(function(result) {
if (result.code === 200) {
ep.emit('sms-captcha', true);
} else {
ep.emit('sms-captcha', false);
errTip($smsCaptchaInput, '验证码不正确');
}
});
});
$smsCaptchaCtrl.on('click', function() {
if ($smsCaptchaCtrl.hasClass('disable')) {
return;
}
$smsCaptchaCtrl.addClass('disable');
$smsCaptchaCtrl.addClass('progress');
disableSMSBtn();
sendSMSCaptcha();
});
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 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:
case 205:
// 已注册绑定过其他的第三方,绑定确定页面
bindConfirmPage(thirdPart, result.data.user);
break;
case 203:
// 关联帐号,关联页面
relateConfirmPage(thirdPart, result.data.user);
break;
default:
// 出错
errTip($nextBtn, '输入错误,请重新输入!');
break;
}
});
}
$nextBtn.on('click', function() {
if ($nextBtn.hasClass('disable')) {
return;
}
nextPage();
});