From 0cd61a51b398f26a21cb5932e507b1be941d7aff Mon Sep 17 00:00:00 2001 From: haoliang <821644@qq.com> Date: Wed, 29 Apr 2026 00:21:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B7=AF=E7=94=B1=E9=87=8D?= =?UTF-8?q?=E5=AE=9A=E5=90=91=E5=88=B0/login=20+=20=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E6=8F=90=E7=A4=BA=E4=B8=8D=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - router: createWebHistory base设为import.meta.env.BASE_URL(build=/admin/, dev=/) - vite.config: base按command区分(build时/admin/,dev时/) - request.ts: HTTP错误(4xx/5xx)时解析后端返回的message显示给用户 - request.ts: token过期跳转登录页使用routerBase而非硬编码路径 --- frontend/src/router/index.ts | 6 +++++- frontend/src/utils/request.ts | 14 +++++++++++--- frontend/vite.config.ts | 8 ++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/frontend/src/router/index.ts b/frontend/src/router/index.ts index 0dfefb8..33fca33 100644 --- a/frontend/src/router/index.ts +++ b/frontend/src/router/index.ts @@ -1,5 +1,9 @@ import { createRouter, createWebHistory, type RouteRecordRaw } from 'vue-router' +// 部署在/admin/子路径下时,base必须是/admin/ +// 本地开发(npm run dev)时base是/ +const routerBase = import.meta.env.BASE_URL + // 管理后台Layout const AdminLayout = () => import('@/layouts/AdminLayout.vue') // 大屏Layout @@ -92,7 +96,7 @@ function generateMockRoutes(routes: RouteRecordRaw[]): RouteRecordRaw[] { } const router = createRouter({ - history: createWebHistory(), + history: createWebHistory(routerBase), routes: [...normalRoutes, ...generateMockRoutes(normalRoutes)], }) diff --git a/frontend/src/utils/request.ts b/frontend/src/utils/request.ts index c957fec..bf765a2 100644 --- a/frontend/src/utils/request.ts +++ b/frontend/src/utils/request.ts @@ -3,6 +3,9 @@ import { ElMessage } from 'element-plus' import router from '@/router' import type { ApiResponse } from '@/types' +// 部署base路径,用于token过期时跳转登录页 +const routerBase = import.meta.env.BASE_URL + // 创建axios实例 const service = axios.create({ timeout: 30000, @@ -36,8 +39,8 @@ service.interceptors.response.use( // Token无效/过期 if (res.code === 40101) { localStorage.removeItem('token') - const isMock = window.location.pathname.startsWith('/mock') - router.push(isMock ? '/mock/login' : '/login') + const loginPath = routerBase + 'login' + router.push(loginPath) ElMessage.warning('登录已过期') return Promise.reject(new Error(res.message)) } @@ -48,10 +51,15 @@ service.interceptors.response.use( return Promise.reject(new Error(msg)) }, (error) => { + // HTTP 错误(4xx/5xx):尝试解析后端返回的错误信息 if (error.response) { - ElMessage.error('网络异常') + const data = error.response.data as ApiResponse | undefined + const msg = data?.message || `请求失败 (${error.response.status})` + ElMessage.error(msg) } else if (error.code === 'ECONNABORTED') { ElMessage.error('请求超时') + } else { + ElMessage.error('网络异常,请检查网络连接') } return Promise.reject(error) } diff --git a/frontend/vite.config.ts b/frontend/vite.config.ts index 3c075f1..73b3d9d 100644 --- a/frontend/vite.config.ts +++ b/frontend/vite.config.ts @@ -4,9 +4,9 @@ import { viteMockPlugin } from './mock/plugin' import path from 'path' // https://vite.dev/config/ -export default defineConfig({ - // 部署到 /admin/ 子路径,静态资源路径前缀 - base: '/admin/', +export default defineConfig(({ command }) => ({ + // build时部署到/admin/子路径,dev时用根路径 + base: command === 'build' ? '/admin/' : '/', plugins: [ vue(), viteMockPlugin({ @@ -35,4 +35,4 @@ export default defineConfig({ } } }, -}) +}))