Files
JayuCloud/JC-Salon-WeChat/app.js
T

151 lines
4.6 KiB
JavaScript

const auth = require('./utils/auth')
const authApi = require('./api/auth')
const { BRAND_CONFIG } = require('./utils/config')
const profileCache = require('./utils/profile-cache')
App({
globalData: {
appName: BRAND_CONFIG.appName,
shopName: BRAND_CONFIG.defaultShopName,
userInfo: null,
planningNeedRefresh: false,
wechatLoginState: null,
reviewNoticeShown: false,
openEmployeeReview: false
},
onLaunch() {
this.bootstrapSession()
},
getWechatCode() {
return new Promise((resolve, reject) => {
wx.login({
success: result => result.code ? resolve(result.code) : reject(new Error('未获取到微信登录凭证')),
fail: reject
})
})
},
async bootstrapWechatLogin() {
if (this._wechatBootstrapPromise) return this._wechatBootstrapPromise
this._wechatBootstrapPromise = this.runWechatBootstrap()
try {
return await this._wechatBootstrapPromise
} finally {
this._wechatBootstrapPromise = null
}
},
async runWechatBootstrap() {
try {
const preference = auth.getLoginPreference()
if (!preference || !preference.rememberMe) {
auth.clearSession()
this.globalData.userInfo = null
this.publishWechatLoginState({ status: 'NEED_AGREEMENT' })
this.goLogin()
return
}
const agreements = await authApi.getCurrentAgreements()
const currentVersions = agreementVersions(agreements)
// 协议版本变化后必须重新征求同意,不能先交换 code 再补弹窗。
if (!hasMatchingAgreementVersions(preference.agreementVersions, currentVersions)) {
auth.clearSession()
this.globalData.userInfo = null
this.publishWechatLoginState({ status: 'NEED_AGREEMENT' })
this.goLogin()
return
}
const result = await authApi.wechatQuickLogin({
wxCode: await this.getWechatCode(),
preferredShopId: preference.preferredShopId,
agreementAccepted: true,
agreementVersions: preference.agreementVersions || {},
clientType: 'WECHAT_MINI'
})
this.publishWechatLoginState({ ...result, autoLogin: true })
if (result.status === 'SUCCESS' && result.accessToken) {
this.setSession(result, true)
wx.reLaunch({ url: '/pages/planning/index' })
return
}
auth.clearSession()
this.globalData.userInfo = null
this.goLogin()
} catch (error) {
auth.clearSession()
this.globalData.userInfo = null
this.publishWechatLoginState({
status: 'ERROR',
message: error.message || '自动登录失败,请点击重试'
})
this.goLogin()
}
},
goLogin() {
const pages = getCurrentPages()
const current = pages.length ? pages[pages.length - 1].route : ''
if (current !== 'pages/login/index') wx.reLaunch({ url: '/pages/login/index' })
},
publishWechatLoginState(state) {
this.globalData.wechatLoginState = state
const pages = getCurrentPages()
const current = pages.length ? pages[pages.length - 1] : null
if (current && current.route === 'pages/login/index' && typeof current.setData === 'function') {
current.setData({ loginState: state })
}
},
bootstrapSession() {
const session = auth.getSession()
this.globalData.userInfo = session.userInfo || null
if (session.shopName) {
this.globalData.shopName = session.shopName
}
},
setSession(session, newLoginSession = false) {
if (newLoginSession) {
this.globalData.reviewNoticeShown = false
this.globalData.openEmployeeReview = false
}
auth.setSession(session, newLoginSession)
this.bootstrapSession()
},
clearSession() {
auth.clearSession()
profileCache.invalidate()
this.globalData.userInfo = null
this.globalData.wechatLoginState = null
this.globalData.reviewNoticeShown = false
this.globalData.openEmployeeReview = false
},
logoutDevice() {
auth.clearDeviceLoginState()
profileCache.invalidate()
this.globalData.userInfo = null
this.globalData.wechatLoginState = null
this.globalData.reviewNoticeShown = false
this.globalData.openEmployeeReview = false
}
})
function agreementVersions(agreements) {
return (Array.isArray(agreements) ? agreements : []).reduce((versions, item) => {
if (item && item.agreementType && item.version) versions[item.agreementType] = item.version
return versions
}, {})
}
function hasMatchingAgreementVersions(accepted, current) {
return Boolean(accepted && current && accepted.USER_TERMS && accepted.PRIVACY_POLICY
&& accepted.USER_TERMS === current.USER_TERMS
&& accepted.PRIVACY_POLICY === current.PRIVACY_POLICY)
}