功能:新增微信小程序一键登录
This commit is contained in:
+123
-2
@@ -1,16 +1,22 @@
|
||||
const auth = require('./utils/auth')
|
||||
const authApi = require('./api/auth')
|
||||
const cartApi = require('./api/cart')
|
||||
const auth = require('./utils/auth')
|
||||
|
||||
App({
|
||||
globalData: {
|
||||
userInfo: null,
|
||||
isLoggedIn: false,
|
||||
cartCount: 0,
|
||||
pendingProductFilter: null
|
||||
pendingProductFilter: null,
|
||||
pendingWechatMiniLogin: null,
|
||||
wechatMiniAutoLoginState: 'idle'
|
||||
},
|
||||
|
||||
onLaunch() {
|
||||
this.bootstrapSession()
|
||||
if (!auth.isLoggedIn()) {
|
||||
this.tryWechatMiniAutoLogin()
|
||||
}
|
||||
},
|
||||
|
||||
bootstrapSession() {
|
||||
@@ -19,6 +25,10 @@ App({
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.isLoggedIn = isLoggedIn
|
||||
|
||||
if (userInfo && userInfo.shopId) {
|
||||
auth.setPreferredMiniLoginShopId(userInfo.shopId)
|
||||
}
|
||||
|
||||
if (isLoggedIn) {
|
||||
this.updateCartBadge()
|
||||
return
|
||||
@@ -27,10 +37,117 @@ App({
|
||||
this.clearCartBadge()
|
||||
},
|
||||
|
||||
async tryWechatMiniAutoLogin() {
|
||||
if (auth.isLoggedIn()) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (this._wechatMiniAutoLoginPromise) {
|
||||
return this._wechatMiniAutoLoginPromise
|
||||
}
|
||||
|
||||
if (typeof wx === 'undefined' || typeof wx.login !== 'function') {
|
||||
return false
|
||||
}
|
||||
|
||||
this.globalData.wechatMiniAutoLoginState = 'pending'
|
||||
this._wechatMiniAutoLoginPromise = this.requestWechatMiniCode()
|
||||
.then(code => authApi.wechatMiniAutoLogin({
|
||||
wxCode: code,
|
||||
preferredShopId: auth.getPreferredMiniLoginShopId() || undefined
|
||||
}))
|
||||
.then(result => this.handleWechatMiniLoginResult(result, { silent: true }))
|
||||
.catch(() => false)
|
||||
.finally(() => {
|
||||
this._wechatMiniAutoLoginPromise = null
|
||||
if (this.globalData.wechatMiniAutoLoginState === 'pending') {
|
||||
this.globalData.wechatMiniAutoLoginState = 'idle'
|
||||
}
|
||||
})
|
||||
|
||||
return this._wechatMiniAutoLoginPromise
|
||||
},
|
||||
|
||||
handleWechatMiniLoginResult(result, options) {
|
||||
const config = options || {}
|
||||
const status = result && result.status ? result.status : ''
|
||||
|
||||
if (status === 'SUCCESS') {
|
||||
this.setUserSession(result)
|
||||
this.clearPendingWechatMiniLogin()
|
||||
this.globalData.wechatMiniAutoLoginState = 'idle'
|
||||
return true
|
||||
}
|
||||
|
||||
if (status === 'SELECT_SHOP') {
|
||||
this.setPendingWechatMiniLogin(result)
|
||||
this.globalData.wechatMiniAutoLoginState = 'idle'
|
||||
if (!config.silent && result.message) {
|
||||
wx.showToast({
|
||||
title: result.message,
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
this.clearPendingWechatMiniLogin()
|
||||
this.globalData.wechatMiniAutoLoginState = 'idle'
|
||||
if (!config.silent && result && result.message) {
|
||||
wx.showToast({
|
||||
title: result.message,
|
||||
icon: 'none'
|
||||
})
|
||||
}
|
||||
return false
|
||||
},
|
||||
|
||||
requestWechatMiniCode() {
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.login({
|
||||
success: res => {
|
||||
if (res && res.code) {
|
||||
resolve(res.code)
|
||||
return
|
||||
}
|
||||
reject(new Error('获取微信登录凭证失败'))
|
||||
},
|
||||
fail: reject
|
||||
})
|
||||
})
|
||||
},
|
||||
|
||||
isWechatMiniAutoLoginPending() {
|
||||
return this.globalData.wechatMiniAutoLoginState === 'pending'
|
||||
},
|
||||
|
||||
waitForWechatMiniAutoLogin() {
|
||||
return this._wechatMiniAutoLoginPromise || Promise.resolve(auth.isLoggedIn())
|
||||
},
|
||||
|
||||
setPendingWechatMiniLogin(payload) {
|
||||
this.globalData.pendingWechatMiniLogin = payload || null
|
||||
},
|
||||
|
||||
consumePendingWechatMiniLogin() {
|
||||
const pending = this.globalData.pendingWechatMiniLogin
|
||||
this.globalData.pendingWechatMiniLogin = null
|
||||
return pending
|
||||
},
|
||||
|
||||
clearPendingWechatMiniLogin() {
|
||||
this.globalData.pendingWechatMiniLogin = null
|
||||
},
|
||||
|
||||
setUserSession(payload) {
|
||||
auth.setSession(payload)
|
||||
this.globalData.userInfo = auth.getStoredUserInfo()
|
||||
this.globalData.isLoggedIn = auth.isLoggedIn()
|
||||
|
||||
if (this.globalData.userInfo && this.globalData.userInfo.shopId) {
|
||||
auth.setPreferredMiniLoginShopId(this.globalData.userInfo.shopId)
|
||||
}
|
||||
|
||||
this.updateCartBadge()
|
||||
},
|
||||
|
||||
@@ -38,6 +155,10 @@ App({
|
||||
auth.updateStoredUserInfo(userInfo)
|
||||
this.globalData.userInfo = userInfo
|
||||
this.globalData.isLoggedIn = auth.isLoggedIn()
|
||||
|
||||
if (userInfo && userInfo.shopId) {
|
||||
auth.setPreferredMiniLoginShopId(userInfo.shopId)
|
||||
}
|
||||
},
|
||||
|
||||
clearUserSession() {
|
||||
|
||||
Reference in New Issue
Block a user