Login.js
7.75 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
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: {},
loginData: {}
},
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://img01.glsx.com.cn/weapp/resource/miniprogram/points-mall/html/${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()
if (!mobile || !captcha || !this.sessionId) {
wx.p.sentry.reportParamError(
'手机号验证码登录异常: mobile, captcha, sessionId',
{mobile, captcha, sessionId: this.sessionId}
)
}
const {
code,
data,
message
} = await commonApi.phoneSmsLogin({
mobile,
captcha,
sessionId: this.sessionId
})
if (code == '0') {
data.headImgUrl = data?.logo
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
}
let { encryptedData, iv, sessionId } = obj
if (!encryptedData || !iv || !sessionId) {
wx.p.sentry.reportParamError(
'授权手机号登录异常: encryptedData, iv, sessionId',
obj
)
}
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.setData({loginData: data})
// 获取积分来源
this.getSource()
},
getSource() {
let { loginData } = this.data
if (!loginData.phone) {
wx.p.sentry.reportParamError(
'获取积分渠道来源异常:staffPhone ',
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)
}
} else {
wx.p.wxApi.showToast(message)
}
})
},
// 设置积分渠道
setChannelSource(source) {
let { loginData } = this.data
// 查询门店数据
this.getMerchantList(source, loginData)
},
getMerchantList(source, loginData) {
if (!loginData.phone || !source) {
wx.p.sentry.reportParamError(
'获取门店列表异常:staffPhone, channelSource',
{...loginData, channelSource: source}
)
}
commonApi.getMerchantData({staffPhone: loginData?.phone, channelSource: source}).then(res => {
let { code, data, message } = res
if (code == 0) {
// 当没有门店数据返回时 默认赋值一个merchantId为 -1的门店作为游客使用
if (!data?.length) {
data = [{merchantId: -1}]
}
if (data?.length == 1) { // 仅有一个门店时跳过选择门店页面 直接登录成功
const { merchantId, merchantName } = data[0] || {}
wx.p.isLogin = true;
if (!source || !merchantId || !loginData.phone) {
wx.p.sentry.reportParamError(
'门店选择异常:staffPhone, channelSource',
{...loginData, channelSource: source, merchantId, merchantName}
)
}
wx.p.storage.set('userInfo', {
...loginData,
channelSource: source,
merchantId,
merchantName
})
wx.p.router.reLaunch('/pages/tab/index/index', {loginPageFlag: true})
} else { // 跳转到选择门店页面
wx.p.router.go('/pages/chooseMerchant/index', {loginData: {channelSource: source, ...loginData}, merchantList: data, loginPageFlag: true})
}
} else {
wx.p.wxApi.showToast(message)
}
})
},
// 选择积分来源确定
pointSourceEvent(e) {
this.setChannelSource(e.detail)
},
// 开通成功
successConfirm() {
let { walletObj } = this.data
// 替换钱包昵称
this.replaceNickName({...walletObj})
},
back() {
wx.p.router.back()
}
})