register-new.js
8.58 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
const $ = require('yoho-jquery');
const $captcha = $('#js-img-check');
const tip = require('plugin/tip');
const showErrTip = tip.show;
const api = require('../api');
const validatePWD = require('../password-check');
const Validate = require('plugin/validata');
const validate = new Validate($captcha, {
useREM: {
rootFontSize: 40,
picWidth: 150
}
});
class RegisterNew {
constructor() {
this.view = {
clearMobile: $('#clearMobile'),
countryCodeBtn: $('#countryCodeBtn'),
countryCodeSelector: $('#countryCodeSelector'),
getVerifyCodeBtn: $('#getVerifyCodeBtn'),
regBtn: $('#regBtn'),
mobileInput: $('input[name=mobile]'),
verifyCodeInput: $('input[name=verifyCode]'),
inviteCodeInput: $('input[name=inviteCode]'),
passwordInput: $('input[name=password]'),
passwordEyeIcon: $('#passwordEyeIcon'),
eyeClose: $('.eye-close'),
eyeOpen: $('.eye-open'),
tokenInput: $('input[name=token]')
};
this.requested = false;
this.view.clearMobile.on('click', this.clearMobile.bind(this));
this.view.regBtn.on('click', this.register.bind(this));
this.view.mobileInput.bind('input', this.changeRegBtnStatus.bind(this));
this.view.inviteCodeInput.bind('input', this.changeRegBtnStatus.bind(this));
this.view.passwordInput.bind('input', this.changeRegBtnStatus.bind(this));
this.view.passwordEyeIcon.on('click', this.passwordShowStatus.bind(this));
if ($captcha.data('geetest')) {
this.view.getVerifyCodeBtn.on('click', this.getVerifyCode.bind(this));
} else {
validate.init();
this.view.getVerifyCodeBtn.on('click', this.imgVerifyInit.bind(this));
}
}
/**
* 图片验证码初始化
*/
imgVerifyInit() {
if (!this.view.getVerifyCodeBtn.hasClass('active')) {
return;
}
validate.refresh();
$(document).off('click.refresh').on('click.refresh', '.img-check-refresh', this.imgVerifyInit.bind(this));
if (!$('#getVerifyCodeBtnByValidate').length) {
$captcha.append('<div class="validate-sub-btn"><button id="getVerifyCodeBtnByValidate">获取验证码</button></div>'); // eslint-disable-line
}
this.view.getVerifyCodeBtnByValidate = $('#getVerifyCodeBtnByValidate');
this.view.getVerifyCodeBtnByValidate.on('click', this.getVerifyCode.bind(this));
$captcha.removeClass('hide');
}
/**
* 清除输入的手机号
*/
clearMobile() {
this.view.mobileInput.val('');
}
/**
* 隐藏显示密码
*/
passwordShowStatus() {
if (this.view.eyeOpen.hasClass('hide')) {
this.view.passwordInput.attr('type', 'text');
this.view.eyeClose.addClass('hide');
this.view.eyeOpen.removeClass('hide');
} else {
this.view.passwordInput.attr('type', 'password');
this.view.eyeOpen.addClass('hide');
this.view.eyeClose.removeClass('hide');
}
}
/**
* 获取验证码倒计时
*/
countDown(during) {
let count = during || 59;
let itime;
this.view.getVerifyCodeBtn.removeClass('active');
itime = setInterval(() => {
if (count === 0) {
this.view.getVerifyCodeBtn.text('重新发送').addClass('active');
clearInterval(itime);
} else {
this.view.getVerifyCodeBtn.text('重新发送 (' + count-- + '秒)');
window.setCookie('count', count);
if (during && parseInt(during, 10) !== 0) {
this.view.getVerifyCodeBtn.removeClass('active');
}
}
}, 1000);
}
/**
* 改变登录按钮状态
*/
changeRegBtnStatus() {
if (this.view.mobileInput.val() &&
this.view.passwordInput.val() &&
this.view.verifyCodeInput.val()) {
this.view.regBtn.addClass('active');
} else {
this.view.regBtn.removeClass('active');
}
}
/**
* 注册动作处理
*/
register() {
let password = this.view.passwordInput.val();
if (!this.view.regBtn.hasClass('active')) {
return;
}
if (!validatePWD(password)) {
showErrTip('密码6-20位,请重新输入');
return;
}
this.postRegister();
}
/**
* 提交注册请求
*/
postRegister() {
if (this.view.regBtn.hasClass('active')) {
this.view.regBtn.removeClass('active');
let postData = {
password: $.trim(this.view.passwordInput.val()),
phoneNum: this.view.mobileInput.val(),
areaCode: this.view.countryCodeSelector.val(),
smsCode: this.view.verifyCodeInput.val(),
token: this.view.tokenInput.val(),
inviteCode: this.view.inviteCodeInput.val()
};
return $.ajax({
type: 'POST',
url: '/passport/register-new',
data: postData,
success: (data) => {
let res = data.data;
if (data.code === 200) {
showErrTip('注册成功');
window.setCookie('refer', res.session);
window.setCookie('msgDelivery', res.msgDelivery, {expires: 1, path: '/' });
// 统计代码:用于统计从哪个渠道注册成功的
if (window._yas && window._yas.sendCustomInfo) {
window._yas.sendCustomInfo({
op: 'YB_REGISTER_SUCCESS_L',
ud: window.getUid(),
param: JSON.stringify({
C_ID: window._ChannelVary[window.cookie('_Channel')] || 1,
UNION_TYPE: window.queryString.union_type || window.cookie('unionTypeYas') || false
})
}, true);
}
setTimeout(function() {
location.href = res.href;
}, 1500);
} else {
this.view.regBtn.addClass('active');
showErrTip(data.message);
}
},
error: (data) => {
this.view.regBtn.addClass('active');
if (data && data.responseJSON && data.responseJSON.message) {
showErrTip(data.message);
}
}
});
}
}
/**
* 获取验证码
*/
getVerifyCode() {
if (!this.view.getVerifyCodeBtn.hasClass('active')) {
return;
}
if (this.requested) {
return false;
}
let areaCode = this.view.countryCodeSelector.val();
let phoneNum = this.view.mobileInput.val();
let params = {
areaCode: areaCode.replace('+', ''),
phoneNum: this.view.mobileInput.val(),
inviteCode: this.view.inviteCodeInput.val()
};
if (api.phoneRegx[areaCode].test(phoneNum) || areaCode !== '+86') {
validate.getResults().then(result => {
this.requested = true;
$.extend(params, result);
$.ajax({
url: '/passport/reg/verifymobile',
type: 'POST',
data: params,
success: postResult => {
validate.type === 2 && validate.refresh();
$captcha.addClass('hide');
if (postResult.code === 200) {
this.view.tokenInput.val(postResult.data.token);
this.countDown();
} else {
(postResult.changeCaptcha && validate.type !== 2) && validate.refresh();
showErrTip(postResult.message);
this.requested = false;
}
},
error: () => {
showErrTip('出错了,请重试');
validate.refresh();
this.requested = false;
}
});
});
}
}
}
module.exports = RegisterNew;