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:
@@ -170,6 +170,7 @@ import { onShow, onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
|
||||
import { onShow } from '@dcloudio/uni-app'
|
||||
// #endif
|
||||
import { api } from '../../config'
|
||||
import { checkAuth } from '../../utils/auth'
|
||||
|
||||
// #ifdef MP-WEIXIN
|
||||
const shareCodeRef = ref('')
|
||||
@@ -216,7 +217,10 @@ async function preCreateShare() {
|
||||
data: { type: 'app', title: '我在AI磁场·职引练习面试', description: 'AI模拟面试+简历优化,快来一起提升吧' },
|
||||
header: { Authorization: `Bearer ${token.value}` },
|
||||
})
|
||||
if (res.statusCode < 200 || res.statusCode >= 300) return
|
||||
if (res.statusCode < 200 || res.statusCode >= 300) {
|
||||
checkAuth(res)
|
||||
return
|
||||
}
|
||||
const data = res.data?.data || res.data
|
||||
if (data.shareCode) {
|
||||
shareCodeRef.value = data.shareCode
|
||||
@@ -231,6 +235,8 @@ const fetchUserInfo = async () => {
|
||||
if (res.statusCode === 200 && res.data) {
|
||||
userInfo.value = res.data
|
||||
uni.setStorageSync('userInfo', JSON.stringify(res.data))
|
||||
} else if (checkAuth(res)) {
|
||||
return // token 过期,已清除并跳转登录
|
||||
}
|
||||
} catch(e) { /* silent */ }
|
||||
}
|
||||
@@ -244,6 +250,8 @@ const loadMemberStatus = async () => {
|
||||
const res = await uni.request({ url: api('/member/status'), method: 'GET', header: { 'Authorization': `Bearer ${token.value}` } })
|
||||
if (res.statusCode >= 200 && res.statusCode < 300 && res.data) {
|
||||
memberInfo.value = { plan: res.data.plan || 'free', planName: res.data.planName || '免费版', remaining: res.data.remaining ?? 0, gravity: res.data.gravity ?? 0 }
|
||||
} else if (checkAuth(res)) {
|
||||
return
|
||||
}
|
||||
} catch(e) { /* silent */ }
|
||||
}
|
||||
@@ -251,7 +259,11 @@ const loadMemberStatus = async () => {
|
||||
const loadStats = async () => {
|
||||
try {
|
||||
const res = await uni.request({ url: api('/interview/stats/mine'), method: 'GET', header: { 'Authorization': `Bearer ${token.value}` } })
|
||||
if (res.statusCode === 200) stats.value = res.data
|
||||
if (res.statusCode === 200) {
|
||||
stats.value = res.data
|
||||
} else if (checkAuth(res)) {
|
||||
return
|
||||
}
|
||||
} catch(e) { console.error(e) }
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user