backEnd.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 { dynamicRoutes, notFoundAndNoPower } 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 { getAPI } from '/@/utils/axios-utils';
  12. import { SysMenuApi } from '/@/api-services/api';
  13. // import { ElMessage } from 'element-plus';
  14. // 后端控制路由
  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.get('token')) return false;
  36. // 触发初始化用户信息 pinia
  37. // https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP
  38. await useUserInfo().setUserInfos();
  39. await useUserInfo().setConstList();
  40. await useUserInfo().setDictList();
  41. // 获取路由菜单数据
  42. const res = await getBackEndControlRoutes();
  43. // 无登录权限时,添加判断
  44. // https://gitee.com/lyt-top/vue-next-admin/issues/I64HVO
  45. if (res == undefined || res.length <= 0) return Promise.resolve(true);
  46. // 存储接口原始路由(未处理component),根据需求选择使用
  47. useRequestOldRoutes().setRequestOldRoutes(res as string[]);
  48. // 处理路由(component),替换 dynamicRoutes(/@/router/route)第一个顶级 children 的路由
  49. dynamicRoutes[0].children = await backEndComponent(res);
  50. // 添加动态路由
  51. await setAddRoute();
  52. // 设置路由到 pinia routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
  53. setFilterMenuAndCacheTagsViewRoutes();
  54. }
  55. /**
  56. * 设置路由到 pinia routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
  57. * @description 用于左侧菜单、横向菜单的显示
  58. * @description 用于 tagsView、菜单搜索中:未过滤隐藏的(isHide)
  59. */
  60. export async function setFilterMenuAndCacheTagsViewRoutes() {
  61. const storesRoutesList = useRoutesList(pinia);
  62. storesRoutesList.setRoutesList(dynamicRoutes[0].children as any);
  63. setCacheTagsViewRoutes();
  64. }
  65. /**
  66. * 缓存多级嵌套数组处理后的一维数组
  67. * @description 用于 tagsView、菜单搜索中:未过滤隐藏的(isHide)
  68. */
  69. export function setCacheTagsViewRoutes() {
  70. const storesTagsView = useTagsViewRoutes(pinia);
  71. storesTagsView.setTagsViewRoutes(formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes))[0].children);
  72. }
  73. /**
  74. * 处理路由格式及添加捕获所有路由或 404 Not found 路由
  75. * @description 替换 dynamicRoutes(/@/router/route)第一个顶级 children 的路由
  76. * @returns 返回替换后的路由数组
  77. */
  78. export function setFilterRouteEnd() {
  79. let filterRouteEnd: any = formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes));
  80. // notFoundAndNoPower 防止 404、401 不在 layout 布局中,不设置的话,404、401 界面将全屏显示
  81. // 关联问题 No match found for location with path 'xxx'
  82. filterRouteEnd[0].children = [...filterRouteEnd[0].children, ...notFoundAndNoPower];
  83. return filterRouteEnd;
  84. }
  85. /**
  86. * 添加动态路由
  87. * @method router.addRoute
  88. * @description 此处循环为 dynamicRoutes(/@/router/route)第一个顶级 children 的路由一维数组,非多级嵌套
  89. * @link 参考:https://next.router.vuejs.org/zh/api/#addroute
  90. */
  91. export async function setAddRoute() {
  92. await setFilterRouteEnd().forEach((route: RouteRecordRaw) => {
  93. router.addRoute(route);
  94. });
  95. }
  96. /**
  97. * 请求后端路由菜单接口
  98. * @description isRequestRoutes 为 true,则开启后端控制路由
  99. * @returns 返回后端路由菜单数据
  100. */
  101. export async function getBackEndControlRoutes() {
  102. var res = await getAPI(SysMenuApi).apiSysMenuLoginMenuTreeGet();
  103. // if (res.data.result == undefined || res.data.result.length < 1) {
  104. // ElMessage.error('没有任何菜单权限,请联系管理员!');
  105. // setTimeout(() => {
  106. // Session.removeToken();
  107. // window.location.reload();
  108. // }, 3000);
  109. // }
  110. return res.data.result;
  111. }
  112. /**
  113. * 重新请求后端路由菜单接口
  114. * @description 用于菜单管理界面刷新菜单(未进行测试)
  115. * @description 路径:/src/views/system/menu/component/addMenu.vue
  116. */
  117. export async function setBackEndControlRefreshRoutes() {
  118. await getBackEndControlRoutes();
  119. }
  120. /**
  121. * 后端路由 component 转换
  122. * @param routes 后端返回的路由表数组
  123. * @returns 返回处理成函数后的 component
  124. */
  125. export function backEndComponent(routes: any) {
  126. if (!routes) return;
  127. return routes.map((item: any) => {
  128. if (item.component) item.component = dynamicImport(dynamicViewsModules, item.component as string);
  129. item.children && backEndComponent(item.children);
  130. return item;
  131. });
  132. }
  133. /**
  134. * 后端路由 component 转换函数
  135. * @param dynamicViewsModules 获取目录下的 .vue、.tsx 全部文件
  136. * @param component 当前要处理项 component
  137. * @returns 返回处理成函数后的 component
  138. */
  139. export function dynamicImport(dynamicViewsModules: Record<string, Function>, component: string) {
  140. const keys = Object.keys(dynamicViewsModules);
  141. const matchKeys = keys.filter((key) => {
  142. const k = key.replace(/..\/views|../, '');
  143. return k.startsWith(`${component}`) || k.startsWith(`/${component}`);
  144. });
  145. if (matchKeys?.length === 1) {
  146. const matchKey = matchKeys[0];
  147. return dynamicViewsModules[matchKey];
  148. }
  149. if (matchKeys?.length > 1) {
  150. return false;
  151. }
  152. }