超级管理员登录设置

This commit is contained in:
dxc
2026-02-04 13:30:07 +08:00
parent 4aa43a0607
commit 13590b1fac
21 changed files with 881 additions and 104 deletions

View File

@ -1,9 +1,18 @@
import { createRouter, createWebHistory } from 'vue-router'
// 核心修改点:使用 'type' 关键字导入 RouteRecordRaw,或者将其分开导入
// 使用 'type' 关键字导入 RouteRecordRaw
import type { RouteRecordRaw } from 'vue-router'
import Layout from '@/layout/index.vue'
import { useUserStore } from '@/stores/user' // [新增] 引入 Store 用于权限判断
const routes: Array<RouteRecordRaw> = [
// [新增] 登录页 (不需要 Layout)
{
path: '/login',
name: 'Login',
component: () => import('@/views/login/index.vue'),
meta: { hidden: true } // 不在侧边栏显示
},
// 1. 首页 Dashboard
{
path: '/',
@ -104,7 +113,35 @@ const routes: Array<RouteRecordRaw> = [
]
},
/* * 暂时屏蔽 BOM 和 系统管理
// 5. [修改] 系统管理 (权限控制 + 用户创建)
{
path: '/system',
component: Layout,
meta: {
title: '系统管理',
icon: 'Setting',
// 只有超级管理员和主管能看到此菜单
roles: ['super_admin', 'supervisor']
},
children: [
{
path: 'user-create',
name: 'UserCreate',
// 指向我们之前创建的新增用户页面
component: () => import('@/views/system/UserCreate.vue'),
meta: { title: '账号开通', icon: 'User' }
},
// 原有的日志页面保留 (如果文件存在)
// {
// path: 'log',
// name: 'OpLog',
// component: () => import('@/views/system/log.vue'),
// meta: { title: '操作日志', icon: 'Document' }
// }
]
},
/* * 暂时屏蔽 BOM
*/
// {
// path: '/bom',
@ -118,25 +155,6 @@ const routes: Array<RouteRecordRaw> = [
// }
// ]
// },
// {
// path: '/system',
// component: Layout,
// meta: { title: '系统管理', icon: 'Setting' },
// children: [
// {
// path: 'user',
// name: 'UserManage',
// component: () => import('@/views/system/user.vue'),
// meta: { title: '用户管理', icon: 'User' }
// },
// {
// path: 'log',
// name: 'OpLog',
// component: () => import('@/views/system/log.vue'),
// meta: { title: '操作日志', icon: 'Document' }
// }
// ]
// },
// 404 路由
{
@ -151,4 +169,40 @@ const router = createRouter({
routes
})
// ==========================================
// [新增] 全局路由守卫:处理登录拦截与权限验证
// ==========================================
router.beforeEach((to, from, next) => {
const userStore = useUserStore()
const token = userStore.token
const userRole = userStore.role
// 1. 白名单:如果是去登录页,直接放行
if (to.path === '/login') {
next()
return
}
// 2. 无 Token强制跳转登录页
if (!token) {
next('/login')
return
}
// 3. 权限判断:检查 meta.roles
if (to.meta.roles && Array.isArray(to.meta.roles)) {
// 如果当前用户角色在允许列表中,放行
if (to.meta.roles.includes(userRole)) {
next()
} else {
// 权限不足,重定向到首页或 403 页面 (这里简单跳回 dashboard)
// 可以在这里触发一个 Element Plus 的 Message 提示
next('/dashboard')
}
} else {
// 没有定义权限要求的页面,默认放行
next()
}
})
export default router