fix(auth): 登录/退出后同步刷新 authReady,避免误判未登录

authReady 只在 onShow 静默登录时创建:手机号授权登录成功或主动退出后不刷新,其他页面 waitForAuth 仍会拿到旧的 Promise 结果。登录成功补 resolve(true),退出补 resolve(false),并在 user 已存在时直接返回成功。
This commit is contained in:
lik
2026-10-31 22:10:09 +08:00
parent c02cfa5c18
commit 2d9d93b333
2 changed files with 11 additions and 0 deletions
+5
View File
@@ -184,6 +184,8 @@ Page({
doLogout() {
const app = getApp()
app.globalData.user = null
// 同步把就绪 Promise 置为失败:否则后续 waitForAuth 仍会拿到旧的 true
app.globalData.authReady = Promise.resolve(false)
wx.removeStorageSync('user_phonenumber')
// 落一个"已主动退出"标记:app.js 的 onShow 每次都会静默重登
// (wxsignin 只凭 code 换 openid 即可重新签发 token),不拦这一下,
@@ -215,6 +217,9 @@ Page({
}
const app = getApp()
app.globalData.user = data.data.user
// 手机号授权登录成功:同步刷新启动期那次静默登录的结果,
// 否则其他页面等到的仍是旧的失败 Promise
app.globalData.authReady = Promise.resolve(true)
wx.setStorageSync('user_phonenumber', data.data.user?.profile?.mobile || '')
// 用户重新授权登录,解除对静默重登的拦截
wx.removeStorageSync('has_logout')