From 06a3638c298fdc341720fe6837b0432f93892fb5 Mon Sep 17 00:00:00 2001 From: huazhongmin Date: Sat, 22 Nov 2025 22:36:42 +0800 Subject: [PATCH] =?UTF-8?q?web=E7=AB=AF=E8=B7=B3=E8=BD=AC=E5=88=B0?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E9=A1=B5=E7=9A=84bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/types/env.d.ts | 1 + web/src/utils/request.ts | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) 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('🚫 用户取消重新登录')