sms-login-new.js
5.66 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
import $ from 'yoho-jquery';
import tip from 'plugin/tip';
import Modal2 from 'plugin/modal2';
import checkPoint from './check-point';
import Page from 'yoho-page';
import Validate from 'plugin/validata';
const $captcha = $('#js-img-check');
const validate = new Validate($captcha, {
useREM: {
rootFontSize: 40,
picWidth: 150
}
});
class SmsLoginNew extends Page {
constructor() {
super();
this.selector = {
clearMobile: $('#clearMobile'),
countryCodeSelector: $('#countryCodeSelector'),
mobileInput: $('input[name=mobile]'),
getVerifyCodeBtn: $('#getVerifyCodeBtn'),
verifyCode: $('input[name=verifyCode]'),
smsLoginBtn: $('#smsLoginBtn'),
getPswrdBtn: $('#getPswrdBtn'),
getPasswordBox: $('.get-password-box'),
showYohoFamilyTip: $('#showYohoFamilyTip')
};
this.init();
}
/**
* 初始化
*/
init() {
validate.init();
this.bindEvents();
// 多次登录失败跳短信认证填充手机号
this.selector.mobileInput.val(localStorage.loginJumpUrl);
localStorage.removeItem('loginJumpUrl');
}
/**
* 事件绑定
*/
bindEvents() {
this.selector.clearMobile.on('click', this.clearMobile.bind(this));
this.selector.mobileInput.bind('input', this.changeBtnStatus.bind(this));
this.selector.verifyCode.bind('input', this.changeBtnStatus.bind(this));
this.selector.smsLoginBtn.on('click', this.login.bind(this));
this.selector.getPswrdBtn.on('click', this.showGetPasswordBox.bind(this));
this.selector.getPasswordBox.on('click', this.hiddenGetPasswordBox.bind(this));
this.selector.showYohoFamilyTip.on('click', this.showYohoFamilyTip.bind(this));
this.selector.getVerifyCodeBtn.on('click', this.getVerifyCode.bind(this));
}
/**
* 展示弹窗
*/
showYohoFamilyTip() {
Modal2.alert('Yoho!Family账号可登录Yoho!Buy有货、Yoho!Now、Mars及SHOW', 'Yoho!Family');
}
/**
* 获取验证码倒计时
*/
countDown(during) {
let count = during || 59;
let itime;
this.selector.getVerifyCodeBtn.removeClass('active');
itime = setInterval(() => {
if (count === 0) {
this.selector.getVerifyCodeBtn.text('重新发送').addClass('active');
clearInterval(itime);
} else {
this.selector.getVerifyCodeBtn.text('重新发送 (' + count-- + '秒)');
window.setCookie('count', count);
if (during && parseInt(during, 10) !== 0) {
this.selector.getVerifyCodeBtn.removeClass('active');
}
}
}, 1000);
}
/**
* 输入监听,改变按钮状态
*/
changeBtnStatus() {
// 获取验证码按钮
if (this.selector.mobileInput.val()) {
this.selector.getVerifyCodeBtn.addClass('active');
} else {
this.selector.getVerifyCodeBtn.removeClass('active');
}
// 登录按钮
if (this.selector.mobileInput.val() && this.selector.verifyCode.val()) {
this.selector.smsLoginBtn.addClass('active');
} else {
this.selector.smsLoginBtn.removeClass('active');
}
}
/**
* 登录
*/
login() {
if (!this.selector.smsLoginBtn.hasClass('active')) {
return;
}
let code = this.selector.verifyCode.val();
this.ajax({
url: '/passport/sms_login/check.json',
data: {
code: code
}
}).then(res => {
if (res.code === 200) {
checkPoint('YB_MOBILE_LOGIN_C'); // 埋点
if (res.newer) {
res.redirect = res.redirect + '®isterCode=' + res.registerCode;
}
location.href = res.redirect;
return;
}
tip.show(res.message);
}).catch(() => {
tip.show('出错了!');
});
}
/**
* 获取验证码
*/
getVerifyCode() {
if (!this.selector.getVerifyCodeBtn.hasClass('active')) {
return;
}
let areaCode = this.selector.countryCodeSelector.val();
let phone = $.trim(this.selector.mobileInput.val());
validate.getResults().then(result => {
let params = {
area: areaCode.replace('+', ''),
mobile: phone
};
$.extend(params, result);
this.ajax({
method: 'POST',
url: '/passport/sms_login/step1_check',
data: params
}).then(data => {
validate.type === 2 && validate.refresh();
if (data.code === 200) {
checkPoint('YB_MOBILE_NEXT_C'); // 埋点
this.countDown();
} else {
(data.changeCaptcha && validate.type !== 2) && validate.refresh();
tip.show(data.message);
}
}).catch(() => {
validate.refresh();
});
});
}
/**
* 清除输入的手机号
*/
clearMobile() {
this.selector.mobileInput.val('');
}
/**
* 显示找回密码遮罩
*/
showGetPasswordBox() {
this.selector.getPasswordBox.removeClass('hide');
}
/**
* 隐藏找回密码遮罩
*/
hiddenGetPasswordBox() {
this.selector.getPasswordBox.addClass('hide');
return false;
}
}
module.exports = SmsLoginNew;