fix(router): 路由守卫改 anon 白名单反向匹配

业务路由越加越多,原来硬编码的 8 项守卫名单跟不上了,
未登录直接进十来个业务页看到骨架再被踢,体感抖。

守卫改成只对登录、注册、找回密码、根路径这四页放行,其余一律查 token。
顺手把 9 处没人读的 meta.requiresAuth 和 meta.title 死字段删了。
This commit is contained in:
hrz 2026-10-12 18:02:36 +08:00
parent 4576e391bb
commit 4aa63baf75

View file

@ -81,10 +81,6 @@ const routes = [
name: 'ParamsManagement', name: 'ParamsManagement',
component: function () { component: function () {
return import('../views/ParamsManagement.vue') return import('../views/ParamsManagement.vue')
},
meta: {
requiresAuth: true,
title: '参数管理'
} }
}, },
{ {
@ -92,10 +88,6 @@ const routes = [
name: 'KnowledgeBaseManagement', name: 'KnowledgeBaseManagement',
component: function () { component: function () {
return import('../views/KnowledgeBaseManagement.vue') return import('../views/KnowledgeBaseManagement.vue')
},
meta: {
requiresAuth: true,
title: '知识库管理'
} }
}, },
{ {
@ -103,10 +95,6 @@ const routes = [
name: 'ServerSideManager', name: 'ServerSideManager',
component: function () { component: function () {
return import('../views/ServerSideManager.vue') return import('../views/ServerSideManager.vue')
},
meta: {
requiresAuth: true,
title: '服务端管理'
} }
}, },
{ {
@ -114,10 +102,6 @@ const routes = [
name: 'OtaManagement', name: 'OtaManagement',
component: function () { component: function () {
return import('../views/OtaManagement.vue') return import('../views/OtaManagement.vue')
},
meta: {
requiresAuth: true,
title: 'OTA管理'
} }
}, },
{ {
@ -125,10 +109,6 @@ const routes = [
name: 'VoiceResourceManagement', name: 'VoiceResourceManagement',
component: function () { component: function () {
return import('../views/VoiceResourceManagement.vue') return import('../views/VoiceResourceManagement.vue')
},
meta: {
requiresAuth: true,
title: '音色资源开通'
} }
}, },
{ {
@ -136,10 +116,6 @@ const routes = [
name: 'VoiceCloneManagement', name: 'VoiceCloneManagement',
component: function () { component: function () {
return import('../views/VoiceCloneManagement.vue') return import('../views/VoiceCloneManagement.vue')
},
meta: {
requiresAuth: true,
title: '音色克隆管理'
} }
}, },
{ {
@ -178,10 +154,6 @@ const routes = [
name: 'FeatureManagement', name: 'FeatureManagement',
component: function () { component: function () {
return import('../views/FeatureManagement.vue') return import('../views/FeatureManagement.vue')
},
meta: {
requiresAuth: true,
title: '功能配置'
} }
}, },
// 替换词管理 // 替换词管理
@ -190,10 +162,6 @@ const routes = [
name: 'ReplacementWordManagement', name: 'ReplacementWordManagement',
component: function () { component: function () {
return import('../views/ReplacementWordManagement.vue') return import('../views/ReplacementWordManagement.vue')
},
meta: {
requiresAuth: true,
title: '替换词管理'
} }
}, },
// 通讯录管理页面路由 // 通讯录管理页面路由
@ -202,10 +170,6 @@ const routes = [
name: 'AddressBookManagement', name: 'AddressBookManagement',
component: function () { component: function () {
return import('../views/AddressBookManagement.vue') 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) => { router.beforeEach((to, from, next) => {
// 检查是否是需要保护的路由 if (anonRouteNames.has(to.name)) {
if (protectedRoutes.includes(to.name)) { next()
// 从localStorage获取token return
const token = localStorage.getItem('token') }
if (!token) { // 非白名单路由必须登录
// 未登录,跳转到登录页 const token = localStorage.getItem('token')
next({ name: 'login', query: { redirect: to.fullPath } }) if (!token) {
return next({ name: 'login' })
} return
} }
next() next()
}) })