index.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. //@ts-nocheck
  2. /**
  3. * @description 所有人可使用的参数配置列表
  4. * @params hideMenu: 是否隐藏当前路由结点不在导航中展示
  5. * @params alwaysShow: 只有一个子路由时是否总是展示菜单,默认false
  6. */
  7. import {createRouter, createWebHistory, RouteRecordRaw} from "vue-router";
  8. import NProgress from "@/utils/system/nprogress";
  9. import {changeTitle} from "@/utils/system/title";
  10. // 动态路由相关引入数据
  11. // 引入modules
  12. import Dashboard from "./modules/dashboard";
  13. import {getUserMenu, IntergrationMenu} from "@/api/settings/menu-settings";
  14. import {createVNode, defineComponent, h, Ref} from "vue";
  15. import {stringNotBlank} from "@/utils/blank-utils";
  16. import {useUserStore} from "@/pinia/user-store";
  17. import XEUtils from "xe-utils";
  18. const router = createRouter({
  19. history: createWebHistory(),
  20. routes: [...Dashboard],
  21. });
  22. const vueFile = import.meta.glob("../views/**/*.{vue,tsx}");
  23. vueFile.Layout = import.meta.glob("../layout/index.vue")["../layout/index.vue"];
  24. let asyncFinished = false;
  25. export const routerMenus: Ref<IntergrationMenu[]> = ref([]);
  26. const firstLevelRouter = [];
  27. const secondLevelRouter = [];
  28. const appFullscreens = [];
  29. export const secondLevelRouterKey = new Map<string, string[]>();
  30. function emptyRouter(routers: IntergrationMenu[]) {
  31. routers.forEach(item => {
  32. const hasChildren = item.children !== null && item.children.length > 0;
  33. if (item.fullscreen) {
  34. appFullscreens.push(item);
  35. } else if (item.component === "Layout" && hasChildren) {
  36. item.children?.forEach(child => {
  37. child.path = item.path + "/" + child.path;
  38. firstLevelRouter.push(child);
  39. });
  40. } else if (hasChildren) {
  41. item.children!.forEach(child => {
  42. child.path = child.path.replace(item.path + "/", "");
  43. });
  44. secondLevelRouter.push(item);
  45. } else {
  46. firstLevelRouter.push(item);
  47. }
  48. if (item.children !== null && item.children.length > 0) {
  49. emptyRouter(item.children);
  50. }
  51. });
  52. }
  53. function convertToRouter(value: IntergrationMenu[]) {
  54. const routers: RouteRecordRaw[] = [];
  55. value.forEach(item => {
  56. const meta: { [key: string]: any } = {};
  57. for (let key in item) {
  58. if (key.startsWith("meta")) {
  59. const metaKey = capitalizeFirstLower(key.replace("meta", ""));
  60. meta[metaKey] = item[key];
  61. }
  62. }
  63. let path = item.path;
  64. if (item.pathParams?.length > 0) {
  65. const paths = item.path.split("/$");
  66. path = paths[0];
  67. }
  68. const data: RouteRecordRaw = {
  69. path: path + (item.pathParams === null ? "" : item.pathParams),
  70. name: item.name,
  71. redirect: item.redirect,
  72. meta: meta,
  73. componentName: item.component,
  74. component: createNameComponent(item),
  75. };
  76. if (item.children && item.children.length > 0) {
  77. data.children = convertToRouter(item.children);
  78. }
  79. routers.push(data);
  80. });
  81. return routers;
  82. }
  83. async function beforeAddRoutes() {
  84. await useUserStore().getUserInfo;
  85. routerMenus.value = await getUserMenu();
  86. emptyRouter(XEUtils.cloneDeep(routerMenus.value));
  87. secondLevelRouter.forEach(item => {
  88. const tmp = XEUtils.eachAndReturnList(item.children, i => {
  89. return i.name;
  90. });
  91. secondLevelRouterKey.set(item.name, tmp);
  92. });
  93. router.addRoute({
  94. name: "Home",
  95. path: "/",
  96. redirect: "/dashboard",
  97. component: () => import("@/layout/index.vue"),
  98. children: [
  99. ...convertToRouter(firstLevelRouter),
  100. ...convertToRouter(secondLevelRouter),
  101. ],
  102. });
  103. convertToRouter(appFullscreens).forEach(item => {
  104. router.addRoute(item);
  105. });
  106. }
  107. function createNameComponent(item) {
  108. return () => {
  109. return new Promise((resolve, reject) => {
  110. try {
  111. vueFile[`${item.component}`]().then(res => {
  112. const tmp = defineComponent({
  113. name: item.name,
  114. render() {
  115. let children = [createVNode(res.default)];
  116. const style = {
  117. height: "100%",
  118. width: "100%",
  119. };
  120. const props = {
  121. style,
  122. };
  123. if (item.mainCard) {
  124. props.class = "layout_card";
  125. }
  126. if (item.mainOverflowAuto) {
  127. children = [
  128. h(
  129. "div",
  130. {
  131. style: {
  132. height: "100%",
  133. width: "100%",
  134. overflow: "auto",
  135. },
  136. },
  137. [createVNode(res.default)]
  138. ),
  139. ];
  140. }
  141. return h("div", props, children);
  142. },
  143. });
  144. resolve(tmp);
  145. });
  146. } catch (e) {
  147. reject("路由错误");
  148. router.push("/500");
  149. }
  150. });
  151. };
  152. }
  153. const capitalizeFirstLower = (word: string) => {
  154. return word.charAt(0).toLowerCase() + word.slice(1);
  155. };
  156. router.beforeEach(async (to, _from, next) => {
  157. NProgress.start();
  158. const hasToken = stringNotBlank(localStorage.token);
  159. function tmpNext(...arg) {
  160. to.meta.title && changeTitle(to.meta.title);
  161. if (!hasToken) {
  162. next("/login");
  163. return;
  164. }
  165. next(...arg);
  166. }
  167. if (to.name === "notFound" && hasToken) {
  168. if (!asyncFinished) {
  169. await beforeAddRoutes();
  170. tmpNext({ path: to.path, query: to.query, replace: true });
  171. asyncFinished = true;
  172. } else {
  173. tmpNext();
  174. }
  175. } else {
  176. if (!hasToken && !to.meta.needToken) {
  177. to.meta.title && changeTitle(to.meta.title);
  178. next();
  179. } else {
  180. tmpNext();
  181. }
  182. }
  183. });
  184. router.afterEach((to, _from) => {
  185. NProgress.done();
  186. });
  187. export default router;