backEnd.ts 6.3 KB

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