code.js
5.46 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
/**
* 注册/找回密码-验证码
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2015/10/8
*/
const $ = require('yoho-jquery');
const $jsImgCheck = $('#js-img-check');
const Validate = require('plugin/validata');
const validate = new Validate($jsImgCheck, {
useREM: {
rootFontSize: 40,
picWidth: 150
}
});
if ($jsImgCheck.data('userverify')) {
validate.init();
}
require('common');
module.exports = function(useInRegister, useForBind, useForRelate) {
let $captcha = $('#captcha'),
$btnNext = $('#btn-next'),
$captchaTip = $('#captcha-tip'),
nickname = $('#nickname').val(),
sourceType = $('#sourceType').val(),
openId = $('#openId').val(),
phoneNum = $('#phone-num').val(),
areaCode = $('#area-code').val().replace('+', ''),
inviteCode = $('#invite-code').val();
let api = require('./api');
let tip = require('plugin/tip');
let trim = $.trim;
let showErrTip = tip.show;
let urlMid = useInRegister ? 'reg' : 'back';
let isReg = parseInt($('#isReg').val(), 10);
function startBind() {
$.ajax({
url: useForBind ? '/passport/bind/bindMobile' : '/passport/bind/relateMobile',
type: 'post',
data: {
areaCode: areaCode.replace('+', ''),
phoneNum: phoneNum,
openId: openId,
sourceType: sourceType,
nickname: nickname,
password: '',
code: trim($captcha.val())
},
success: function(res) {
if (res.code === 200) {
tip.show('登录成功');
setTimeout(function() {
location.href = res.data.refer;
}, 2000);
} else {
tip.show(res.message);
}
},
error: function() {
tip.show('登录失败,请重试!');
}
});
}
function startReg() {
$.ajax({
type: 'POST',
url: '/passport/' + urlMid + '/verifycode',
data: {
phoneNum: phoneNum,
areaCode: areaCode,
code: trim($captcha.val()),
token: $('#token').val(),
inviteCode: inviteCode
},
success: function(data) {
if (data.code === 200) {
location.href = data.data;
} else {
// 验证码不正确,显示提示
showErrTip(data.message);
if (data.refer) {
location.href = data.refer;
}
}
}
});
}
function countDown(during) {
let count = during || 59,
itime;
itime = setInterval(function() {
if (count === 0) {
$captchaTip.text('重新获取').removeClass('disable');
clearInterval(itime);
} else {
$captchaTip.text('重新获取 (' + count-- + '秒)');
window.setCookie('count', count);
if (during && parseInt(during, 10) !== 0) {
$captchaTip.addClass('disable');
}
}
}, 1000);
}
api.bindClearEvt();
$captcha.bind('input', function() {
if (trim($captcha.val()) !== '') {
$btnNext.removeClass('disable');
} else {
$btnNext.addClass('disable');
}
});
// 重新获取验证码
$captchaTip.on('touchstart', function() {
if ($captchaTip.hasClass('disable')) {
return;
}
validate.getResults().then(result => {
let paramsData = {
phoneNum: phoneNum,
areaCode: areaCode,
};
$.extend(paramsData, result);
$.ajax({
type: 'POST',
url: (useForBind || useForRelate) ? '/passport/bind/sendBindMsg' :
'/passport/' + urlMid + '/sendcodeagain',
data: paramsData,
success: function(data) {
validate.type === 2 && validate.refresh();
if (data.code === 200) {
$captchaTip.text('重发验证码 (60秒)').addClass('disable');
countDown();
} else {
data.during && countDown(data.during);
// 验证码不正确,显示提示
showErrTip(data.message);
}
},
error: function() {
validate.refresh();
}
});
});
});
$btnNext.on('touchstart', function() {
if ($btnNext.hasClass('disable')) {
return;
}
if (useForBind || useForRelate) {
if (isReg) {
startBind();
} else {
location.href = '/passport/bind/password?phoneNum=' +
phoneNum + '&areaCode=' + areaCode + '&openId=' +
openId + '&sourceType=' + sourceType + '&nickname=' + nickname + '&code=' + trim($captcha.val());
}
} else {
startReg();
}
});
if (window.cookie('count') && parseInt(window.cookie('count'), 10) > 0) {
countDown(window.cookie('count'));
} else {
countDown();
}
};