优化代码,调整权限设置( 非管理员打开提示拒绝访问并自动跳转到首页),修复bug;

This commit is contained in:
LiJinHui
2025-09-19 15:34:43 +08:00
parent 09d5d37867
commit 5fcc7592c9
7 changed files with 103 additions and 297 deletions
+15 -2
View File
@@ -168,12 +168,15 @@ VueRouter.prototype.push = function push(location) {
}
// 需要登录才能访问的路由
const protectedRoutes = ['home', 'RoleConfig', 'DeviceManagement', 'UserManagement', 'ModelConfig', 'TemplateQuickConfig']
const protectedRoutes = ['home', 'RoleConfig', 'DeviceManagement', 'UserManagement', 'ModelConfig']
// 需要管理员权限才能访问的路由
const adminRoutes = ['TemplateQuickConfig', 'AgentTemplateManagement']
// 路由守卫
router.beforeEach((to, from, next) => {
// 检查是否是需要保护的路由
if (protectedRoutes.includes(to.name)) {
if (protectedRoutes.includes(to.name) || adminRoutes.includes(to.name)) {
// 从localStorage获取token
const token = localStorage.getItem('token')
if (!token) {
@@ -181,6 +184,16 @@ router.beforeEach((to, from, next) => {
next({ name: 'login', query: { redirect: to.fullPath } })
return
}
// 对于需要管理员权限的路由,检查用户是否是超级管理员
if (adminRoutes.includes(to.name)) {
const isSuperAdmin = localStorage.getItem('isSuperAdmin') === 'true'
if (!isSuperAdmin) {
// 不是管理员,跳转到首页或显示无权限提示
next({ name: 'home' })
return
}
}
}
next()
})