_email-bind-mobile.js
8.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
import $ from 'yoho-jquery';
import tip from 'plugin/tip';
import Page from 'yoho-page';
import api from '../api';
import Validate from 'plugin/validata';
import dialog from 'plugin/dialog';
import bindDialogHbs from 'passport/bind-dialog-tip.hbs';
const $captcha = $('#js-img-check');
const validate = new Validate($captcha, {
useREM: {
rootFontSize: 40,
picWidth: 150
}
});
const showErrTip = tip.show;
class EmailBindMobile extends Page {
constructor() {
super();
this.selector = {
itime: 0,
countrySelect: $('.mobile-form-login select.country-select'),
mobileInput: $('.mobile-form-login input.mobile-input'),
clearMobile: $('.mobile-form-login .clear'),
verifyCodeInput: $('.mobile-form-login input.verify-code-input'),
verifyCodeBtn: $('.mobile-form-login .get-verify-code'),
loginBtn: $('.mobile-form-login .sms-login-btn'),
};
this.init();
}
init() {
if ($captcha.data('userverify')) {
validate.init();
}
this.bindEvents();
}
bindEvents() {
this.selector.countrySelect.on('change', this.changeBtnStatus.bind(this));
this.selector.mobileInput.on('input', this.changeBtnStatus.bind(this));
this.selector.clearMobile.on('click', this.clearMobile.bind(this));
this.selector.verifyCodeInput.on('input', this.changeBtnStatus.bind(this, true));
this.selector.verifyCodeBtn.on('click', this.getVerifyCode.bind(this));
this.selector.loginBtn.on('click', this.login.bind(this));
}
/**
* 清除输入的手机号
*/
clearMobile() {
this.selector.mobileInput.val('');
this.selector.verifyCodeInput.val('');
this.selector.clearMobile.addClass('hide');
this.selector.countrySelect.trigger('change');
}
/**
* 输入监听,改变按钮状态
*/
changeBtnStatus(isVerifycode) {
let areaCode = $.trim(this.selector.countrySelect.val());
let phoneNum = $.trim(this.selector.mobileInput.val());
let verifyCode = $.trim(this.selector.verifyCodeInput.val());
if (!api.phoneRegx[areaCode]) {
return showErrTip('出错了,请重新刷新页面');
}
if (phoneNum) {
this.selector.clearMobile.removeClass('hide');
}
// 验证码输入框单独处理
if (isVerifycode === true) {
// 提交表单按钮激活
if (verifyCode && api.phoneRegx[areaCode].test(phoneNum)) {
this.selector.loginBtn.addClass('active');
} else {
this.selector.loginBtn.removeClass('active');
}
return;
}
clearInterval(this.selector.itime);
this.selector.verifyCodeBtn.text('获取验证码');
if (api.phoneRegx[areaCode].test(phoneNum)) {
this.selector.verifyCodeBtn.addClass('active');
if (verifyCode) {
this.selector.loginBtn.addClass('active');
}
} else {
this.selector.verifyCodeBtn.removeClass('active');
this.selector.loginBtn.removeClass('active');
}
}
/**
* 获取验证码倒计时
*/
countDown(during) {
let count = parseInt(during, 10) || 59;
if (!this.selector.verifyCodeBtn.hasClass('active')) {
return;
}
this.selector.verifyCodeBtn.removeClass('active');
this.selector.itime = setInterval(() => {
window.setCookie('intTimer', count, {path: '/' });
if (count <= 0) {
this.selector.verifyCodeBtn.text('重新获取').addClass('active');
clearInterval(this.selector.itime);
} else {
this.selector.verifyCodeBtn.text('重新获取 (' + count-- + '秒)');
}
}, 1000);
}
/**
* 获取验证码
*/
getVerifyCode() {
let pn = $.trim(this.selector.mobileInput.val());
let area = $.trim(this.selector.countrySelect.val());
if (!this.selector.verifyCodeBtn.hasClass('active') ||
this.selector.verifyCodeBtn.data('oneClick')) {
return;
}
if (!api.phoneRegx[area]) {
return showErrTip('出错了,请重新刷新页面');
} else if (!api.phoneRegx[area].test(pn)) {
return showErrTip('手机号格式不正确,请重新输入');
}
this.selector.verifyCodeBtn.data('oneClick', true);
validate.getResults().then(result => {
let params = {
area: area.replace('+', ''),
mobile: pn
};
$.extend(params, result);
this.ajax({
url: '/passport/bind/forceSendMsg',
type: 'POST',
data: params
}).then(codeResult => {
this.selector.verifyCodeBtn.data('oneClick', false);
validate.type === 2 && validate.refresh();
if (codeResult.code === 200) {
this.countDown();
return;
} else {
showErrTip(codeResult.message);
}
(codeResult.changeCaptcha && validate.type !== 2) && validate.refresh();
}).catch(() => {
this.selector.verifyCodeBtn.data('oneClick', false);
showErrTip('出错了,请重试');
validate.refresh();
});
});
}
login() {
let pn = $.trim(this.selector.mobileInput.val());
let area = $.trim(this.selector.countrySelect.val());
let verifyCode = $.trim(this.selector.verifyCodeInput.val());
if (!this.selector.loginBtn.hasClass('active')) {
return;
}
if (!api.phoneRegx[area]) {
return showErrTip('出错了,请重新刷新页面');
} else if (!api.phoneRegx[area].test(pn)) {
return showErrTip('手机号格式不正确,请重新输入');
}
this.selector.loginBtn.data('oneClick', true);
let params = {
area: area.replace('+', ''),
mobile: pn,
code: verifyCode,
};
this.ajax({
url: '/passport/bind/forceMobileCheck',
type: 'POST',
data: params
}).then(codeResult => {
this.selector.loginBtn.data('oneClick', false);
if (codeResult.code === 200) {
this.continueBind(
$.extend({isBind: 'N', isRegister: 'N'}, codeResult.data),
params
);
} else {
showErrTip(codeResult.message);
}
}).catch(() => {
this.selector.loginBtn.data('oneClick', false);
showErrTip('出错了,请重试');
});
}
continueBind(codeResult, params) {
let that = this;
let goUrl = '/passport/bind/success';
let bindParams = {
isBind: codeResult.isBind === 'Y',
isRegister: codeResult.isRegister === 'Y'
};
// 如果没有绑定没有注册,直接注册
if (!(bindParams.isBind || bindParams.isRegister)) {
document.location.href = goUrl;
return;
}
dialog.showDialog({
dialogText: bindDialogHbs(bindParams),
fast: true,
hasFooter: {
leftBtnText: '去登录',
rightBtnText: '继续'
}
}, function() {
return that.ajax({
url: '/passport/bind/continueMobile',
type: 'POST',
data: params
}).then(cresult => {
if (cresult.code === 200) {
document.location.href = goUrl;
return;
} else {
showErrTip(cresult.message);
}
}).catch(() => {
showErrTip('出错了,请重试');
});
}, function() {
document.location.href = '//m.yohobuy.com/signin.html';
});
$('.dialog-wrapper').addClass('s-dialog-bind');
}
}
module.exports = EmailBindMobile;