codeLogin.js
10.7 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// pages/login/codeLogin/codeLogin.js
// var app = getApp();
var api = 'https://proxy.shunzhi.net/';
var api_card = 'https://proxy.shunzhi.net/card/';
var appid = 'wx186236fdcd93edcc';
var util = require('../../../utils/md5.js')
import request from '../../../api/request.js'
Page({
/**
* 页面的初始数据
*/
data: {
pageShow: false, //是否显示登录页面内容
codeCont: '获取验证码',
code: '',
loginCode: '', //wx.login获取到的code
userName: '',
session_key: '',
encryptedData: '',
iv: '',
canClick: true, // 节流
clockTimer: null, // 定时器
path: '', //登录后跳转页面
modelShow: false, //完善信息
phone: '', //getUserInfo获取到的手机号
openId: ''
},
onLoad: function (options) {
console.log('登录页options:', options)
if (options.path) {
this.setData({
path: options.path
})
}
let userInfo = wx.getStorageSync('userInfo')
if (userInfo) { //如果有路径,并且缓存有数据,直接跳转
if (userInfo.userName && userInfo.graduateSchool) {
this.loginSuccess()
// console.log('已完善信息,允许登录');
return;
}
if (userInfo.phone) {
this.setData({
phone: userInfo.phone
}, () => {
this.getUserInfo()
})
}
}
this.setData({
pageShow: true
})
wx.login({
success: (res) => {
if (res.code) {
this.setData({
loginCode: res.code
})
this.getOpenId()
} else {
console.log('登录失败!' + res.errMsg)
}
}
})
},
login() {
if (!this.data.encryptedData || !this.data.session_key || !this.data.iv) {
wx.showToast({
title: '一键登陆失败,请重试',
icon: 'none',
duration: 2000
})
}
request({
url: `/wx/app/oneClickLogin/`+ appid,
method: 'post',
data: {
"encryptedData": this.data.encryptedData,
"sessionKey": this.data.session_key,
"iv": this.data.iv,
"openId": this.data.openId
},
}).then(res => {
if (res.code == 0) {
if (res.data) {
this.setData({
phone: res.data.phone
}, () => {
wx.setStorageSync('userInfo', res.data)
wx.setStorageSync('userInfo_id', res.data.id)
wx.switchTab({
url: '/pages/mine/index',
})
})
} else {
wx.showToast({
title: '一键登陆失败,请重试',
icon: 'none',
duration: 2000
})
}
}
})
},
loginSuccess() {
if (this.data.path) {
// 获取三位一体的数据
wx.redirectTo({
url: this.data.path,
})
} else {
wx.switchTab({
url: '/pages/index/index'
})
}
},
getUserInfo() {
// let that = this
// request({
// url: `h5/user/getuserInfoNew`,
// method: 'get',
// data: {
// // PhoneName: '13355752969'
// PhoneName: this.data.phone
// },
// }).then((res) => {
// if (res.success) {
// if (res.data) {
// wx.setStorageSync('userInfo', res.data)
// wx.setStorageSync('showModal', '')
// }
// }
// })
},
getOpenId() {
return request({
url: `/wx/app/getOpenId/wx186236fdcd93edcc`,
method: 'get',
data: {
code: this.data.loginCode
},
}).then(res2 => {
this.setData({
session_key: res2.data.sessionKey
})
this.setData({
openId: res2.data.openid
})
console.log(this.data);
})
},
bindUserName: function (e) {
this.setData({
userName: e.detail.value
})
},
bindCode: function (e) {
this.setData({
code: e.detail.value
})
},
// 点击下一步
next() {
var username = this.data.userName;
var code = this.data.code;
if (!username) {
wx.showToast({
title: '请填写手机号',
icon: 'none',
duration: 1500
})
return false;
}
if (!code) {
wx.showToast({
title: '请填写验证码',
icon: 'none',
duration: 1500
})
return false;
}
// 然后判断手机号码是否正确
if (this.checkPhone(this.data.userName)) {
wx.showToast({
title: '请输入正确的手机号',
icon: 'none',
duration: 2000
})
return
}
wx.showLoading({
title: '请求中',
})
wx.request({
url: api_card + '/wx/app/loginCode',
data: {
code: code,
phone: username
},
method: 'post',
success: (res) => {
wx.hideLoading();
if (res.data.success) {
console.log(res.data)
if (res.data.data) {
this.setData({
phone: res.data.data.phone
}, () => {
this.getUserInfo()
})
} else {
wx.setStorage({
key: "phone",
data: username,
success: function () {
wx.navigateTo({
url: '/pages/login/loginInfo/loginInfo?phone=' + username,
})
}
})
}
} else {
wx.showToast({
title: res.data.message,
icon: 'none',
duration: 1000
})
}
},
fail: function () {
wx.hideLoading();
wx.showToast({
title: '请求失败',
icon: 'none',
duration: 1000
})
}
})
},
checkPhone(phone) {
if (!(/^1[3456789]\d{9}$/.test(phone))) {
return true
} else {
return false
}
},
getCode() {
if (!this.data.canClick) return // 节流 防止频繁访问接口
this.setData({
canClick: false
})
var codeCont = this.data.codeCont;
// 获取验证码的时候 先判断是否输入的手机号
if (!this.data.userName) {
wx.showToast({
title: '请输入手机号',
icon: 'none',
duration: 2000
})
return
}
// 然后判断手机号码是否正确
if (this.checkPhone(this.data.userName)) {
wx.showToast({
title: '请输入正确的手机号',
icon: 'none',
duration: 2000
})
return
}
if (codeCont == '获取验证码') {
wx.showLoading({
title: '请求中',
})
wx.request({
url: api + 'h5/MyVoluntary/AddVerificationCode',
// util.hex_md5
header: {
sign: util.hex_md5("1" + this.data.userName + "206")
},
data: {
code: 206,
mobile: this.data.userName,
type: 1, //写死
},
method: 'get',
success: (res) => {
wx.hideLoading();
if (res.data.success) {
var num = 60;
this.data.clockTimer = setInterval(() => {
num--;
if (num < 0) {
clearInterval(this.data.clockTimer);
this.setData({
canClick: true,
codeCont: '获取验证码'
})
} else {
this.setData({
codeCont: num
})
}
}, 1000)
}
},
fail: function () {
wx.hideLoading();
wx.showToast({
title: '请求失败',
icon: 'none',
duration: 1000
})
}
})
}
},
getPhoneNumber(e) {
console.log(e.detail)
if (e.detail.errMsg == 'getPhoneNumber:ok') {
this.setData({
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
}, () => {
this.login()
})
}
},
//去查看协议
toProtocol() {
wx.navigateTo({
url: '../protocol/protocol',
})
},
//去查看政策
toPolicy() {
wx.navigateTo({
url: '../policy/policy',
})
},
goIndex() {
wx.switchTab({
url: '/pages/index/rank/rank',
})
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
clearInterval(this.data.clockTimer);
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
clearInterval(this.data.clockTimer);
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})