index.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { isUrl } from "/@/utils/is";
  2. import { getConfig } from "/@/config";
  3. import { toRouteType } from "./types";
  4. import { openLink } from "/@/utils/link";
  5. import NProgress from "/@/utils/progress";
  6. import { findIndex } from "lodash-unified";
  7. import { transformI18n } from "/@/plugins/i18n";
  8. import { storageSession } from "/@/utils/storage";
  9. import { buildHierarchyTree } from "/@/utils/tree";
  10. import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
  11. import { usePermissionStoreHook } from "/@/store/modules/permission";
  12. import {
  13. Router,
  14. RouteMeta,
  15. createRouter,
  16. RouteRecordRaw,
  17. RouteComponent,
  18. RouteRecordName
  19. } from "vue-router";
  20. import {
  21. ascending,
  22. initRouter,
  23. getHistoryMode,
  24. getParentPaths,
  25. findRouteByPath,
  26. handleAliveRoute,
  27. formatTwoStageRoutes,
  28. formatFlatteningRoutes
  29. } from "./utils";
  30. import pptRouter from "./modules/ppt";
  31. import homeRouter from "./modules/home";
  32. import ableRouter from "./modules/able";
  33. import listRouter from "./modules/list";
  34. import aboutRouter from "./modules/about";
  35. import errorRouter from "./modules/error";
  36. import guideRouter from "./modules/guide";
  37. import resultRouter from "./modules/result";
  38. import editorRouter from "./modules/editor";
  39. import nestedRouter from "./modules/nested";
  40. import flowChartRouter from "./modules/flowchart";
  41. import remainingRouter from "./modules/remaining";
  42. import componentsRouter from "./modules/components";
  43. import formDesignRouter from "./modules/formdesign";
  44. // 原始静态路由(未做任何处理)
  45. const routes = [
  46. pptRouter,
  47. homeRouter,
  48. ableRouter,
  49. listRouter,
  50. aboutRouter,
  51. errorRouter,
  52. guideRouter,
  53. resultRouter,
  54. nestedRouter,
  55. editorRouter,
  56. flowChartRouter,
  57. componentsRouter,
  58. formDesignRouter
  59. ];
  60. // 导出处理后的静态路由(三级及以上的路由全部拍成二级)
  61. export const constantRoutes: Array<RouteRecordRaw> = formatTwoStageRoutes(
  62. formatFlatteningRoutes(buildHierarchyTree(ascending(routes)))
  63. );
  64. // 用于渲染菜单,保持原始层级
  65. export const constantMenus: Array<RouteComponent> = ascending(routes).concat(
  66. ...remainingRouter
  67. );
  68. // 不参与菜单的路由
  69. export const remainingPaths = Object.keys(remainingRouter).map(v => {
  70. return remainingRouter[v].path;
  71. });
  72. // 创建路由实例
  73. export const router: Router = createRouter({
  74. history: getHistoryMode(),
  75. routes: constantRoutes.concat(...remainingRouter),
  76. strict: true,
  77. scrollBehavior(to, from, savedPosition) {
  78. return new Promise(resolve => {
  79. if (savedPosition) {
  80. return savedPosition;
  81. } else {
  82. if (from.meta.saveSrollTop) {
  83. const top: number =
  84. document.documentElement.scrollTop || document.body.scrollTop;
  85. resolve({ left: 0, top });
  86. }
  87. }
  88. });
  89. }
  90. });
  91. // 路由白名单
  92. const whiteList = ["/login"];
  93. router.beforeEach((to: toRouteType, _from, next) => {
  94. if (to.meta?.keepAlive) {
  95. const newMatched = to.matched;
  96. handleAliveRoute(newMatched, "add");
  97. // 页面整体刷新和点击标签页刷新
  98. if (_from.name === undefined || _from.name === "Redirect") {
  99. handleAliveRoute(newMatched);
  100. }
  101. }
  102. const name = storageSession.getItem("info");
  103. NProgress.start();
  104. const externalLink = isUrl(to?.name);
  105. if (!externalLink)
  106. to.matched.some(item => {
  107. if (!item.meta.title) return "";
  108. const Title = getConfig().Title;
  109. if (Title)
  110. document.title = `${transformI18n(item.meta.title)} | ${Title}`;
  111. else document.title = transformI18n(item.meta.title);
  112. });
  113. if (name) {
  114. if (_from?.name) {
  115. // name为超链接
  116. if (externalLink) {
  117. openLink(to?.name);
  118. NProgress.done();
  119. } else {
  120. next();
  121. }
  122. } else {
  123. // 刷新
  124. if (usePermissionStoreHook().wholeMenus.length === 0)
  125. initRouter(name.username).then((router: Router) => {
  126. if (!useMultiTagsStoreHook().getMultiTagsCache) {
  127. const handTag = (
  128. path: string,
  129. parentPath: string,
  130. name: RouteRecordName,
  131. meta: RouteMeta
  132. ): void => {
  133. useMultiTagsStoreHook().handleTags("push", {
  134. path,
  135. parentPath,
  136. name,
  137. meta
  138. });
  139. };
  140. // 未开启标签页缓存,刷新页面重定向到顶级路由(参考标签页操作例子,只针对静态路由)
  141. if (to.meta?.refreshRedirect) {
  142. const routes = router.options.routes;
  143. const { refreshRedirect } = to.meta;
  144. const { name, meta } = findRouteByPath(refreshRedirect, routes);
  145. handTag(
  146. refreshRedirect,
  147. getParentPaths(refreshRedirect, routes)[1],
  148. name,
  149. meta
  150. );
  151. return router.push(refreshRedirect);
  152. } else {
  153. const { path } = to;
  154. const index = findIndex(remainingRouter, v => {
  155. return v.path == path;
  156. });
  157. const routes =
  158. index === -1
  159. ? router.options.routes[0].children
  160. : router.options.routes;
  161. const route = findRouteByPath(path, routes);
  162. const routePartent = getParentPaths(path, routes);
  163. // 未开启标签页缓存,刷新页面重定向到顶级路由(参考标签页操作例子,只针对动态路由)
  164. if (
  165. path !== routes[0].path &&
  166. route?.meta?.rank !== 0 &&
  167. routePartent.length === 0
  168. ) {
  169. if (!route?.meta?.refreshRedirect) return;
  170. const { name, meta } = findRouteByPath(
  171. route.meta.refreshRedirect,
  172. routes
  173. );
  174. handTag(
  175. route.meta?.refreshRedirect,
  176. getParentPaths(route.meta?.refreshRedirect, routes)[0],
  177. name,
  178. meta
  179. );
  180. return router.push(route.meta?.refreshRedirect);
  181. } else {
  182. handTag(
  183. route.path,
  184. routePartent[routePartent.length - 1],
  185. route.name,
  186. route.meta
  187. );
  188. return router.push(path);
  189. }
  190. }
  191. }
  192. router.push(to.fullPath);
  193. });
  194. next();
  195. }
  196. } else {
  197. if (to.path !== "/login") {
  198. if (whiteList.indexOf(to.path) !== -1) {
  199. next();
  200. } else {
  201. next({ path: "/login" });
  202. }
  203. } else {
  204. next();
  205. }
  206. }
  207. });
  208. router.afterEach(() => {
  209. NProgress.done();
  210. });
  211. export default router;