import { createRouter, createWebHistory } from 'vue-router' const routes = [ { path: '/', redirect: '/index', }, { path: '/index', component: () => import('@/pages/Index/Index.vue'), children: [ { path: '/index', redirect: '/index/regular', }, { path: 'regular', //常规 component: () => import('@/pages/Index/Regular.vue'), children: [ { path: '/index/regular', redirect: '/index/regular/consumables', }, { path: 'consumables', //耗材, component: () => import('@/pages/Index/Regular/Consumables.vue'), }, { path: 'test-tube', //试管, component: () => import('@/pages/Index/Regular/TestTube.vue'), }, { path: 'running', //运行 component: () => import('@/pages/Index/Regular/Running.vue'), }, ], }, { path: 'history', component: () => import('@/pages/Index/History.vue'), }, { path: 'setting', component: () => import('@/pages/Index/Setting.vue'), meta: { requiresAuth: true, requiresAdmin: true }, // 需要登录和 Admin 权限 children: [ { path: '/index/setting', redirect: '/index/setting/device', }, { path: 'device', //设备 component: () => import('@/pages/Index/Settings/Device.vue'), }, { path: 'users', //用户管理 component: () => import('@/pages/Index/Settings/Users.vue'), }, { path: 'version', //版本管理 component: () => import('@/pages/Index/Settings/Version.vue'), }, { path: 'lis', //LIS配置 component: () => import('@/pages/Index/Settings/Lis.vue'), }, ], }, { path: 'emergency', //添加急诊页面 component: () => import('@/pages/Index/Regular/Emergency.vue'), }, { path: 'change-user', //编辑患者页面 component: () => import('@/pages/Index/TestTube/ChangeUser.vue'), }, ], }, { path: '/login', component: () => import('@/pages/Login/Login.vue'), }, //匹配任何找不到的路由,404兜底 { path: '/notFound', component: () => import('@/pages/NotFound/NotFound.vue'), }, ] const router = createRouter({ history: createWebHistory(), routes, }) function getCurrentUser() { const token = JSON.parse(sessionStorage.getItem('token')!) return token } // 路由守卫,检查本地 Token 是否存在 // router.beforeEach((to, from, next) => { // console.log(from) // const token = sessionStorage.getItem('token') // if (!token && to.path !== '/login') { // next('/login') // 没有 token,重定向到登录页 // } else if (token && to.path === '/login') { // next('/index') // 已有 token 且请求登录页,则跳转到主页 // } else { // next() // 通过 // } // }) // 在 router/index.ts 中添加全局路由守卫 // router.beforeEach((to, from, next) => { // console.log(from) // const user = getCurrentUser() // 获取当前用户 // console.log(user) // // 判断目标路由是否需要权限检查 // if (to.matched.some((record) => record.meta.requiresAuth)) { // // 如果没有登录,重定向到登录页面 // if (!user) { // return next('/login') // 保存当前访问的路径,用于登录后重定向 // } // // 判断是否需要 Admin 权限 // if ( // to.matched.some((record) => record.meta.requiresAdmin) && // user.usrRole !== 'Admin' // ) { // return next({ path: '/notFound' }) // 权限不足,重定向到首页或者其他页面 // } // } // // 如果没有问题,继续导航 // next() // }) export default router