From 4aa63baf75273bf3eae55a9a9bc1f60817abbcb7 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Mon, 12 Oct 2026 18:02:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(router):=20=E8=B7=AF=E7=94=B1=E5=AE=88?= =?UTF-8?q?=E5=8D=AB=E6=94=B9=20anon=20=E7=99=BD=E5=90=8D=E5=8D=95?= =?UTF-8?q?=E5=8F=8D=E5=90=91=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 业务路由越加越多,原来硬编码的 8 项守卫名单跟不上了, 未登录直接进十来个业务页看到骨架再被踢,体感抖。 守卫改成只对登录、注册、找回密码、根路径这四页放行,其余一律查 token。 顺手把 9 处没人读的 meta.requiresAuth 和 meta.title 死字段删了。 --- main/manager-web/src/router/index.js | 58 ++++++---------------------- 1 file changed, 11 insertions(+), 47 deletions(-) diff --git a/main/manager-web/src/router/index.js b/main/manager-web/src/router/index.js index bb45b9110..d4b486e97 100644 --- a/main/manager-web/src/router/index.js +++ b/main/manager-web/src/router/index.js @@ -81,10 +81,6 @@ const routes = [ name: 'ParamsManagement', component: function () { return import('../views/ParamsManagement.vue') - }, - meta: { - requiresAuth: true, - title: '参数管理' } }, { @@ -92,10 +88,6 @@ const routes = [ name: 'KnowledgeBaseManagement', component: function () { return import('../views/KnowledgeBaseManagement.vue') - }, - meta: { - requiresAuth: true, - title: '知识库管理' } }, { @@ -103,10 +95,6 @@ const routes = [ name: 'ServerSideManager', component: function () { return import('../views/ServerSideManager.vue') - }, - meta: { - requiresAuth: true, - title: '服务端管理' } }, { @@ -114,10 +102,6 @@ const routes = [ name: 'OtaManagement', component: function () { return import('../views/OtaManagement.vue') - }, - meta: { - requiresAuth: true, - title: 'OTA管理' } }, { @@ -125,10 +109,6 @@ const routes = [ name: 'VoiceResourceManagement', component: function () { return import('../views/VoiceResourceManagement.vue') - }, - meta: { - requiresAuth: true, - title: '音色资源开通' } }, { @@ -136,10 +116,6 @@ const routes = [ name: 'VoiceCloneManagement', component: function () { return import('../views/VoiceCloneManagement.vue') - }, - meta: { - requiresAuth: true, - title: '音色克隆管理' } }, { @@ -178,10 +154,6 @@ const routes = [ name: 'FeatureManagement', component: function () { return import('../views/FeatureManagement.vue') - }, - meta: { - requiresAuth: true, - title: '功能配置' } }, // 替换词管理 @@ -190,10 +162,6 @@ const routes = [ name: 'ReplacementWordManagement', component: function () { return import('../views/ReplacementWordManagement.vue') - }, - meta: { - requiresAuth: true, - title: '替换词管理' } }, // 通讯录管理页面路由 @@ -202,10 +170,6 @@ const routes = [ name: 'AddressBookManagement', component: function () { return import('../views/AddressBookManagement.vue') - }, - meta: { - requiresAuth: true, - title: '通讯录管理' } }, ] @@ -228,20 +192,20 @@ VueRouter.prototype.push = function push(location) { }) } -// 需要登录才能访问的路由 -const protectedRoutes = ['home', 'RoleConfig', 'DeviceManagement', 'UserManagement', 'ModelConfig', 'KnowledgeBaseManagement', 'KnowledgeFileUpload', 'AddressBookManagement'] +// 匿名可访问的路由白名单 +const anonRouteNames = new Set(['login', 'Register', 'RetrievePassword', 'welcome']) // 路由守卫 router.beforeEach((to, from, next) => { - // 检查是否是需要保护的路由 - if (protectedRoutes.includes(to.name)) { - // 从localStorage获取token - const token = localStorage.getItem('token') - if (!token) { - // 未登录,跳转到登录页 - next({ name: 'login', query: { redirect: to.fullPath } }) - return - } + if (anonRouteNames.has(to.name)) { + next() + return + } + // 非白名单路由必须登录 + const token = localStorage.getItem('token') + if (!token) { + next({ name: 'login' }) + return } next() })