Login.js
4.22 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
const phoneRex = /^[1]([3-9])[0-9]{9}$/
Page({
/**
* 页面的初始数据
*/
data: {
loginType: 0,
isAgree: false,
mobile: '',
captcha: '',
count: 0
},
changeLoginType() {
const {
loginType
} = this.data
this.setData({
loginType: loginType == 0 ? 1 : 0
})
},
// 同意隐私政策
handleChangeAgree() {
this.setData({
isAgree: !this.data.isAgree
})
},
// 跳转隐私政策页面
handleAgree(e) {
const {
name
} = e.currentTarget.dataset
wx.p.router.jumpH5(`https://img02.glsx.com.cn/weapp/resource/saas/scrm/html/hmd/${name}.html`, true)
},
async onLoad() {
wx.p.wxApi.showLoading()
const {
data,
code
} = await wx.p.api.userApi.login()
if (code == '0') this.customerLoginCallBack(data)
if (data) this.sessionId = data.sessionId
wx.hideLoading()
},
input(e) {
const {
value
} = e.detail, {
name
} = e.target.dataset
this.setData({
[name]: value
})
},
/**获取验证码 */
async getSmsCode() {
const {
mobile,
count
} = this.data
try {
if (!phoneRex.test(mobile)) throw '请输入正确的手机号'
if (count) throw '验证码发送过于频繁,请稍后在试'
} catch (error) {
return wx.p.wxApi.showToast(error)
}
wx.p.wxApi.showLoading()
try {
const {
code,
message
} = await wx.p.api.userApi.getSmsCode(mobile)
if (code == '0') {
wx.p.wxApi.showToast('验证码已发送')
this.countDown(60)
} else wx.p.wxApi.showToast(message || '发送验证码失败')
} catch (error) {
wx.p.wxApi.showToast(error)
}
},
/**验证码倒计时 */
countDown(countDown) {
if (countDown) this.setData({
count: countDown
})
setTimeout(() => {
let {
count
} = this.data
this.setData({
count: --count
})
if (count > 0) this.countDown()
else this.setData({
count: null
})
}, 1e3)
},
showTips(message) {
wx.p.wxApi.showToast(typeof message == 'string' ? message : '请同意以下用户协议')
},
/**手机号短信登录 */
async phoneLogin() {
const {
mobile,
captcha,
isAgree
} = this.data
try {
if (!mobile) throw '请输入手机号'
if (!captcha) throw '请输入验证码'
if (!phoneRex.test(mobile)) throw '请输入正确的手机号'
if (!isAgree) throw '请同意以下用户协议'
wx.p.wxApi.showLoading()
const {
code,
data,
message
} = await wx.p.api.userApi.phoneLogin({
mobile,
captcha,
sessionId: this.sessionId
})
if (code == '0') this.customerLoginCallBack(data)
else throw message
wx.hideLoading()
} catch (error) {
this.showTips(error || '服务不可用')
setTimeout(() => {
this.onLoad()
}, 1500)
}
},
/**微信登录 */
async getPhoneNumber({
detail
}) {
const {
isAgree
} = this.data
try {
if (detail.errMsg && ['authorized', 'deny'].findIndex(e => detail.errMsg.toLowerCase().includes(e)) != -1) return
if (!detail?.code) throw '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
if (!isAgree) throw '请同意以下用户协议'
wx.p.wxApi.showLoading()
const {
code,
data,
message
} = await wx.p.api.userApi.authUserPhone(detail, this.sessionId)
wx.hideLoading()
if (code == '0') this.customerLoginCallBack(data)
else throw message
} catch (error) {
console.log(error)
this.showTips(error || '服务不可用')
setTimeout(() => {
this.onLoad()
}, 1500)
}
},
/**C端登录回调 */
customerLoginCallBack(data) {
wx.$LoginStatus = false
wx.p.storage.set('userInfo', data)
wx.p.storage.set('sessionId', data.sessionId)
// 返回上一页
wx.p.router.back(thisPage => {
thisPage && thisPage.setData({
userInfo: data
})
thisPage.onLoginSuccess && thisPage.onLoginSuccess() //登陆成功后页面回调
})
},
back() {
wx.p.router.back()
}
})