index.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { createWebHashHistory, RouteRecordRaw } from 'vue-router'
  2. import { useLoginStore, useRouterStore, i18n } from '@/stores'
  3. import dynamicRouter from './dynamicRouter'
  4. import historyRouter from './historyRouter'
  5. import service from '@/services'
  6. const loginStore = useLoginStore()
  7. const routerStore = useRouterStore()
  8. // 获取首页地址
  9. const getHomeUrl = () => {
  10. const findNode = (tree: Model.MenusRsp[]): string => {
  11. for (const node of tree) {
  12. if (node.children?.length) {
  13. const res = findNode(node.children)
  14. if (res) return res
  15. } else {
  16. return node.url
  17. }
  18. }
  19. return '/404'
  20. }
  21. return findNode(routerStore.filterMenus(routerStore.userRoutes))
  22. }
  23. const routes: Array<RouteRecordRaw> = [
  24. {
  25. path: '/',
  26. redirect: () => loginStore.token ? getHomeUrl() : '/login', // 重定向到默认页面
  27. },
  28. {
  29. path: '/login',
  30. name: 'login',
  31. component: () => import('../views/auth/login/index.vue'),
  32. meta: {
  33. title: '登录',
  34. keepAlive: false
  35. },
  36. },
  37. {
  38. path: '/boot',
  39. name: 'boot',
  40. component: () => import('../views/boot/index.vue'),
  41. meta: {
  42. title: '初始化',
  43. keepAlive: false
  44. },
  45. }
  46. ]
  47. // 开发环境
  48. if (process.env.NODE_ENV === 'development') {
  49. routes.push({
  50. path: '/router',
  51. name: 'router',
  52. component: () => import('../components/layouts/page/index.vue'),
  53. meta: {
  54. title: '路由管理',
  55. keepAlive: true
  56. },
  57. children: [
  58. {
  59. path: 'menu',
  60. name: 'router_menu',
  61. component: () => import('../views/system/menu/index.vue'),
  62. meta: {
  63. title: '菜单管理',
  64. keepAlive: true
  65. }
  66. }
  67. ]
  68. })
  69. }
  70. const router = historyRouter.create({
  71. history: createWebHashHistory(),
  72. routes
  73. })
  74. // 路由跳转拦截
  75. router.beforeEach((to, from, next) => {
  76. const isLoginOrRegister = to.name === 'login' || to.name === 'register'
  77. document.title = i18n.global.t('app.name')
  78. // 判断服务是否加载完成
  79. if (service.isReady) {
  80. if (loginStore.token) {
  81. if (dynamicRouter.isReady) {
  82. if (isLoginOrRegister) {
  83. next('/')
  84. } else {
  85. next()
  86. }
  87. } else {
  88. // 注册动态路由
  89. dynamicRouter.registerRoutes()
  90. next({ ...to, replace: true })
  91. }
  92. } else {
  93. if (isLoginOrRegister) {
  94. next()
  95. } else {
  96. next({
  97. name: 'login',
  98. query: { redirect: to.fullPath },
  99. })
  100. }
  101. }
  102. } else {
  103. if (to.name === 'boot' || to.name === 'login') {
  104. next()
  105. } else {
  106. next({
  107. name: 'boot',
  108. query: { redirect: to.fullPath },
  109. })
  110. }
  111. }
  112. })
  113. export default router