优化小程序手机号与微信授权登录
This commit is contained in:
@@ -101,7 +101,8 @@ Page({
|
||||
shopSelectVisible: false,
|
||||
shopSelectMode: '',
|
||||
shopOptions: [],
|
||||
switchingShop: false
|
||||
switchingShop: false,
|
||||
wechatLoginTicket: ''
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
@@ -291,6 +292,7 @@ Page({
|
||||
const query = [
|
||||
`registrationTicket=${encodeURIComponent(ticket)}`,
|
||||
`phone=${encodeURIComponent(data.phone || this.data.form.phone || '')}`,
|
||||
data.wxCode ? `wxCode=${encodeURIComponent(data.wxCode)}` : '',
|
||||
data.registrationApplicationId ? `applicationId=${encodeURIComponent(data.registrationApplicationId)}` : ''
|
||||
].filter(Boolean).join('&')
|
||||
setTimeout(() => wx.navigateTo({ url: `/pages/register/index?${query}` }), 700)
|
||||
@@ -382,6 +384,8 @@ Page({
|
||||
try {
|
||||
if (this.data.shopSelectMode === 'login') {
|
||||
await this.loginWithSelectedShop(shopId)
|
||||
} else if (this.data.shopSelectMode === 'wechat') {
|
||||
await this.loginWithWechatSelectedShop(shopId)
|
||||
} else {
|
||||
await this.switchSelectedShop(shopId)
|
||||
}
|
||||
@@ -409,6 +413,16 @@ Page({
|
||||
wx.reLaunch({ url: '/pages/planning/index' })
|
||||
},
|
||||
|
||||
async loginWithWechatSelectedShop(shopId) {
|
||||
const result = await authApi.wechatConfirmLogin({ ticket: this.data.wechatLoginTicket, shopId, ...this.buildAgreementPayload() })
|
||||
if (result.status !== 'SUCCESS' || !result.accessToken) {
|
||||
throw new Error(result.message || '进入店铺失败')
|
||||
}
|
||||
getApp().setSession(result)
|
||||
this.closeShopSelector()
|
||||
wx.reLaunch({ url: '/pages/planning/index' })
|
||||
},
|
||||
|
||||
startCountdown() {
|
||||
if (this.data.countdownTimer) {
|
||||
clearInterval(this.data.countdownTimer)
|
||||
@@ -531,5 +545,38 @@ Page({
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '微信快捷登录失败', icon: 'none' })
|
||||
}
|
||||
},
|
||||
|
||||
async wechatPhoneLogin(event) {
|
||||
if (this.data.loading || !this.ensureAgreementAccepted()) {
|
||||
return
|
||||
}
|
||||
const phoneCode = event && event.detail && event.detail.code
|
||||
if (!phoneCode) {
|
||||
wx.showToast({ title: '需要授权手机号后才能登录', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
const wxCode = await this.getWechatCode()
|
||||
const result = await authApi.wechatPhoneLogin({ wxCode, phoneCode, ...this.buildAgreementPayload() })
|
||||
if (result.registrationRequired) {
|
||||
this.openRegistrationFlow({ ...result, wxCode })
|
||||
return
|
||||
}
|
||||
if (result.status === 'SELECT_SHOP') {
|
||||
this.setData({ shopOptions: buildShopOptionsFromSelection(result), shopSelectVisible: true, shopSelectMode: 'wechat', wechatLoginTicket: result.ticket, switchingShop: false })
|
||||
return
|
||||
}
|
||||
if (result.status !== 'SUCCESS' || !result.accessToken) {
|
||||
throw new Error(result.message || '微信手机号登录暂不可用')
|
||||
}
|
||||
getApp().setSession(result)
|
||||
wx.reLaunch({ url: '/pages/planning/index' })
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '微信手机号登录失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user