This commit is contained in:
2026-09-13 02:02:51 +08:00
parent afd0973ce5
commit df5492a48a
4 changed files with 98 additions and 37 deletions
+2 -15
View File
@@ -34,21 +34,8 @@ export function isLoggedIn(): boolean {
return !!getAccessToken()
}
// token 过期提前量(秒):提前 30s 视为过期,避免请求在途时刚好失效
const EXPIRE_LEAD_SECONDS = 30
/**
* access token 是否已过期(仅用于请求前预刷新,不参与登录态判定)
* expire_time 单位兼容秒与毫秒:数值大于 1e12 视为毫秒时间戳。
*/
export function isTokenExpired(): boolean {
const expireTime = getExpireTime()
if (!expireTime) return false // 未记录过期时间时不臆测,交给后端判定
const isMillisecond = expireTime > 1e12
const expireMs = isMillisecond ? expireTime : expireTime * 1000
return Date.now() + EXPIRE_LEAD_SECONDS * 1000 >= expireMs
}
// token 过期判定统一由后端 401 驱动,前端不再猜测 expire_time 的单位
// (秒 / 毫秒 / 剩余秒数无法可靠区分,误判会导致不必要的刷新与误踢登录)
export function clearUserInfo(): void {
wx.removeStorageSync(STORAGE_KEYS.USER_INFO)