routeHelper.ts 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. import type { AppRouteModule, AppRouteRecordRaw } from '/@/router/types';
  2. import type { Router, RouteRecordNormalized } from 'vue-router';
  3. import { getParentLayout, LAYOUT, EXCEPTION_COMPONENT } from '/@/router/constant';
  4. import { cloneDeep, omit } from 'lodash-es';
  5. import { warn } from '/@/utils/log';
  6. import { createRouter, createWebHashHistory } from 'vue-router';
  7. import { getTenantId, getToken } from '/@/utils/auth';
  8. import { URL_HASH_TAB } from '/@/utils';
  9. import { packageViews } from '/@/utils/monorepo/dynamicRouter';
  10. import { useI18n } from '/@/hooks/web/useI18n';
  11. export type LayoutMapKey = 'LAYOUT';
  12. const IFRAME = () => import('/@/views/sys/iframe/FrameBlank.vue');
  13. // const IFRAME = () => import('/@/views/sys/iframe/index.vue');
  14. const LayoutContent = () => import('/@/layouts/default/content/index.vue');
  15. const LayoutMap = new Map<string, () => Promise<typeof import('*.vue')>>();
  16. LayoutMap.set('LAYOUT', LAYOUT);
  17. LayoutMap.set('IFRAME', IFRAME);
  18. //微前端qiankun
  19. LayoutMap.set('LayoutsContent', LayoutContent);
  20. let dynamicViewsModules: Record<string, () => Promise<Recordable>>;
  21. // Dynamic introduction
  22. function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) {
  23. if (!dynamicViewsModules) {
  24. dynamicViewsModules = import.meta.glob('../../views/**/*.{vue,tsx}');
  25. // 跟模块views合并
  26. dynamicViewsModules = Object.assign({}, dynamicViewsModules, packageViews);
  27. }
  28. if (!routes) return;
  29. routes.forEach((item) => {
  30. //【jeecg-boot/issues/I5N2PN】左侧动态菜单怎么做国际化处理 2022-10-09
  31. //菜单支持国际化翻译
  32. if (item?.meta?.title) {
  33. const { t } = useI18n();
  34. if (item.meta.title.includes("t('") && t) {
  35. item.meta.title = eval(item.meta.title);
  36. //console.log('译后: ',item.meta.title)
  37. }
  38. }
  39. // update-begin--author:sunjianlei---date:20210918---for:适配旧版路由选项 --------
  40. // @ts-ignore 适配隐藏路由
  41. if (item?.hidden) {
  42. item.meta.hideMenu = true;
  43. //是否隐藏面包屑
  44. item.meta.hideBreadcrumb = true;
  45. }
  46. // @ts-ignore 添加忽略路由配置
  47. if (item?.route == 0) {
  48. item.meta.ignoreRoute = true;
  49. }
  50. // @ts-ignore 添加是否缓存路由配置
  51. item.meta.ignoreKeepAlive = !item?.meta.keepAlive;
  52. let token = getToken();
  53. let tenantId = getTenantId();
  54. // URL支持{{ window.xxx }}占位符变量
  55. //update-begin---author:wangshuai ---date:20220711 for:[VUEN-1638]菜单tenantId需要动态生成------------
  56. item.component = (item.component || '')
  57. .replace(/{{([^}}]+)?}}/g, (s1, s2) => eval(s2))
  58. .replace('${token}', token)
  59. .replace('${tenantId}', tenantId);
  60. //update-end---author:wangshuai ---date:20220711 for:[VUEN-1638]菜单tenantId需要动态生成------------
  61. // 适配 iframe
  62. if (/^\/?http(s)?/.test(item.component as string)) {
  63. item.component = item.component.substring(1, item.component.length);
  64. }
  65. if (/^http(s)?/.test(item.component as string)) {
  66. if (item.meta?.internalOrExternal) {
  67. // @ts-ignore 外部打开
  68. item.path = item.component;
  69. // update-begin--author:sunjianlei---date:20220408---for: 【VUEN-656】配置外部网址打不开,原因是带了#号,需要替换一下
  70. item.path = item.path.replace('#', URL_HASH_TAB);
  71. // update-end--author:sunjianlei---date:20220408---for: 【VUEN-656】配置外部网址打不开,原因是带了#号,需要替换一下
  72. } else {
  73. // @ts-ignore 内部打开
  74. item.meta.frameSrc = item.component;
  75. }
  76. delete item.component;
  77. }
  78. // update-end--author:sunjianlei---date:20210918---for:适配旧版路由选项 --------
  79. if (!item.component && item.meta?.frameSrc) {
  80. item.component = 'IFRAME';
  81. }
  82. let { component, name } = item;
  83. const { children } = item;
  84. if (component) {
  85. const layoutFound = LayoutMap.get(component.toUpperCase());
  86. if (layoutFound) {
  87. item.component = layoutFound;
  88. } else {
  89. // update-end--author:zyf---date:20220307--for:VUEN-219兼容后台返回动态首页,目的适配跟v2版本配置一致 --------
  90. if (component.indexOf('dashboard/') > -1) {
  91. //当数据标sys_permission中component没有拼接index时前端需要拼接
  92. if (component.indexOf('/index') < 0) {
  93. component = component + '/index';
  94. }
  95. }
  96. // update-end--author:zyf---date:20220307---for:VUEN-219兼容后台返回动态首页,目的适配跟v2版本配置一致 --------
  97. item.component = dynamicImport(dynamicViewsModules, component as string);
  98. }
  99. } else if (name) {
  100. item.component = getParentLayout();
  101. }
  102. children && asyncImportRoute(children);
  103. });
  104. }
  105. function dynamicImport(dynamicViewsModules: Record<string, () => Promise<Recordable>>, component: string) {
  106. const keys = Object.keys(dynamicViewsModules);
  107. const matchKeys = keys.filter((key) => {
  108. const k = key.replace('../../views', '');
  109. const startFlag = component.startsWith('/');
  110. const endFlag = component.endsWith('.vue') || component.endsWith('.tsx');
  111. const startIndex = startFlag ? 0 : 1;
  112. const lastIndex = endFlag ? k.length : k.lastIndexOf('.');
  113. return k.substring(startIndex, lastIndex) === component;
  114. });
  115. if (matchKeys?.length === 1) {
  116. const matchKey = matchKeys[0];
  117. return dynamicViewsModules[matchKey];
  118. } else if (matchKeys?.length > 1) {
  119. warn(
  120. 'Please do not create `.vue` and `.TSX` files with the same file name in the same hierarchical directory under the views folder. This will cause dynamic introduction failure'
  121. );
  122. return;
  123. }
  124. }
  125. // Turn background objects into routing objects
  126. export function transformObjToRoute<T = AppRouteModule>(routeList: AppRouteModule[]): T[] {
  127. routeList.forEach((route) => {
  128. const component = route.component as string;
  129. if (component) {
  130. if (component.toUpperCase() === 'LAYOUT') {
  131. route.component = LayoutMap.get(component.toUpperCase());
  132. } else {
  133. route.children = [cloneDeep(route)];
  134. route.component = LAYOUT;
  135. route.name = `${route.name}Parent`;
  136. route.path = '';
  137. const meta = route.meta || {};
  138. meta.single = true;
  139. meta.affix = false;
  140. route.meta = meta;
  141. }
  142. } else {
  143. warn('请正确配置路由:' + route?.name + '的component属性');
  144. }
  145. route.children && asyncImportRoute(route.children);
  146. });
  147. return routeList as unknown as T[];
  148. }
  149. /**
  150. * 将多级路由转换为二级
  151. */
  152. export function flatMultiLevelRoutes(routeModules: AppRouteModule[]) {
  153. const modules: AppRouteModule[] = cloneDeep(routeModules);
  154. for (let index = 0; index < modules.length; index++) {
  155. const routeModule = modules[index];
  156. if (!isMultipleRoute(routeModule)) {
  157. continue;
  158. }
  159. promoteRouteLevel(routeModule);
  160. }
  161. return modules;
  162. }
  163. //提升路由级别
  164. function promoteRouteLevel(routeModule: AppRouteModule) {
  165. // Use vue-router to splice menus
  166. let router: Router | null = createRouter({
  167. routes: [routeModule as unknown as RouteRecordNormalized],
  168. history: createWebHashHistory(),
  169. });
  170. const routes = router.getRoutes();
  171. addToChildren(routes, routeModule.children || [], routeModule);
  172. router = null;
  173. routeModule.children = routeModule.children?.map((item) => omit(item, 'children'));
  174. }
  175. // Add all sub-routes to the secondary route
  176. function addToChildren(routes: RouteRecordNormalized[], children: AppRouteRecordRaw[], routeModule: AppRouteModule) {
  177. for (let index = 0; index < children.length; index++) {
  178. const child = children[index];
  179. const route = routes.find((item) => item.name === child.name);
  180. if (!route) {
  181. continue;
  182. }
  183. routeModule.children = routeModule.children || [];
  184. if (!routeModule.children.find((item) => item.name === route.name)) {
  185. routeModule.children?.push(route as unknown as AppRouteModule);
  186. }
  187. if (child.children?.length) {
  188. addToChildren(routes, child.children, routeModule);
  189. }
  190. }
  191. }
  192. // Determine whether the level exceeds 2 levels
  193. function isMultipleRoute(routeModule: AppRouteModule) {
  194. if (!routeModule || !Reflect.has(routeModule, 'children') || !routeModule.children?.length) {
  195. return false;
  196. }
  197. const children = routeModule.children;
  198. let flag = false;
  199. for (let index = 0; index < children.length; index++) {
  200. const child = children[index];
  201. if (child.children?.length) {
  202. flag = true;
  203. break;
  204. }
  205. }
  206. return flag;
  207. }
  208. /**
  209. * 组件地址前加斜杠处理
  210. * @updateBy:lsq
  211. * @updateDate:2021-09-08
  212. */
  213. export function addSlashToRouteComponent(routeList: AppRouteRecordRaw[]) {
  214. routeList.forEach((route) => {
  215. if (route.path.startsWith('/subSysmodal')) {
  216. route.path = '/micro-vent-3dModal' + route.path;
  217. route.component = 'layouts/default/index';
  218. }
  219. let component = route.component as string;
  220. if (component) {
  221. const layoutFound = LayoutMap.get(component);
  222. if (!layoutFound) {
  223. route.component = component.startsWith('/') ? component : `/${component}`;
  224. }
  225. }
  226. route.children && addSlashToRouteComponent(route.children);
  227. });
  228. return routeList as unknown as T[];
  229. }