优化:小程序记住登录与启动跳转

This commit is contained in:
2026-10-10 16:41:37 +08:00
parent ae1b568131
commit d38c5cd4fe
13 changed files with 194 additions and 21 deletions
+18 -6
View File
@@ -1,6 +1,7 @@
const auth = require('./utils/auth')
const authApi = require('./api/auth')
const { BRAND_CONFIG } = require('./utils/config')
const loginFlow = require('./utils/login-flow')
App({
globalData: {
@@ -13,7 +14,6 @@ App({
onLaunch() {
this.bootstrapSession()
this.bootstrapWechatLogin()
},
getWechatCode() {
@@ -26,14 +26,24 @@ App({
},
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 status = await authApi.wechatStatus(await this.getWechatCode())
this.publishWechatLoginState(status)
const preference = auth.getLoginPreference()
if (!preference.rememberMe || status.status !== 'READY') {
if (!loginFlow.shouldAutoLogin(preference, status)) {
auth.clearSession()
this.globalData.userInfo = null
this.goLoginIfNeeded()
this.goLogin()
return
}
const result = await authApi.wechatQuickLogin({
@@ -49,7 +59,9 @@ App({
wx.reLaunch({ url: '/pages/planning/index' })
return
}
wx.reLaunch({ url: '/pages/login/index' })
auth.clearSession()
this.globalData.userInfo = null
this.goLogin()
} catch (error) {
auth.clearSession()
this.globalData.userInfo = null
@@ -57,11 +69,11 @@ App({
status: 'ERROR',
message: error.message || '自动登录失败,请点击重试'
})
this.goLoginIfNeeded()
this.goLogin()
}
},
goLoginIfNeeded() {
goLogin() {
const pages = getCurrentPages()
const current = pages.length ? pages[pages.length - 1].route : ''
if (current !== 'pages/login/index') wx.reLaunch({ url: '/pages/login/index' })