index.js 4.5 KB

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