import { createRouter, createWebHashHistory } from 'vue-router' import { getToken } from '@/utils/storage' /** * 路由配置 * meta.requiresAuth = true 需要登录才能访问 * meta.requiresAuth = false 不需要登录 */ const routes = [ { path: '/login', name: 'Login', component: () => import('@/views/login/index.vue'), meta: { requiresAuth: false, title: '登录' } }, { path: '/', component: () => import('@/layout/index.vue'), meta: { requiresAuth: true }, redirect: '/user/app', children: [ { path: 'user/app', name: 'AppUser', component: () => import('@/views/user/app-user/index.vue'), meta: { requiresAuth: true, title: '账号管理', icon: 'User', hidden: true } }, { path: 'user', name: 'User', meta: { requiresAuth: true, title: '用户管理', icon: 'UserFilled' }, redirect: '/user/profile', children: [ { path: 'profile', name: 'UserProfile', component: () => import('@/views/user/profile/index.vue'), meta: { requiresAuth: true, title: '用户列表', icon: 'User' } }, { path: 'plan', name: 'UserPlan', component: () => import('@/views/user/plan/index.vue'), meta: { requiresAuth: true, title: '用户方案管理', icon: 'Document' } } ] }, { path: 'user/device', name: 'UserDevice', component: () => import('@/views/user/device/index.vue'), meta: { requiresAuth: true, title: '用户设备管理', icon: 'Monitor', hidden: true } }, { path: 'acupoint', name: 'Acupoint', component: () => import('@/views/acupoint/index.vue'), meta: { requiresAuth: true, title: '穴位管理', icon: 'FirstAidKit' } }, { path: 'device', name: 'Device', component: () => import('@/views/device/index.vue'), meta: { requiresAuth: true, title: '设备管理', icon: 'Monitor' } }, { path: 'device/users', name: 'DeviceUsers', component: () => import('@/views/device/users.vue'), meta: { requiresAuth: true, title: '设备用户管理', hidden: true } }, { path: 'moxibustion', name: 'Moxibustion', component: () => import('@/views/moxibustion/index.vue'), meta: { requiresAuth: true, title: '艾灸手法管理', icon: 'Promotion' } }, { path: 'plan', name: 'Plan', component: () => import('@/views/plan/index.vue'), meta: { requiresAuth: true, title: '方案管理', icon: 'Notebook' } }, { path: 'simulation', name: 'Simulation', component: () => import('@/views/simulation/index.vue'), meta: { requiresAuth: true, title: '方案模拟测试', icon: 'DataAnalysis' } }, { path: 'content', name: 'Content', component: () => import('@/views/content/index.vue'), meta: { requiresAuth: true, title: '内容管理', icon: 'Document' } }, { path: 'system', name: 'System', meta: { requiresAuth: true, title: '系统管理', icon: 'Setting' }, redirect: '/system/admin', children: [ { path: 'admin', name: 'Admin', component: () => import('@/views/admin/index.vue'), meta: { requiresAuth: true, title: '管理员管理', icon: 'Avatar' } }, { path: 'user-category', name: 'UserCategory', component: () => import('@/views/system/user-category/index.vue'), meta: { requiresAuth: true, title: '用户类别', icon: 'Collection' } }, { path: 'log', name: 'Log', component: () => import('@/views/log/index.vue'), meta: { requiresAuth: true, title: '系统日志', icon: 'Document' } } ] } ] }, { path: '/404', name: '404', component: () => import('@/views/404.vue'), meta: { requiresAuth: false, title: '页面不存在' } }, // 未匹配到的路由重定向到 404 { path: '/:pathMatch(.*)*', redirect: '/404' } ] const router = createRouter({ history: createWebHashHistory(), routes }) // 全局路由守卫 router.beforeEach((to, from, next) => { // 设置页面标题 if (to.meta?.title) { document.title = `${to.meta.title} - 艾灸椅管理后台` } const token = getToken() const requiresAuth = to.meta?.requiresAuth !== false // 默认需要登录 if (requiresAuth && !token) { // 需要登录但未登录,跳转到登录页 next({ path: '/login', query: { redirect: to.fullPath } }) } else if (to.path === '/login' && token) { // 已登录用户访问登录页,跳转到首页 next('/') } else { next() } }) export default router