index.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router';
  2. import { app_ready, app_routes } from '~/store/app';
  3. import { RouteRecordDetailRaw } from './routes.d';
  4. // import localRoutes, { title } from './routes/xdjx';
  5. import localRoutes, { title } from './routes/xdjy';
  6. // import localRoutes, { title } from './routes/xdhq';
  7. document.title = title
  8. // !auto switch routes way 1:
  9. // !build error
  10. // const localRoutes = (await import(`./routes/${import.meta.env.MODE}.ts`))['default'] as RouteRecordDetailRaw[]
  11. // const currentRouteModule = import.meta.glob(`./routes/*.ts`, { eager: true })[`./routes/${import.meta.env.MODE}.ts`] as { title: string, default: RouteRecordDetailRaw[] }
  12. // document.title = currentRouteModule['title']
  13. // const localRoutes = currentRouteModule['default']
  14. console.log('localRoutes :>> ', localRoutes);
  15. const router = createRouter({
  16. history: createWebHashHistory(),
  17. routes: [
  18. {
  19. path: '/',
  20. component: () => import("~/layout/loading/index.vue")
  21. },
  22. {
  23. path: '/routes',
  24. name: '-ROUTES',
  25. props: true,
  26. meta: {
  27. hidden: false,
  28. sort: -1,
  29. title: "组织机构与权限"
  30. },
  31. component: () => import("~/router/routes.vue")
  32. },
  33. {
  34. path: '/xxzx',
  35. name: 'xxzx',
  36. props: true,
  37. meta: {
  38. hidden: true,
  39. sort: -1,
  40. title: "消息中心"
  41. },
  42. component: () => import("~/pages/[...]/xxzx/index.vue")
  43. }
  44. ]
  45. })
  46. export default router
  47. const debug = localStorage.getItem("debug");
  48. const whiteList = ['/xxzx']
  49. async function handleFilterRoutes<R extends RouteRecordDetailRaw[]>(routes: R): Promise<Partial<R>> {
  50. console.log(' handleFilterRoutes ');
  51. function getAppRoutes(routes: any[]) {
  52. if (debug === '1') { return routes }
  53. else {
  54. const hash = location.hash
  55. // '#/dddx/ddrwfk' => '/dddx'
  56. const first = '/' + hash.split('/')[1]
  57. console.log('getAppRoutes :>> ', first, routes.filter(route => route.path === first));
  58. return routes.filter(route => route.path === first || whiteList.includes(route.path))
  59. }
  60. }
  61. // await new Promise((resolve) => {
  62. // setTimeout(() => {
  63. // resolve(getAppRoutes(routes))
  64. // }, 0);
  65. // })
  66. return getAppRoutes(routes) as Partial<R>
  67. }
  68. const routes = await handleFilterRoutes(localRoutes)
  69. sessionStorage.setItem('routes', JSON.stringify(routes.concat([
  70. // {
  71. // path: '/routes',
  72. // name: '-ROUTES',
  73. // meta: {
  74. // hidden: false,
  75. // sort: -1,
  76. // title: "组织机构与权限"
  77. // },
  78. // component: () => import("~/router/routes.vue")
  79. // }
  80. ])))
  81. routes.forEach(filteredRoute => router.addRoute(filteredRoute as RouteRecordRaw))
  82. app_ready.value = true
  83. app_routes.value = (routes as RouteRecordDetailRaw[])
  84. router.beforeEach((to, from) => {
  85. console.groupCollapsed(`%c${from.name?.toString()} => ${to.name?.toString()}`, 'color:#0ff');
  86. console.log(`%c${from.meta.title} => ${to.meta.title}`, 'color:#0cc');
  87. console.groupEnd();
  88. })