register-new.js
7.78 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
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]')
};
validate.init();
this.view.clearMobile.on('click', this.clearMobile.bind(this));
this.view.regBtn.on('click', this.register.bind(this));
this.view.mobileInput.bind('input', this.changeBtnStatus.bind(this));
this.view.inviteCodeInput.bind('input', this.changeBtnStatus.bind(this));
this.view.passwordInput.bind('input', this.changeBtnStatus.bind(this));
this.view.passwordEyeIcon.on('click', this.passwordShowStatus.bind(this));
this.view.getVerifyCodeBtn.on('click', this.getVerifyCode.bind(this));
}
/**
* 清除输入的手机号
*/
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);
}
/**
* 输入监听,改变按钮状态
*/
changeBtnStatus() {
// 获取验证码按钮
if (this.view.mobileInput.val()) {
this.view.getVerifyCodeBtn.addClass('active');
} else {
this.view.getVerifyCodeBtn.removeClass('active');
}
// 登录按钮
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;
}
let validateResult = validatePWD(password, result => {
if (!result.valid) {
showErrTip(result.msg);
}
return result.valid;
});
if (!validateResult) {
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;
}
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 => {
$.extend(params, result);
$.ajax({
url: '/passport/reg/verifymobile',
type: 'POST',
data: params,
success: postResult => {
validate.type === 2 && validate.refresh();
if (postResult.code === 200) {
this.view.tokenInput.val(postResult.data.token);
this.countDown();
} else {
(postResult.changeCaptcha && validate.type !== 2) && validate.refresh();
showErrTip(postResult.message);
}
},
error: () => {
showErrTip('出错了,请重试');
validate.refresh();
}
});
});
}
}
}
module.exports = RegisterNew;