backEnd.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { RouteRecordRaw } from 'vue-router';
  2. import pinia from '/@/stores/index';
  3. import { useUserInfo } from '/@/stores/userInfo';
  4. import { useRequestOldRoutes } from '/@/stores/requestOldRoutes';
  5. import { Session } from '/@/utils/storage';
  6. import { NextLoading } from '/@/utils/loading';
  7. import { baseRoutes, notFoundAndNoPower, dynamicRoutes } from '/@/router/route';
  8. import { formatTwoStageRoutes, formatFlatteningRoutes, router } from '/@/router/index';
  9. import { useRoutesList } from '/@/stores/routesList';
  10. import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
  11. import { useMenuApi } from '/@/api/admin/menu';
  12. // 后端控制路由
  13. // 引入 api 请求接口
  14. const menuApi = useMenuApi();
  15. /**
  16. * 获取目录下的 .vue、.tsx 全部文件
  17. * @method import.meta.glob
  18. * @link 参考:https://cn.vitejs.dev/guide/features.html#json
  19. */
  20. const layouModules: any = import.meta.glob('../layout/routerView/*.{vue,tsx}');
  21. const viewsModules: any = import.meta.glob('../views/**/*.{vue,tsx}');
  22. const dynamicViewsModules: Record<string, Function> = Object.assign({}, { ...layouModules }, { ...viewsModules });
  23. /**
  24. * 后端控制路由:初始化方法,防止刷新时路由丢失
  25. * @method NextLoading 界面 loading 动画开始执行
  26. * @method useUserInfo().setUserInfos() 触发初始化用户信息 pinia
  27. * @method useRequestOldRoutes().setRequestOldRoutes() 存储接口原始路由(未处理component),根据需求选择使用
  28. * @method setAddRoute 添加动态路由
  29. * @method setFilterMenuAndCacheTagsViewRoutes 设置路由到 pinia routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
  30. */
  31. export async function initBackEndControlRoutes() {
  32. // 界面 loading 动画开始执行
  33. if (window.nextLoading === undefined) NextLoading.start();
  34. // 无 token 停止执行下一步
  35. if (!Session.getToken()) return false;
  36. // 触发初始化用户信息 pinia
  37. await useUserInfo().setUserInfos();
  38. // 获取路由菜单数据
  39. const res = await getBackEndControlRoutes();
  40. // 无登录权限时,添加判断
  41. // https://gitee.com/lyt-top/vue-next-admin/issues/I64HVO
  42. if ((res.data || []).length <= 0) return Promise.resolve(true);
  43. // 存储接口原始路由(未处理component),根据需求选择使用
  44. useRequestOldRoutes().setRequestOldRoutes(JSON.parse(JSON.stringify(res.data)));
  45. // 处理路由(component),替换 baseRoutes(/@/router/route)第一个顶级 children 的路由
  46. baseRoutes[0].children = [...dynamicRoutes, ...(await backEndComponent(res.data))];
  47. // 添加动态路由
  48. await setAddRoute();
  49. // 设置路由到 pinia routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
  50. await setFilterMenuAndCacheTagsViewRoutes();
  51. }
  52. /**
  53. * 设置路由到 pinia routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
  54. * @description 用于左侧菜单、横向菜单的显示
  55. * @description 用于 tagsView、菜单搜索中:未过滤隐藏的(isHide)
  56. */
  57. export async function setFilterMenuAndCacheTagsViewRoutes() {
  58. const storesRoutesList = useRoutesList(pinia);
  59. storesRoutesList.setRoutesList(baseRoutes[0].children as any);
  60. setCacheTagsViewRoutes();
  61. }
  62. /**
  63. * 缓存多级嵌套数组处理后的一维数组
  64. * @description 用于 tagsView、菜单搜索中:未过滤隐藏的(isHide)
  65. */
  66. export function setCacheTagsViewRoutes() {
  67. const storesTagsView = useTagsViewRoutes(pinia);
  68. storesTagsView.setTagsViewRoutes(formatTwoStageRoutes(formatFlatteningRoutes(baseRoutes))[0].children);
  69. }
  70. /**
  71. * 处理路由格式及添加捕获所有路由或 404 Not found 路由
  72. * @description 替换 baseRoutes(/@/router/route)第一个顶级 children 的路由
  73. * @returns 返回替换后的路由数组
  74. */
  75. export function setFilterRouteEnd() {
  76. let filterRouteEnd: any = formatTwoStageRoutes(formatFlatteningRoutes(baseRoutes));
  77. // notFoundAndNoPower 防止 404、401 不在 layout 布局中,不设置的话,404、401 界面将全屏显示
  78. // 关联问题 No match found for location with path 'xxx'
  79. filterRouteEnd[0].children = [...filterRouteEnd[0].children, ...notFoundAndNoPower];
  80. return filterRouteEnd;
  81. }
  82. /**
  83. * 添加动态路由
  84. * @method router.addRoute
  85. * @description 此处循环为 baseRoutes(/@/router/route)第一个顶级 children 的路由一维数组,非多级嵌套
  86. * @link 参考:https://next.router.vuejs.org/zh/api/#addroute
  87. */
  88. export async function setAddRoute() {
  89. await setFilterRouteEnd().forEach((route: RouteRecordRaw) => {
  90. router.addRoute(route);
  91. });
  92. }
  93. /**
  94. * 请求后端路由菜单接口
  95. * @description isRequestRoutes 为 true,则开启后端控制路由
  96. * @returns 返回后端路由菜单数据
  97. */
  98. export function getBackEndControlRoutes() {
  99. return menuApi.getAdminMenu();
  100. }
  101. /**
  102. * 重新请求后端路由菜单接口
  103. * @description 用于菜单管理界面刷新菜单(未进行测试)
  104. * @description 路径:/src/views/admin/menu/component/addMenu.vue
  105. */
  106. export async function setBackEndControlRefreshRoutes() {
  107. await getBackEndControlRoutes();
  108. }
  109. /**
  110. * 后端路由 component 转换
  111. * @param routes 后端返回的路由表数组
  112. * @returns 返回处理成函数后的 component
  113. */
  114. export function backEndComponent(routes: any) {
  115. if (!routes) return;
  116. return routes.map((item: any) => {
  117. if (item.path && item.path.startsWith('http')) {
  118. if (item.meta.isIframe) {
  119. item.component = () => import('/@/layout/routerView/iframes.vue');
  120. } else {
  121. item.component = () => import('/@/layout/routerView/link.vue');
  122. }
  123. item.path = '/iframes/' + window.btoa(item.path);
  124. } else if (item.path) {
  125. item.component = dynamicImport(dynamicViewsModules, item.path.split('/:')[0]);
  126. }
  127. item.children && backEndComponent(item.children);
  128. if (item.children) {
  129. item.redirect = item.children[0].path;
  130. }
  131. return item;
  132. });
  133. }
  134. /**
  135. * 后端路由 component 转换函数
  136. * @param dynamicViewsModules 获取目录下的 .vue、.tsx 全部文件
  137. * @param component 当前要处理项 component
  138. * @returns 返回处理成函数后的 component
  139. */
  140. export function dynamicImport(dynamicViewsModules: Record<string, Function>, component: string) {
  141. const keys = Object.keys(dynamicViewsModules);
  142. const matchKeys = keys.filter((key) => {
  143. const k = key.replace(/..\/views|../, '');
  144. return k.startsWith(`${component}.vue`) || k.startsWith(`/${component}.vue`);
  145. });
  146. if (matchKeys?.length === 1) {
  147. const matchKey = matchKeys[0];
  148. return dynamicViewsModules[matchKey];
  149. }
  150. if (matchKeys?.length > 1) {
  151. return false;
  152. }
  153. }