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.

167 lines
5.2 KiB

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