index.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { createRouter, createWebHashHistory, Router } from 'vue-router'
  2. import Layout from '@/layout'
  3. import { RouterTy } from '~/router'
  4. import routesOne from './routes/routes-file-one'
  5. import routesTwo from './routes/routes-file-two'
  6. import routesThree from './routes/routes-file-three'
  7. import routesFour from './routes/routes-file-four'
  8. import routesFive from './routes/routes-file-five'
  9. import routesSix from './routes/routes-file-six'
  10. import { Page, useKeepAlive } from '@/store/keepAlive'
  11. const routesPush = (arr, routes) => {
  12. if (!Array.isArray(arr)) return '请传入数组格式路由表'
  13. for (let i = 0; i < arr.length; i++) {
  14. routes.push(arr[i])
  15. }
  16. }
  17. export const constantRoutes: RouterTy = [
  18. {
  19. path: '/redirect',
  20. component: Layout,
  21. hidden: true,
  22. children: [
  23. {
  24. path: '/redirect/:path(.*)',
  25. component: () => import('@/views/redirect'),
  26. },
  27. ],
  28. },
  29. {
  30. path: '/login',
  31. component: () => import('@/views/login/Login.vue'),
  32. hidden: true,
  33. },
  34. {
  35. path: '/404',
  36. component: () => import('@/views/error-page/404.vue'),
  37. hidden: true,
  38. },
  39. {
  40. path: '/401',
  41. component: () => import('@/views/error-page/401.vue'),
  42. hidden: true,
  43. },
  44. ]
  45. /**
  46. * asyncRoutes
  47. * the routes that need to be dynamically loaded based on user roles
  48. */
  49. export const asyncRoutes: RouterTy = [
  50. // 404 page must be placed at the end !!!
  51. // using pathMatch install of "*" in vue-router 4.0
  52. { path: '/:pathMatch(.*)', redirect: '/404', hidden: true },
  53. ]
  54. // 插入路由
  55. routesPush(
  56. [
  57. ...routesOne,
  58. ...routesSix,
  59. ...routesFive,
  60. ...routesTwo,
  61. ...routesThree,
  62. ...routesFour,
  63. ],
  64. asyncRoutes
  65. )
  66. const router: Router = createRouter({
  67. history: createWebHashHistory(),
  68. scrollBehavior: () => ({ top: 0 }),
  69. routes: constantRoutes,
  70. })
  71. router.beforeEach((to, from, next) => {
  72. const { savedPages, savePage } = useKeepAlive()
  73. if (from.matched?.[0]?.name === 'RealTime') {
  74. if (
  75. typeof from.name === 'string' &&
  76. ((from.name.includes('FlightView') &&
  77. from.query.flightNO &&
  78. from.query.flightDate) ||
  79. (from.name.includes('WaybillView') && from.query.waybillNO))
  80. ) {
  81. savePage(from as Page)
  82. }
  83. }
  84. if (to.matched?.[0]?.name === 'RealTime') {
  85. if (
  86. typeof to.name === 'string' &&
  87. ((to.name.includes('FlightView') &&
  88. (!to.query.flightNO || !to.query.flightDate)) ||
  89. (to.name.includes('WaybillView') && !to.query.waybillNO))
  90. ) {
  91. const savedPage = savedPages.find(savedPage => savedPage.name === to.name)
  92. if (savedPage) {
  93. next(savedPage.fullPath)
  94. }
  95. }
  96. }
  97. next()
  98. })
  99. // export function resetRouter() {
  100. // const newRouter = createRouter({
  101. // history: createWebHashHistory(),
  102. // scrollBehavior: () => ({ top: 0 }),
  103. // routes: constantRoutes
  104. // })
  105. // }
  106. export default router