index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { createRouter, createWebHashHistory } from 'vue-router'
  2. import { useStore } from '../store'
  3. import LoginPage from '../components/LoginPage.vue'
  4. import MainLayout from '../components/MainLayout.vue'
  5. import Dashboard from '../views/Dashboard.vue'
  6. import RoleManagement from '../views/RoleManagement.vue'
  7. import VoiceManagement from '../views/VoiceManagement.vue'
  8. import RecommendationManagement from '../views/RecommendationManagement.vue'
  9. // 路由守卫
  10. const requireAuth = (to, from, next) => {
  11. const store = useStore()
  12. if (!store.user && to.path !== '/login') {
  13. next('/login')
  14. } else {
  15. next()
  16. }
  17. }
  18. const routes = [
  19. {
  20. path: '/login',
  21. name: 'Login',
  22. component: LoginPage
  23. },
  24. {
  25. path: '/',
  26. component: MainLayout,
  27. beforeEnter: requireAuth,
  28. children: [
  29. {
  30. path: '',
  31. name: 'Dashboard',
  32. component: Dashboard
  33. },
  34. {
  35. path: 'roles',
  36. name: 'RoleManagement',
  37. component: RoleManagement
  38. },
  39. {
  40. path: 'voices',
  41. name: 'VoiceManagement',
  42. component: VoiceManagement
  43. },
  44. {
  45. path: 'recommendations',
  46. name: 'RecommendationManagement',
  47. component: RecommendationManagement
  48. },
  49. {
  50. path: 'settings',
  51. name: 'Settings',
  52. component: () => import('../views/Settings.vue')
  53. }
  54. ]
  55. },
  56. {
  57. path: '/:pathMatch(.*)*',
  58. redirect: '/login'
  59. }
  60. ]
  61. const router = createRouter({
  62. history: createWebHashHistory(),
  63. routes
  64. })
  65. export default router