修改登录退出逻辑
This commit is contained in:
@ -1,19 +1,18 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
// 使用 'type' 关键字导入 RouteRecordRaw
|
||||
import type { RouteRecordRaw } from 'vue-router'
|
||||
import Layout from '@/layout/index.vue'
|
||||
import { useUserStore } from '@/stores/user' // [新增] 引入 Store 用于权限判断
|
||||
import { useUserStore } from '@/stores/user'
|
||||
|
||||
const routes: Array<RouteRecordRaw> = [
|
||||
// [新增] 登录页 (不需要 Layout)
|
||||
// 1. 登录页
|
||||
{
|
||||
path: '/login',
|
||||
name: 'Login',
|
||||
component: () => import('@/views/login/index.vue'),
|
||||
meta: { hidden: true } // 不在侧边栏显示
|
||||
meta: { hidden: true }
|
||||
},
|
||||
|
||||
// 1. 首页 Dashboard
|
||||
// 2. 首页 Dashboard
|
||||
{
|
||||
path: '/',
|
||||
component: Layout,
|
||||
@ -28,7 +27,7 @@ const routes: Array<RouteRecordRaw> = [
|
||||
]
|
||||
},
|
||||
|
||||
// 2. 基础信息 (对应 views/material/list.vue)
|
||||
// 3. 基础信息
|
||||
{
|
||||
path: '/material',
|
||||
component: Layout,
|
||||
@ -37,14 +36,13 @@ const routes: Array<RouteRecordRaw> = [
|
||||
{
|
||||
path: 'index',
|
||||
name: 'MaterialBase',
|
||||
// 基础信息列表
|
||||
component: () => import('@/views/material/list.vue'),
|
||||
meta: { title: '基础信息', icon: 'Box' }
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
// 3. 库存管理 (采购/半成品/成品/权益)
|
||||
// 4. 库存管理
|
||||
{
|
||||
path: '/inventory',
|
||||
component: Layout,
|
||||
@ -54,35 +52,31 @@ const routes: Array<RouteRecordRaw> = [
|
||||
{
|
||||
path: 'buy',
|
||||
name: 'InventoryBuy',
|
||||
// 采购入库页面
|
||||
component: () => import('@/views/stock/inbound/buy.vue'),
|
||||
meta: { title: '采购件' }
|
||||
},
|
||||
{
|
||||
path: 'semi',
|
||||
name: 'InventorySemi',
|
||||
// 半成品页面
|
||||
component: () => import('@/views/stock/inbound/semi.vue'),
|
||||
meta: { title: '半成品' }
|
||||
},
|
||||
{
|
||||
path: 'product',
|
||||
name: 'InventoryProduct',
|
||||
// 成品页面
|
||||
component: () => import('@/views/stock/inbound/product.vue'),
|
||||
meta: { title: '成品' }
|
||||
},
|
||||
{
|
||||
path: 'service',
|
||||
name: 'InventoryService',
|
||||
// 服务权益页面
|
||||
component: () => import('@/views/stock/inbound/service.vue'),
|
||||
meta: { title: '服务权益' }
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
// 4. 业务操作 (借库/维修/报废)
|
||||
// 5. 业务操作
|
||||
{
|
||||
path: '/operation',
|
||||
component: Layout,
|
||||
@ -92,70 +86,43 @@ const routes: Array<RouteRecordRaw> = [
|
||||
{
|
||||
path: 'borrow',
|
||||
name: 'OpBorrow',
|
||||
// 借库页面
|
||||
component: () => import('@/views/transaction/borrow.vue'),
|
||||
meta: { title: '借库' }
|
||||
},
|
||||
{
|
||||
path: 'repair',
|
||||
name: 'OpRepair',
|
||||
// 维修页面 (指向 return.vue)
|
||||
component: () => import('@/views/transaction/return.vue'),
|
||||
meta: { title: '维修' }
|
||||
},
|
||||
{
|
||||
path: 'scrap',
|
||||
name: 'OpScrap',
|
||||
// 报废页面
|
||||
component: () => import('@/views/transaction/scrap.vue'),
|
||||
meta: { title: '报废' }
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
// 5. [修改] 系统管理 (权限控制 + 用户创建)
|
||||
// 6. 系统管理
|
||||
{
|
||||
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',
|
||||
// component: Layout,
|
||||
// children: [
|
||||
// {
|
||||
// path: 'index',
|
||||
// name: 'BOM',
|
||||
// component: () => import('@/views/bom/index.vue'),
|
||||
// meta: { title: 'BOM管理', icon: 'List' }
|
||||
// }
|
||||
// ]
|
||||
// },
|
||||
|
||||
// 404 路由
|
||||
{
|
||||
path: '/:pathMatch(.*)*',
|
||||
@ -170,37 +137,45 @@ const router = createRouter({
|
||||
})
|
||||
|
||||
// ==========================================
|
||||
// [新增] 全局路由守卫:处理登录拦截与权限验证
|
||||
// [关键修改] 全局路由守卫
|
||||
// ==========================================
|
||||
router.beforeEach((to, from, next) => {
|
||||
const userStore = useUserStore()
|
||||
const token = userStore.token
|
||||
const userRole = userStore.role
|
||||
|
||||
// 1. 白名单:如果是去登录页,直接放行
|
||||
// 1. 实时获取 Token (优先取 store,防止 store 未初始化取 localStorage)
|
||||
const token = userStore.token || localStorage.getItem('token')
|
||||
const userRole = userStore.role || localStorage.getItem('role') || 'user'
|
||||
|
||||
// 2. 如果要去的是登录页
|
||||
if (to.path === '/login') {
|
||||
next()
|
||||
// 如果有 Token,说明已登录,踢回首页 (防止重复登录)
|
||||
if (token) {
|
||||
next('/')
|
||||
} else {
|
||||
// 没有 Token,允许访问登录页
|
||||
next()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 2. 无 Token:强制跳转登录页
|
||||
// 3. 如果去的不是登录页,但没有 Token
|
||||
if (!token) {
|
||||
next('/login')
|
||||
// 强制重定向到登录页
|
||||
// 使用 replace 防止用户点击浏览器“返回”按钮时进入死循环
|
||||
next({ path: '/login', replace: true })
|
||||
return
|
||||
}
|
||||
|
||||
// 3. 权限判断:检查 meta.roles
|
||||
// 4. 权限判断 (已有 Token)
|
||||
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()
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user