优化小程序手机号与微信授权登录
This commit is contained in:
@@ -99,6 +99,24 @@ module.exports = {
|
||||
})
|
||||
},
|
||||
|
||||
wechatPhoneLogin(data) {
|
||||
return request({
|
||||
url: '/user/wechat-mini/phone-login',
|
||||
method: 'POST',
|
||||
params: { __baseUrl: API_DOMAIN },
|
||||
data
|
||||
})
|
||||
},
|
||||
|
||||
wechatConfirmLogin(data) {
|
||||
return request({
|
||||
url: '/user/wechat-mini/confirm-login',
|
||||
method: 'POST',
|
||||
params: { __baseUrl: API_DOMAIN },
|
||||
data
|
||||
})
|
||||
},
|
||||
|
||||
getPhoneLoginShops(phone) {
|
||||
return request({
|
||||
url: `/user/check/login-shops/phone/${encodeURIComponent(phone)}`,
|
||||
|
||||
@@ -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 })
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -23,12 +23,12 @@
|
||||
</view>
|
||||
<view wx:if="{{loginMode === 'phone'}}" class="field">
|
||||
<text class="field-label">手机号</text>
|
||||
<input value="{{form.phone}}" data-field="phone" bindinput="onInput" type="number" placeholder="请输入手机号" />
|
||||
<input value="{{form.phone}}" data-field="phone" bindinput="onInput" type="text" maxlength="11" placeholder="请输入手机号" />
|
||||
</view>
|
||||
<view wx:if="{{loginMode === 'phone'}}" class="field">
|
||||
<text class="field-label">验证码</text>
|
||||
<view class="code-row">
|
||||
<input class="code-input" value="{{form.code}}" data-field="code" bindinput="onInput" type="number" placeholder="请输入验证码" />
|
||||
<input class="code-input" value="{{form.code}}" data-field="code" bindinput="onInput" type="text" maxlength="6" placeholder="请输入验证码" />
|
||||
<button class="code-btn {{countdown > 0 ? 'counting' : ''}}" loading="{{codeLoading}}" disabled="{{codeLoading || countdown > 0}}" bindtap="sendLoginCode">
|
||||
<text wx:if="{{countdown > 0}}">重新获取 {{countdown}}s</text>
|
||||
<text wx:else>获取验证码</text>
|
||||
@@ -56,7 +56,7 @@
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<button class="secondary-btn wechat-btn" bindtap="wechatLogin">微信快捷登录</button>
|
||||
<button class="secondary-btn wechat-btn" open-type="getPhoneNumber" bindgetphonenumber="wechatPhoneLogin">微信手机号一键登录</button>
|
||||
|
||||
<view wx:if="{{shopSelectVisible}}" class="shop-select-mask">
|
||||
<view class="shop-select-dialog">
|
||||
|
||||
Reference in New Issue
Block a user