fix: token 过期时前端自动清除登录态并跳转登录
根因: 页面使用 uni.request() 直接请求,401 时只有静默 catch, 不清除本地 token,导致界面显示已登录但后端拒接。 修复: - 新增 utils/auth.ts: clearAuth() + checkAuth() 统一处理 401 - member.vue: refreshState / startGravityPay / startPlanPay / pollPayResult / activatePlan 全部添加 401 检测 - user.vue: fetchUserInfo / loadMemberStatus / loadStats / preCreateShare 添加 401 检测 - interview.vue: startInterview / sendAnswer / speakAiText / startRecord 添加 401 检测
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* 清除登录状态 —— token 过期 / 未授权时调用
|
||||
*/
|
||||
export function clearAuth() {
|
||||
uni.removeStorageSync('token')
|
||||
uni.removeStorageSync('userInfo')
|
||||
uni.showToast({ title: '登录已过期,请重新登录', icon: 'none' })
|
||||
// 延迟跳转,避免和当前页面操作冲突
|
||||
setTimeout(() => {
|
||||
uni.navigateTo({ url: '/pages/login/login' })
|
||||
}, 800)
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查 uni.request 的响应是否为 401,是则自动清除登录态
|
||||
* 返回 true 表示已处理 401(调用方应停止后续逻辑)
|
||||
*/
|
||||
export function checkAuth(res: any): boolean {
|
||||
if (res.statusCode === 401) {
|
||||
clearAuth()
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user