index.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import { createRouter, createWebHashHistory } from "vue-router";
  2. import { ElNotification } from "element-plus";
  3. import config from "@/config";
  4. import NProgress from "nprogress";
  5. import "nprogress/nprogress.css";
  6. import tool from "@/utils/tool";
  7. import systemRouter from "./systemRouter";
  8. import { beforeEach, afterEach } from "./scrollBehavior";
  9. import { t2 } from "@/utils/index";
  10. import { isOidcEnabled } from "@/auth/keycloak";
  11. // 匹配views里面所有的.vue文件
  12. const modules = import.meta.glob("./../views/**/*.vue");
  13. const otherModules = {
  14. 404: () => import("../layout/other/404.vue"),
  15. empty: () => import("../layout/other/empty.vue"),
  16. };
  17. //系统路由
  18. const routes = systemRouter;
  19. //系统特殊路由
  20. const routes_404 = {
  21. path: "/:pathMatch(.*)*",
  22. hidden: true,
  23. component: otherModules["404"],
  24. };
  25. let routes_404_r = () => {};
  26. const router = createRouter({
  27. history: createWebHashHistory(),
  28. routes,
  29. });
  30. //设置标题
  31. document.title = tool.data.get("APP_NAME");
  32. //判断是否已加载过动态/静态路由
  33. var isGetRouter = false;
  34. router.beforeEach(async (to, from, next) => {
  35. NProgress.start();
  36. //动态标题
  37. document.title = to.meta.title
  38. ? `${t2(to.meta.code) || to.meta.title} - ` +
  39. (tool.data.get("APP_NAME") || config.APP_NAME)
  40. : tool.data.get("APP_NAME") || config.APP_NAME;
  41. let token = tool.data.get(config.TOKEN);
  42. if (to.path === "/login" || to.path === "/ddlogin") {
  43. if (isOidcEnabled && to.path === "/login" && token) {
  44. next({ path: "/" });
  45. return false;
  46. }
  47. //删除路由(替换当前layout路由)
  48. router.addRoute(routes[0]);
  49. //删除路由(404)
  50. routes_404_r();
  51. isGetRouter = false;
  52. next();
  53. return false;
  54. }
  55. if (!token) {
  56. next({
  57. path: "/login",
  58. });
  59. return false;
  60. }
  61. //整页路由处理
  62. if (to.meta.fullpage) {
  63. to.matched = [to.matched[to.matched.length - 1]];
  64. }
  65. //加载动态/静态路由
  66. if (!isGetRouter) {
  67. let apiMenu = tool.data.get("MENU") || [];
  68. var menuRouter = filterAsyncRouter(apiMenu);
  69. menuRouter = flatAsyncRoutes(menuRouter);
  70. menuRouter.forEach((item) => {
  71. router.addRoute("layout", item);
  72. });
  73. routes_404_r = router.addRoute(routes_404);
  74. if (to.matched.length == 0) {
  75. router.push(to.fullPath);
  76. }
  77. isGetRouter = true;
  78. }
  79. beforeEach(to, from);
  80. next();
  81. });
  82. router.afterEach((to, from) => {
  83. afterEach(to, from);
  84. NProgress.done();
  85. });
  86. router.onError((error) => {
  87. NProgress.done();
  88. if (
  89. error.message.includes("Failed to fetch dynamically imported module") ||
  90. error.message.includes("Importing a module script failed")
  91. ) {
  92. ElNotification.warning({
  93. title: "系统更新",
  94. message: "检测到版本更新,正在刷新页面...",
  95. duration: 2000,
  96. });
  97. setTimeout(() => {
  98. window.location.reload();
  99. }, 2000);
  100. } else {
  101. ElNotification.error({
  102. title: "路由错误",
  103. message: error.message,
  104. });
  105. }
  106. });
  107. //入侵追加自定义方法、对象
  108. router.getMenu = () => {
  109. var apiMenu = tool.data.get("MENU") || [];
  110. return apiMenu;
  111. };
  112. //转换
  113. function filterAsyncRouter(routerMap) {
  114. const accessedRouters = [];
  115. routerMap.forEach((item) => {
  116. item.meta = item.meta ? item.meta : {};
  117. //处理外部链接特殊路由
  118. if (item.meta.target == "iframe") {
  119. item.meta.url = item.path;
  120. item.path = `/i/${item.meta.id}`;
  121. item.component = undefined;
  122. } else {
  123. item.component = item.path;
  124. }
  125. if (item.path == undefined) {
  126. item.path = "";
  127. }
  128. //MAP转路由对象
  129. var route = {
  130. path: item.path,
  131. name:
  132. item.path
  133. .replace(/^\//g, "")
  134. .replace(/[/]/g, "-")
  135. .replace(/[?]/g, "-")
  136. .replace(/&/g, "-")
  137. .replace(/=/g, "-") + item.meta.id,
  138. meta: item.meta,
  139. redirect: item.redirect,
  140. children: item.children ? filterAsyncRouter(item.children) : null,
  141. component: loadComponent(item.component),
  142. };
  143. accessedRouters.push(route);
  144. });
  145. return accessedRouters;
  146. }
  147. function loadComponent(component) {
  148. if (component) {
  149. for (const path in modules) {
  150. const dir = path.split("views")[1].split(".vue")[0];
  151. if (dir === component || dir === component + "/index") {
  152. return () => modules[path]();
  153. }
  154. }
  155. }
  156. return otherModules["empty"];
  157. }
  158. // function loadComponent(component) {
  159. // if (component) {
  160. // return () => modules[`./../views${component}.vue`]();
  161. // } else {
  162. // return otherModules["empty"];
  163. // }
  164. // }
  165. //路由扁平化
  166. function flatAsyncRoutes(routes, breadcrumb = []) {
  167. let res = [];
  168. routes.forEach((route) => {
  169. const tmp = { ...route };
  170. if (tmp.children && tmp.children.length > 0) {
  171. let childrenBreadcrumb = [...breadcrumb];
  172. childrenBreadcrumb.push(route);
  173. let tmpRoute = { ...route };
  174. tmpRoute.meta.breadcrumb = childrenBreadcrumb;
  175. delete tmpRoute.children;
  176. if (tmpRoute.path) {
  177. res.push(tmpRoute);
  178. }
  179. let childrenRoutes = flatAsyncRoutes(
  180. tmp.children,
  181. childrenBreadcrumb
  182. );
  183. childrenRoutes.map((item) => {
  184. if (item.path) {
  185. res.push(item);
  186. }
  187. });
  188. } else {
  189. let tmpBreadcrumb = [...breadcrumb];
  190. tmpBreadcrumb.push(tmp);
  191. tmp.meta.breadcrumb = tmpBreadcrumb;
  192. if (tmp.path) {
  193. res.push(tmp);
  194. }
  195. }
  196. });
  197. return res;
  198. }
  199. export default router;