diff --git a/web/src/types/env.d.ts b/web/src/types/env.d.ts index 766cc99..863fb8f 100644 --- a/web/src/types/env.d.ts +++ b/web/src/types/env.d.ts @@ -10,6 +10,7 @@ interface ImportMetaEnv { readonly VITE_DEBUG: string readonly VITE_MOCK: string readonly VITE_APP_DESCRIPTION: string + readonly BASE_URL: string } interface ImportMeta { diff --git a/web/src/utils/request.ts b/web/src/utils/request.ts index 4d2cf02..1583eb8 100644 --- a/web/src/utils/request.ts +++ b/web/src/utils/request.ts @@ -190,8 +190,13 @@ const createAxiosInstance = (): AxiosInstance => { const handleUnauthorized = () => { console.warn('🚫 收到401未授权响应') - // 检查当前页面是否是登录页,避免在登录页面重复处理 - if (window.location.pathname === '/login') { + // 计算当前应用登录页路径,兼容本地和生产环境 + const base = import.meta.env.BASE_URL || '/' + const normalizedBase = base.endsWith('/') ? base : `${base}/` + const loginPath = `${normalizedBase}login` + + // 如果已经在登录页,避免重复处理 + if (window.location.pathname === loginPath) { console.log('🚫 当前在登录页面,不处理401错误') return } @@ -211,8 +216,8 @@ const handleUnauthorized = () => { localStorage.removeItem('refresh_token') localStorage.removeItem('user_info') - // 跳转到登录页 - window.location.href = '/login' + // 使用带前缀的完整登录路径,避免 404 + window.location.href = loginPath }).catch(() => { // 用户取消,不清除认证信息,让用户继续操作 console.log('🚫 用户取消重新登录')