index.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { createRouter, createWebHashHistory } from 'vue-router'
  2. import { getToken } from '@/utils/storage'
  3. /**
  4. * 路由配置
  5. * meta.requiresAuth = true 需要登录才能访问
  6. * meta.requiresAuth = false 不需要登录
  7. */
  8. const routes = [
  9. {
  10. path: '/login',
  11. name: 'Login',
  12. component: () => import('@/views/login/index.vue'),
  13. meta: { requiresAuth: false, title: '登录' }
  14. },
  15. {
  16. path: '/',
  17. component: () => import('@/layout/index.vue'),
  18. meta: { requiresAuth: true },
  19. redirect: '/user/app',
  20. children: [
  21. {
  22. path: 'user/app',
  23. name: 'AppUser',
  24. component: () => import('@/views/user/app-user/index.vue'),
  25. meta: { requiresAuth: true, title: '账号管理', icon: 'User', hidden: true }
  26. },
  27. {
  28. path: 'user',
  29. name: 'User',
  30. meta: { requiresAuth: true, title: '用户管理', icon: 'UserFilled' },
  31. redirect: '/user/profile',
  32. children: [
  33. {
  34. path: 'profile',
  35. name: 'UserProfile',
  36. component: () => import('@/views/user/profile/index.vue'),
  37. meta: { requiresAuth: true, title: '用户列表', icon: 'User' }
  38. },
  39. {
  40. path: 'plan',
  41. name: 'UserPlan',
  42. component: () => import('@/views/user/plan/index.vue'),
  43. meta: { requiresAuth: true, title: '用户方案管理', icon: 'Document' }
  44. }
  45. ]
  46. },
  47. {
  48. path: 'user/device',
  49. name: 'UserDevice',
  50. component: () => import('@/views/user/device/index.vue'),
  51. meta: { requiresAuth: true, title: '用户设备管理', icon: 'Monitor', hidden: true }
  52. },
  53. {
  54. path: 'acupoint',
  55. name: 'Acupoint',
  56. component: () => import('@/views/acupoint/index.vue'),
  57. meta: { requiresAuth: true, title: '穴位管理', icon: 'FirstAidKit' }
  58. },
  59. {
  60. path: 'device',
  61. name: 'Device',
  62. component: () => import('@/views/device/index.vue'),
  63. meta: { requiresAuth: true, title: '设备管理', icon: 'Monitor' }
  64. },
  65. {
  66. path: 'device/users',
  67. name: 'DeviceUsers',
  68. component: () => import('@/views/device/users.vue'),
  69. meta: { requiresAuth: true, title: '设备用户管理', hidden: true }
  70. },
  71. {
  72. path: 'moxibustion',
  73. name: 'Moxibustion',
  74. component: () => import('@/views/moxibustion/index.vue'),
  75. meta: { requiresAuth: true, title: '艾灸手法管理', icon: 'Promotion' }
  76. },
  77. {
  78. path: 'plan',
  79. name: 'Plan',
  80. component: () => import('@/views/plan/index.vue'),
  81. meta: { requiresAuth: true, title: '方案管理', icon: 'Notebook' }
  82. },
  83. {
  84. path: 'simulation',
  85. name: 'Simulation',
  86. component: () => import('@/views/simulation/index.vue'),
  87. meta: { requiresAuth: true, title: '方案模拟测试', icon: 'DataAnalysis' }
  88. },
  89. {
  90. path: 'content',
  91. name: 'Content',
  92. component: () => import('@/views/content/index.vue'),
  93. meta: { requiresAuth: true, title: '内容管理', icon: 'Document' }
  94. },
  95. {
  96. path: 'system',
  97. name: 'System',
  98. meta: { requiresAuth: true, title: '系统管理', icon: 'Setting' },
  99. redirect: '/system/admin',
  100. children: [
  101. {
  102. path: 'admin',
  103. name: 'Admin',
  104. component: () => import('@/views/admin/index.vue'),
  105. meta: { requiresAuth: true, title: '管理员管理', icon: 'Avatar' }
  106. },
  107. {
  108. path: 'user-category',
  109. name: 'UserCategory',
  110. component: () => import('@/views/system/user-category/index.vue'),
  111. meta: { requiresAuth: true, title: '用户类别', icon: 'Collection' }
  112. },
  113. {
  114. path: 'log',
  115. name: 'Log',
  116. component: () => import('@/views/log/index.vue'),
  117. meta: { requiresAuth: true, title: '系统日志', icon: 'Document' }
  118. }
  119. ]
  120. }
  121. ]
  122. },
  123. {
  124. path: '/404',
  125. name: '404',
  126. component: () => import('@/views/404.vue'),
  127. meta: { requiresAuth: false, title: '页面不存在' }
  128. },
  129. // 未匹配到的路由重定向到 404
  130. {
  131. path: '/:pathMatch(.*)*',
  132. redirect: '/404'
  133. }
  134. ]
  135. const router = createRouter({
  136. history: createWebHashHistory(),
  137. routes
  138. })
  139. // 全局路由守卫
  140. router.beforeEach((to, from, next) => {
  141. // 设置页面标题
  142. if (to.meta?.title) {
  143. document.title = `${to.meta.title} - 艾灸椅管理后台`
  144. }
  145. const token = getToken()
  146. const requiresAuth = to.meta?.requiresAuth !== false // 默认需要登录
  147. if (requiresAuth && !token) {
  148. // 需要登录但未登录,跳转到登录页
  149. next({ path: '/login', query: { redirect: to.fullPath } })
  150. } else if (to.path === '/login' && token) {
  151. // 已登录用户访问登录页,跳转到首页
  152. next('/')
  153. } else {
  154. next()
  155. }
  156. })
  157. export default router