index copy.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { createRouter, createWebHashHistory } from 'vue-router';
  2. import NProgress from 'nprogress';
  3. import 'nprogress/nprogress.css';
  4. import pinia from '/@/stores/index';
  5. import { storeToRefs } from 'pinia';
  6. import { useKeepALiveNames } from '/@/stores/keepAliveNames';
  7. import { useRoutesList } from '/@/stores/routesList';
  8. import { Session } from '/@/utils/storage';
  9. import { staticRoutes, notFoundAndNoPower } from '/@/router/route';
  10. import { initBackEndControlRoutes } from '/@/router/backEnd';
  11. /**
  12. * 1、前端控制路由时:isRequestRoutes 为 false,需要写 roles,需要走 setFilterRoute 方法。
  13. * 2、后端控制路由时:isRequestRoutes 为 true,不需要写 roles,不需要走 setFilterRoute 方法),
  14. * 相关方法已拆解到对应的 `backEnd.ts` 与 `frontEnd.ts`(他们互不影响,不需要同时改 2 个文件)。
  15. * 特别说明:
  16. * 1、前端控制:路由菜单由前端去写(无菜单管理界面,有角色管理界面),角色管理中有 roles 属性,需返回到 userInfo 中。
  17. * 2、后端控制:路由菜单由后端返回(有菜单管理界面、有角色管理界面)
  18. */
  19. /**
  20. * 创建一个可以被 Vue 应用程序使用的路由实例
  21. * @method createRouter(options: RouterOptions): Router
  22. * @link 参考:https://next.router.vuejs.org/zh/api/#createrouter
  23. */
  24. export const router = createRouter({
  25. history: createWebHashHistory(),
  26. /**
  27. * 说明:
  28. * 1、notFoundAndNoPower 默认添加 404、401 界面,防止一直提示 No match found for location with path 'xxx'
  29. * 2、backEnd.ts(后端控制路由)、frontEnd.ts(前端控制路由) 中也需要加 notFoundAndNoPower 404、401 界面。
  30. * 防止 404、401 不在 layout 布局中,不设置的话,404、401 界面将全屏显示
  31. */
  32. routes: [...notFoundAndNoPower, ...staticRoutes],
  33. });
  34. /**
  35. * 路由多级嵌套数组处理成一维数组
  36. * @param arr 传入路由菜单数据数组
  37. * @returns 返回处理后的一维路由菜单数组
  38. */
  39. export function formatFlatteningRoutes(arr: any) {
  40. if (arr.length <= 0) return false;
  41. for (let i = 0; i < arr.length; i++) {
  42. if (arr[i].children) {
  43. arr = arr.slice(0, i + 1).concat(arr[i].children, arr.slice(i + 1));
  44. }
  45. }
  46. return arr;
  47. }
  48. /**
  49. * 一维数组处理成多级嵌套数组(只保留二级:也就是二级以上全部处理成只有二级,keep-alive 支持二级缓存)
  50. * @description isKeepAlive 处理 `name` 值,进行缓存。顶级关闭,全部不缓存
  51. * @link 参考:https://v3.cn.vuejs.org/api/built-in-components.html#keep-alive
  52. * @param arr 处理后的一维路由菜单数组
  53. * @returns 返回将一维数组重新处理成 `定义动态路由(baseRoutes)` 的格式
  54. */
  55. export function formatTwoStageRoutes(arr: any) {
  56. if (arr.length <= 0) return false;
  57. const newArr: any = [];
  58. const cacheList: Array<string> = [];
  59. arr.forEach((v: any) => {
  60. if (v.path === '/') {
  61. newArr.push({ component: v.component, name: v.name, path: v.path, redirect: v.redirect, meta: v.meta, children: [] });
  62. } else {
  63. // 判断是否是动态路由(xx/:id/:name),用于 tagsView 等中使用
  64. if (v.path.indexOf('/:') > -1) {
  65. v.meta['isDynamic'] = true;
  66. v.meta['isDynamicPath'] = v.path;
  67. }
  68. newArr[0].children.push({ ...v });
  69. // 存 name 值,keep-alive 中 include 使用,实现路由的缓存
  70. // 路径:/@/layout/routerView/parent.vue
  71. if (newArr[0].meta.isKeepAlive && v.meta.isKeepAlive) {
  72. cacheList.push(v.name);
  73. const stores = useKeepALiveNames(pinia);
  74. stores.setCacheKeepAlive(cacheList);
  75. }
  76. }
  77. });
  78. return newArr;
  79. }
  80. // 路由加载前
  81. router.beforeEach(async (to, from, next) => {
  82. NProgress.configure({ showSpinner: false });
  83. if (to.name) NProgress.start();
  84. // 设置模拟token,跳过验证
  85. if (!Session.getToken()) {
  86. Session.set('token', 'mock-token-for-development');
  87. }
  88. if (to.meta.isAuth !== undefined && !to.meta.isAuth) {
  89. next();
  90. NProgress.done();
  91. } else {
  92. // 直接跳转到首页,跳过登录验证
  93. if (to.path === '/login') {
  94. next('/home');
  95. NProgress.done();
  96. } else {
  97. const storesRoutesList = useRoutesList(pinia);
  98. const { routesList } = storeToRefs(storesRoutesList);
  99. if (routesList.value.length === 0) {
  100. // 后端控制路由:路由数据初始化,防止刷新时丢失
  101. await initBackEndControlRoutes();
  102. next({ path: to.path, query: to.query });
  103. } else {
  104. next();
  105. }
  106. }
  107. }
  108. });
  109. // 路由加载后
  110. router.afterEach(() => {
  111. NProgress.done();
  112. });
  113. // 导出路由
  114. export default router;