bindMobile.js
6.33 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
import wx from '../../utils/wx';
import event from '../../common/event';
import Yas from '../../common/yas';
import {getVerifyCode, bindMobileAction} from '../../common/login';
let app = getApp();
let yas = new Yas();
let router = global.router;
Page({
data: {
areaCode: '86',
phoneNum: '',
smsCode: '',
btnText: '获取验证码',
timerSec: 60,
counting: false,
regState: 0,
activeClass: '',
completeClass: '',
areaName: '中国',
hasUnionID: false,
isSubmitting: false,
autoBtnText: '自动验证'
},
onLoad: function(query) {
const {phone, area} = query || {};
if (+area === 86 && phone) {
this.setData({
phoneNum: phone,
activeClass: 'active'
});
}
event.on('choose-area', params => {
this.setData({
areaCode: params.code,
areaName: params.name
});
});
event.on('register-type-report', params => {
yas.report('YB_REGISTER_SUCCESS', params);
});
this.setData({
hasUnionID: !!app.globalData.unionID
});
yas.pageOpenReport();
},
phoneInput: function(e) {
this.setData({
phoneNum: e.detail.value.trim(),
activeClass: e.detail.value.trim() ? 'active' : '',
completeClass: (this.data.smsCode && e.detail.value.trim()) ? 'active' : ''
});
},
codeInput: function(e) {
this.setData({
smsCode: e.detail.value.trim(),
completeClass: (this.data.phoneNum && e.detail.value.trim()) ? 'active' : ''
});
},
sendCode: function() {
if (!this.data.phoneNum || this.data.counting) {
return;
}
if (this.checkPhoneFormat()) {
return getVerifyCode(this.data.areaCode, this.data.phoneNum).then(res => {
if (res.code === 200) {
this.countDown();
} else {
wx.showModal({
title: '提示',
content: res.message || '验证码发送失败',
showCancel: false
});
}
})
.catch(() => {
wx.showModal({
title: '提示',
content: '验证码发送失败',
showCancel: false
});
});
}
wx.showModal({
title: '提示',
content: '手机号码格式不正确',
showCancel: false
});
},
// 倒计时
countDown: function() {
let seconds = 60;
let timer = setInterval(() => {
seconds--;
if (seconds < 0) {
this.setData({
btnText: '重新获取验证码',
timerSec: 60,
counting: false,
});
clearInterval(timer);
} else {
this.setData({
btnText: `${seconds}s`,
timerSec: seconds,
counting: true,
});
}
}, 1000);
this.setData({
btnText: '60s',
timerSec: 60,
counting: true,
});
},
submitTap: function() {
if (!this.data.completeClass) {
return;
}
if (!this.data.smsCode || !this.data.phoneNum) {
return wx.showModal({
title: '提示',
content: '手机号码和验证码都不能为空',
showCancel: false
});
}
if (!this.checkPhoneFormat()) {
return wx.showModal({
title: '提示',
content: '手机号码格式不正确',
showCancel: false
});
}
wx.showToast({
title: '加载中',
icon: 'loading',
duration: 10000
});
if (this.data.isSubmitting) {
return;
}
this.data.isSubmitting = true;
bindMobileAction(this.data.areaCode, this.data.phoneNum, this.data.smsCode)
.then((res => {
wx.hideToast();
this.data.isSubmitting = false;
if (res.code === 200) {
let userInfo = {};
userInfo.uid = res.data.uid;
userInfo.is_bind = res.data.is_bind || '';
userInfo.mobile = res.data.profile;
userInfo.ssouid = res.data.ssouid;
userInfo.sessionKey = res.data.session_key;
app.setUserInfo(userInfo);
app.setSessionKey(userInfo.sessionKey);
event.emit('user-login-success');
if (res.data && res.data.is_register === 0) {
event.emit('register-type-report', {YB_REGISTER_SUCCESS: 5});
}
wx.navigateBack();
} else {
wx.showModal({
title: '提示',
content: res.message || '服务器错误',
showCancel: false
});
}
}))
.catch(() => {
this.data.isSubmitting = false;
wx.showModal({
title: '提示',
content: '服务器错误',
showCancel: false
});
wx.hideToast();
});
},
checkPhoneFormat: function() {
// 手机号码验证规则
let phoneRegx = {
'+86': /^1[35847]{1}[0-9]{9}$/,
'+852': /^[965]{1}[0-9]{7}$/,
'+853': /^[0-9]{8}$/,
'+886': /^[0-9]{10}$/,
'+65': /^[98]{1}[0-9]{7}$/,
'+60': /^1[1234679]{1}[0-9]{8}$/,
'+1': /^[0-9]{10}$/,
'+82': /^01[0-9]{9}$/,
'+44': /^7[789][0-9]{8}$/,
'+81': /^0[9|8|7][0-9]{9}$/,
'+61': /^[0-9]{11}$/
};
return phoneRegx[`+${this.data.areaCode}`].test(this.data.phoneNum);
},
chooseArea: function() {
router.go('chooseArea');
}
});