You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
5.1 KiB

3 years ago
3 years ago
  1. import { toRouteType } from "./types";
  2. import { openLink } from "/@/utils/link";
  3. import NProgress from "/@/utils/progress";
  4. import { constantRoutes } from "./modules";
  5. import { split, findIndex } from "lodash-es";
  6. import { transformI18n } from "/@/plugins/i18n";
  7. import remainingRouter from "./modules/remaining";
  8. import { storageSession } from "/@/utils/storage";
  9. import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
  10. import { usePermissionStoreHook } from "/@/store/modules/permission";
  11. import { Router, RouteMeta, createRouter, RouteRecordName } from "vue-router";
  12. import {
  13. initRouter,
  14. getHistoryMode,
  15. getParentPaths,
  16. findRouteByPath,
  17. handleAliveRoute
  18. } from "./utils";
  19. // 创建路由实例
  20. export const router: Router = createRouter({
  21. history: getHistoryMode(),
  22. routes: constantRoutes.concat(...remainingRouter),
  23. strict: true,
  24. scrollBehavior(to, from, savedPosition) {
  25. return new Promise(resolve => {
  26. if (savedPosition) {
  27. return savedPosition;
  28. } else {
  29. if (from.meta.saveSrollTop) {
  30. const top: number =
  31. document.documentElement.scrollTop || document.body.scrollTop;
  32. resolve({ left: 0, top });
  33. }
  34. }
  35. });
  36. }
  37. });
  38. // 路由白名单
  39. const whiteList = ["/login"];
  40. router.beforeEach((to: toRouteType, _from, next) => {
  41. if (to.meta?.keepAlive) {
  42. const newMatched = to.matched;
  43. handleAliveRoute(newMatched, "add");
  44. // 页面整体刷新和点击标签页刷新
  45. if (_from.name === undefined || _from.name === "redirect") {
  46. handleAliveRoute(newMatched);
  47. }
  48. }
  49. const name = storageSession.getItem("info");
  50. NProgress.start();
  51. const externalLink = to?.redirectedFrom?.fullPath;
  52. if (!externalLink)
  53. to.matched.some(item => {
  54. item.meta.title
  55. ? (document.title = transformI18n(
  56. item.meta.title as string,
  57. item.meta?.i18n as boolean
  58. ))
  59. : "";
  60. });
  61. if (name) {
  62. if (_from?.name) {
  63. // 如果路由包含http 则是超链接 反之是普通路由
  64. if (externalLink && externalLink.includes("http")) {
  65. openLink(`http${split(externalLink, "http")[1]}`);
  66. NProgress.done();
  67. } else {
  68. next();
  69. }
  70. } else {
  71. // 刷新
  72. if (usePermissionStoreHook().wholeMenus.length === 0)
  73. initRouter(name.username).then((router: Router) => {
  74. if (!useMultiTagsStoreHook().getMultiTagsCache) {
  75. const handTag = (
  76. path: string,
  77. parentPath: string,
  78. name: RouteRecordName,
  79. meta: RouteMeta
  80. ): void => {
  81. useMultiTagsStoreHook().handleTags("push", {
  82. path,
  83. parentPath,
  84. name,
  85. meta
  86. });
  87. };
  88. // 未开启标签页缓存,刷新页面重定向到顶级路由(参考标签页操作例子,只针对静态路由)
  89. if (to.meta?.dynamicLevel) {
  90. const routes = router.options.routes;
  91. const { refreshRedirect } = to.meta;
  92. const { name, meta } = findRouteByPath(refreshRedirect, routes);
  93. handTag(
  94. refreshRedirect,
  95. getParentPaths(refreshRedirect, routes)[1],
  96. name,
  97. meta
  98. );
  99. return router.push(refreshRedirect);
  100. } else {
  101. const { path } = to;
  102. const index = findIndex(remainingRouter, v => {
  103. return v.path == path;
  104. });
  105. const routes =
  106. index === -1
  107. ? router.options.routes[0].children
  108. : router.options.routes;
  109. const route = findRouteByPath(path, routes);
  110. const routePartent = getParentPaths(path, routes);
  111. // 未开启标签页缓存,刷新页面重定向到顶级路由(参考标签页操作例子,只针对动态路由)
  112. if (
  113. path !== routes[0].path &&
  114. route?.meta?.rank !== 0 &&
  115. routePartent.length === 0
  116. ) {
  117. const { name, meta } = findRouteByPath(
  118. route?.meta?.refreshRedirect,
  119. routes
  120. );
  121. handTag(
  122. route.meta?.refreshRedirect,
  123. getParentPaths(route.meta?.refreshRedirect, routes)[0],
  124. name,
  125. meta
  126. );
  127. return router.push(route.meta?.refreshRedirect);
  128. } else {
  129. handTag(
  130. route.path,
  131. routePartent[routePartent.length - 1],
  132. route.name,
  133. route.meta
  134. );
  135. return router.push(path);
  136. }
  137. }
  138. }
  139. router.push(to.fullPath);
  140. });
  141. next();
  142. }
  143. } else {
  144. if (to.path !== "/login") {
  145. if (whiteList.indexOf(to.path) !== -1) {
  146. next();
  147. } else {
  148. next({ path: "/login" });
  149. }
  150. } else {
  151. next();
  152. }
  153. }
  154. });
  155. router.afterEach(() => {
  156. NProgress.done();
  157. });
  158. export default router;