backEnd.ts 6.8 KB

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