过期跳转登录页

This commit is contained in:
WindowBird 2025-11-10 16:34:34 +08:00
parent cd4b64140d
commit f31ef4b79d

View File

@ -4,6 +4,8 @@ import { useUserStore } from '@/store/user'
* 初始化请求拦截器
* 使用 Pinia store 管理用户状态和 token
*/
let isRedirectingToLogin = false
export const Request = () => {
// 初始化请求配置
uni.$uv.http.setConfig((config) => {
@ -48,9 +50,15 @@ export const Request = () => {
// token 过期或无效,清除登录状态
const userStore = useUserStore()
userStore.logout()
// 跳转到登录页面(根据实际路由调整)
// uni.reLaunch({ url: '/pages/login/index' })
if (!isRedirectingToLogin) {
isRedirectingToLogin = true
uni.$uv.toast('登录已过期,请重新登录')
setTimeout(() => {
uni.reLaunch({ url: '/pages/login/index' })
isRedirectingToLogin = false
}, 200)
}
}
if (data.code !== 200) {
@ -83,7 +91,14 @@ export const Request = () => {
if (response.statusCode === 401 || response.statusCode === 403) {
const userStore = useUserStore()
userStore.logout()
// uni.reLaunch({ url: '/pages/login/index' })
if (!isRedirectingToLogin) {
isRedirectingToLogin = true
uni.$uv.toast('登录已过期,请重新登录')
setTimeout(() => {
uni.reLaunch({ url: '/pages/login/index' })
isRedirectingToLogin = false
}, 200)
}
}
return Promise.reject(response)
})