Login.js
7.92 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
import commonApi from '~/api/module/common.js'
const phoneRex = /^[1]([3-9])[0-9]{9}$/
Page({
/**
* 页面的初始数据
*/
data: {
loginType: 0,
isAgree: false,
mobile: '',
captcha: '',
count: 0,
showChooseSource: false,
showEnableWallet: false,
showEnableSuccess: false,
buttons: [{
text: '取消'
}, {
text: '同意',
extClass: 'btn_primary'
}],
pointsSource: [],
walletObj: {}
},
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 : '请同意以下用户协议')
},
showModal() {
this.setData({
showAgreeModal: true
})
},
dialogConfirm(e) {
let { value } = e.detail
this.setData({
showAgreeModal: false
})
if (value == 1) {
this.setData({
isAgree: true
})
}
},
/**手机号短信登录 */
async phoneLogin() {
const {
mobile,
captcha,
isAgree
} = this.data
if (!isAgree) {
return this.showModal()
}
try {
if (!mobile) throw '请输入手机号'
if (!captcha) throw '请输入验证码'
if (!phoneRex.test(mobile)) throw '请输入正确的手机号'
wx.p.wxApi.showLoading()
const {
code,
data,
message
} = await commonApi.phoneSmsLogin({
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
}) {
try {
if (detail.errMsg && ['authorized', 'deny'].findIndex(e => detail.errMsg.toLowerCase().includes(e)) != -1) return
if (!detail?.code) throw '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。'
wx.p.wxApi.showLoading()
let obj = {
encryptedData: detail.encryptedData,
iv: detail.iv,
sessionId: this.sessionId
}
const {
code,
data,
message
} = await commonApi.authPhoneLogin(obj)
wx.hideLoading()
if (code == '0') this.customerLoginCallBack(data)
else throw message
} catch (error) {
this.showTips(error || '服务不可用')
setTimeout(() => {
this.onLoad()
}, 1500)
}
},
/**登录回调 */
customerLoginCallBack(data) {
wx.p.storage.set('sessionId', data?.sessionId)
// 获取积分来源
this.getSource(data)
},
getSource(loginData) {
commonApi.getPointsSourceList({
staffPhone: loginData?.phone
}).then(res => {
let { code, data, message } = res
if (code == 0) {
this.setData({pointsSource: data?.map(val => { return val.channelSource })})
if (data?.length > 1) { // 积分渠道来源大于1 需要选择
this.setData({
showChooseSource: true
})
} else if (data?.length == 1) {
this.setChannelSource(data[0]?.channelSource, loginData)
}
} else {
wx.p.wxApi.showToast(message)
}
})
},
// 设置积分渠道
setChannelSource(source, loginData) {
wx.p.isLogin = true
wx.p.storage.set('userInfo', {...loginData})
wx.p.storage.set('channelSource', `${source}`)
wx.p.storage.set('sessionId', loginData?.sessionId)
// 查询积分钱包
this.getPointsWallet()
},
// 获取积分钱包数据
getPointsWallet() {
let userInfo = wx.p.cache.userInfo.getUserInfo()
commonApi.getPointsWalletData({
phone: userInfo?.phone,
name: userInfo?.nickName,
scoreSource: wx.p.storage.get('channelSource')
}).then(res => {
let { code, data, message } = res
if (code == 0) {
if (data && Object.keys(data)?.length) { // 有数据返回说明已开通钱包
// 调用替换钱包昵称接口
this.replaceNickName(data)
} else {
let channelSource = wx.p.storage.get('channelSource')
// 获取当前积分数量
commonApi.getPointsSummary({staffPhone: userInfo?.phone, channelSource}).then(r => {
if (r?.code == 0) {
this.setData({
showEnableWallet: true,
walletObj: {name: userInfo?.nickName, phone: userInfo?.phone, scoreSource: channelSource, currentScore: r?.data?.currentScore || 0}
})
} else {
wx.p.wxApi.showToast(r.message)
}
})
}
} else {
wx.p.wxApi.showToast(message)
}
})
},
// 替换积分钱包昵称
replaceNickName(data) {
let userInfo = wx.p.cache.userInfo.getUserInfo()
commonApi.replaceWalletName({
nickName: data?.name,
phone: data?.phone,
scoreSource: data?.scoreSource
}).then(res => {
let { code, data, message } = res
wx.p.storage.set('userInfo', {...userInfo, nickName: data?.name}) // 更新storage中的nickName
if (code == 0) {
wx.p.router.reLaunch('/pages/tab/index/index')
} else {
wx.p.wxApi.showToast(message)
}
})
},
// 选择积分来源
pointSourceEvent(e) {
wx.p.storage.set('channelSource', `${e.detail}`)
},
// 关闭开通积分钱包弹窗
closeWallet() {
this.setData({ showEnableWallet: false })
let userInfo = wx.p.cache.userInfo.getUserInfo()
let data = {
name: userInfo?.nickName,
phone: userInfo?.phone,
scoreSource: wx.p.storage.get('channelSource')
}
// 替换钱包昵称 使用登录信息返回的数据
this.replaceNickName(data)
},
// 一键开通积分钱包
enableWalletConfirm(e) {
let { walletObj } = e.detail
commonApi.openPointsWallet(walletObj).then(res => {
let { code, message } = res
if (code == 0) {
// 替换钱包昵称
this.replaceNickName({...walletObj})
} else {
wx.p.wxApi.showToast(message)
}
})
},
// 开通成功
successConfirm() {
},
back() {
wx.p.router.back()
}
})