index.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import {
  2. Router,
  3. createRouter,
  4. RouteComponent,
  5. createWebHashHistory
  6. } from "vue-router";
  7. import { split } from "lodash-es";
  8. import { i18n } from "/@/plugins/i18n";
  9. import NProgress from "/@/utils/progress";
  10. import { openLink } from "/@/utils/link";
  11. import { storageSession, storageLocal } from "/@/utils/storage";
  12. import { usePermissionStoreHook } from "/@/store/modules/permission";
  13. // 静态路由
  14. import homeRouter from "./modules/home";
  15. import Layout from "/@/layout/index.vue";
  16. import errorRouter from "./modules/error";
  17. import editorRouter from "./modules/editor";
  18. import nestedRouter from "./modules/nested";
  19. import externalLink from "./modules/externalLink";
  20. import remainingRouter from "./modules/remaining";
  21. import flowChartRouter from "./modules/flowchart";
  22. import componentsRouter from "./modules/components";
  23. // 动态路由
  24. import { getAsyncRoutes } from "/@/api/routes";
  25. // https://cn.vitejs.dev/guide/features.html#glob-import
  26. const modulesRoutes = import.meta.glob("/src/views/*/*/*.vue");
  27. const constantRoutes: Array<RouteComponent> = [
  28. homeRouter,
  29. flowChartRouter,
  30. editorRouter,
  31. componentsRouter,
  32. nestedRouter,
  33. externalLink,
  34. errorRouter
  35. ];
  36. // 按照路由中meta下的rank等级升序来排序路由
  37. export const ascending = arr => {
  38. return arr.sort((a: any, b: any) => {
  39. return a?.meta?.rank - b?.meta?.rank;
  40. });
  41. };
  42. // 将所有静态路由导出
  43. export const constantRoutesArr: Array<RouteComponent> = ascending(
  44. constantRoutes
  45. ).concat(...remainingRouter);
  46. // 过滤meta中showLink为false的路由
  47. export const filterTree = data => {
  48. const newTree = data.filter(v => v.meta.showLink);
  49. newTree.forEach(v => v.children && (v.children = filterTree(v.children)));
  50. return newTree;
  51. };
  52. // 过滤后端传来的动态路由 重新生成规范路由
  53. export const addAsyncRoutes = (arrRoutes: Array<RouteComponent>) => {
  54. if (!arrRoutes || !arrRoutes.length) return;
  55. arrRoutes.forEach((v: any) => {
  56. if (v.redirect) {
  57. v.component = Layout;
  58. } else {
  59. v.component = modulesRoutes[`/src/views${v.path}/index.vue`];
  60. }
  61. if (v.children) {
  62. addAsyncRoutes(v.children);
  63. }
  64. });
  65. return arrRoutes;
  66. };
  67. export const router: Router = createRouter({
  68. history: createWebHashHistory(),
  69. routes: filterTree(ascending(constantRoutes)).concat(...remainingRouter),
  70. scrollBehavior(to, from, savedPosition) {
  71. return new Promise(resolve => {
  72. if (savedPosition) {
  73. return savedPosition;
  74. } else {
  75. if (from.meta.saveSrollTop) {
  76. const top: number =
  77. document.documentElement.scrollTop || document.body.scrollTop;
  78. resolve({ left: 0, top });
  79. }
  80. }
  81. });
  82. }
  83. });
  84. export const initRouter = name => {
  85. return new Promise(resolve => {
  86. getAsyncRoutes({ name }).then(({ info }) => {
  87. if (info.length === 0) {
  88. usePermissionStoreHook().changeSetting(info);
  89. } else {
  90. addAsyncRoutes(info).map((v: any) => {
  91. // 防止重复添加路由
  92. if (
  93. router.options.routes.findIndex(value => value.path === v.path) !==
  94. -1
  95. ) {
  96. return;
  97. } else {
  98. // 切记将路由push到routes后还需要使用addRoute,这样路由才能正常跳转
  99. router.options.routes.push(v);
  100. // 最终路由进行升序
  101. ascending(router.options.routes);
  102. router.addRoute(v.name, v);
  103. usePermissionStoreHook().changeSetting(info);
  104. }
  105. resolve(router);
  106. });
  107. }
  108. router.addRoute({
  109. path: "/:pathMatch(.*)",
  110. redirect: "/error/404"
  111. });
  112. });
  113. });
  114. };
  115. // reset router
  116. export function resetRouter() {
  117. router.getRoutes().forEach(route => {
  118. const { name } = route;
  119. if (name) {
  120. router.hasRoute(name) && router.removeRoute(name);
  121. }
  122. });
  123. }
  124. const whiteList = ["/login", "/register"];
  125. router.beforeEach((to, _from, next) => {
  126. const name = storageSession.getItem("info");
  127. NProgress.start();
  128. const externalLink = to?.redirectedFrom?.fullPath;
  129. // @ts-ignore
  130. const { t } = i18n.global;
  131. // @ts-ignore
  132. if (!externalLink) to.meta.title ? (document.title = t(to.meta.title)) : "";
  133. if (name) {
  134. if (_from?.name) {
  135. // 如果路由包含http 则是超链接 反之是普通路由
  136. if (externalLink && externalLink.includes("http")) {
  137. openLink(`http${split(externalLink, "http")[1]}`);
  138. NProgress.done();
  139. } else {
  140. next();
  141. }
  142. } else {
  143. // 刷新
  144. if (usePermissionStoreHook().wholeRoutes.length === 0)
  145. initRouter(name.username).then((router: Router) => {
  146. router.push(to.path);
  147. // 刷新页面更新标签栏与页面路由匹配
  148. const localRoutes = storageLocal.getItem(
  149. "responsive-routesInStorage"
  150. );
  151. const optionsRoutes = router.options?.routes;
  152. const newLocalRoutes = [];
  153. optionsRoutes.forEach(ors => {
  154. localRoutes.forEach(lrs => {
  155. if (ors.path === lrs.parentPath) {
  156. newLocalRoutes.push(lrs);
  157. }
  158. });
  159. });
  160. storageLocal.setItem("responsive-routesInStorage", newLocalRoutes);
  161. });
  162. next();
  163. }
  164. } else {
  165. if (to.path !== "/login") {
  166. if (whiteList.indexOf(to.path) !== -1) {
  167. next();
  168. } else {
  169. next({ path: "/login" });
  170. }
  171. } else {
  172. next();
  173. }
  174. }
  175. });
  176. router.afterEach(() => {
  177. NProgress.done();
  178. });
  179. export default router;