backEnd.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. import { nextTick } from 'vue';
  2. import { RouteRecordRaw } from 'vue-router';
  3. import pinia from '/@/stores/index';
  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 { dynamicRoutes, 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. import { getAPI } from '/@/utils/axios-utils';
  13. import { SysMenuApi } from '/@/api-services/api';
  14. import { augmentAllowedPathsForAidopHomepage, patchAidopMenuTitles } from '/@/utils/aidopMenuDisplay';
  15. // import { ElMessage } from 'element-plus';
  16. // 后端控制路由
  17. /**
  18. * 获取目录下的 .vue、.tsx 全部文件
  19. * @method import.meta.glob
  20. * @link 参考:https://cn.vitejs.dev/guide/features.html#json
  21. */
  22. const layouModules: any = import.meta.glob('../layout/routerView/*.{vue,tsx}');
  23. const viewsModules: any = import.meta.glob('../views/**/*.{vue,tsx}');
  24. const dynamicViewsModules: Record<string, Function> = Object.assign({}, { ...layouModules }, { ...viewsModules });
  25. const DEFAULT_HOME_REDIRECT = '/dashboard/home';
  26. function normalizeMenuPath(p: string | undefined | null): string {
  27. if (p == null || typeof p !== 'string') return '';
  28. const t = p.trim();
  29. if (!t) return '';
  30. return t.startsWith('/') ? t : `/${t}`;
  31. }
  32. function collectMenuPathsFromTree(menus: any[] | undefined, acc: Set<string>): void {
  33. for (const m of menus || []) {
  34. const np = normalizeMenuPath(m?.path);
  35. if (np) acc.add(np);
  36. if (m?.children?.length) collectMenuPathsFromTree(m.children, acc);
  37. }
  38. }
  39. /** 后端 MenuTypeEnum:2=菜单(可对应页面路由),排除目录与按钮 */
  40. function pickFirstRoutableMenuPath(menus: any[] | undefined): string {
  41. for (const m of menus || []) {
  42. const t = Number(m?.type);
  43. if (t === 2) {
  44. const np = normalizeMenuPath(m?.path);
  45. if (np) return np;
  46. }
  47. const sub = pickFirstRoutableMenuPath(m?.children);
  48. if (sub) return sub;
  49. }
  50. return '';
  51. }
  52. /**
  53. * 个性化首页仅当落在当前用户菜单 path 中时才作为根重定向;
  54. * 回退为「当前用户菜单树里第一个页面菜单」而非写死 /dashboard/home,避免受限角色 404。
  55. */
  56. function resolveHomeRedirect(menuTree: any[]): string {
  57. const fallback = pickFirstRoutableMenuPath(menuTree) || DEFAULT_HOME_REDIRECT;
  58. let hpRaw: string | undefined;
  59. try {
  60. hpRaw = Session.get('homepage') as string;
  61. } catch {
  62. hpRaw = undefined;
  63. }
  64. const hp = normalizeMenuPath(hpRaw);
  65. if (!hp) return fallback;
  66. const allowed = new Set<string>();
  67. collectMenuPathsFromTree(menuTree, allowed);
  68. augmentAllowedPathsForAidopHomepage(menuTree, allowed);
  69. if (allowed.has(hp)) return hp;
  70. const noTrail = hp.replace(/\/$/, '') || '/';
  71. if (allowed.has(noTrail)) return noTrail;
  72. return fallback;
  73. }
  74. /** addRoute 之后:若根 redirect 仍无法匹配(组件解析失败等),再回退到可解析路径 */
  75. async function clampRootRedirectAfterAddRoute(menuTree: any[]): Promise<void> {
  76. await nextTick();
  77. let target = normalizeMenuPath(dynamicRoutes[0].redirect as string) || DEFAULT_HOME_REDIRECT;
  78. const trySet = (p: string) => {
  79. const np = normalizeMenuPath(p);
  80. if (!np) return false;
  81. const { matched } = router.resolve(np);
  82. if (!matched.length) return false;
  83. dynamicRoutes[0].redirect = np;
  84. return true;
  85. };
  86. if (trySet(target)) return;
  87. const fromMenu = pickFirstRoutableMenuPath(menuTree);
  88. if (fromMenu && trySet(fromMenu)) return;
  89. const walk = (nodes: any[] | undefined): string => {
  90. for (const n of nodes || []) {
  91. if (n.component === false) continue;
  92. const p = normalizeMenuPath(n.path);
  93. if (p && p !== '/' && n.component) {
  94. if (router.resolve(p).matched.length) return p;
  95. }
  96. const sub = walk(n.children);
  97. if (sub) return sub;
  98. }
  99. return '';
  100. };
  101. const fromCfg = walk(dynamicRoutes[0].children as any[]);
  102. if (fromCfg && trySet(fromCfg)) return;
  103. trySet(DEFAULT_HOME_REDIRECT);
  104. }
  105. /**
  106. * 后端控制路由:初始化方法,防止刷新时路由丢失
  107. * @method NextLoading 界面 loading 动画开始执行
  108. * @method useUserInfo().setUserInfos() 触发初始化用户信息 pinia
  109. * @method useRequestOldRoutes().setRequestOldRoutes() 存储接口原始路由(未处理component),根据需求选择使用
  110. * @method setAddRoute 添加动态路由
  111. * @method setFilterMenuAndCacheTagsViewRoutes 设置路由到 pinia routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
  112. */
  113. export async function initBackEndControlRoutes() {
  114. // 界面 loading 动画开始执行
  115. if (window.nextLoading === undefined) NextLoading.start();
  116. // 无 token 停止执行下一步
  117. if (!Session.get('token')) return false;
  118. // 触发初始化用户信息 pinia
  119. // https://gitee.com/lyt-top/vue-next-admin/issues/I5F1HP
  120. await useUserInfo().setUserInfos();
  121. await useUserInfo().setConstList();
  122. await useUserInfo().setDictList();
  123. // 获取路由菜单数据
  124. const res = await getBackEndControlRoutes();
  125. // 无登录权限时,添加判断
  126. // https://gitee.com/lyt-top/vue-next-admin/issues/I64HVO
  127. if (res == undefined || res.length <= 0) return Promise.resolve(true);
  128. // Ai-DOP:统一一级目录侧栏文案(与种子 S0–S9 一致,并兼容旧 aidopDirM01–M10)
  129. patchAidopMenuTitles(res as any[]);
  130. // 存储接口原始路由(未处理component),根据需求选择使用
  131. useRequestOldRoutes().setRequestOldRoutes(res as string[]);
  132. // 处理路由(component),替换 dynamicRoutes(/@/router/route)第一个顶级 children 的路由
  133. dynamicRoutes[0].children = await backEndComponent(res);
  134. dynamicRoutes[0].redirect = resolveHomeRedirect(res as any[]);
  135. // 添加动态路由
  136. await setAddRoute();
  137. await clampRootRedirectAfterAddRoute(res as any[]);
  138. // 设置路由到 pinia routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
  139. setFilterMenuAndCacheTagsViewRoutes();
  140. }
  141. /**
  142. * 设置路由到 pinia routesList 中(已处理成多级嵌套路由)及缓存多级嵌套数组处理后的一维数组
  143. * @description 用于左侧菜单、横向菜单的显示
  144. * @description 用于 tagsView、菜单搜索中:未过滤隐藏的(isHide)
  145. */
  146. export async function setFilterMenuAndCacheTagsViewRoutes() {
  147. const storesRoutesList = useRoutesList(pinia);
  148. storesRoutesList.setRoutesList(dynamicRoutes[0].children as any);
  149. setCacheTagsViewRoutes();
  150. }
  151. /**
  152. * 缓存多级嵌套数组处理后的一维数组
  153. * @description 用于 tagsView、菜单搜索中:未过滤隐藏的(isHide)
  154. */
  155. export function setCacheTagsViewRoutes() {
  156. const storesTagsView = useTagsViewRoutes(pinia);
  157. storesTagsView.setTagsViewRoutes(formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes))[0].children);
  158. }
  159. /**
  160. * 处理路由格式及添加捕获所有路由或 404 Not found 路由
  161. * @description 替换 dynamicRoutes(/@/router/route)第一个顶级 children 的路由
  162. * @returns 返回替换后的路由数组
  163. */
  164. export function setFilterRouteEnd() {
  165. let filterRouteEnd: any = formatTwoStageRoutes(formatFlatteningRoutes(dynamicRoutes));
  166. // notFoundAndNoPower 防止 404、401 不在 layout 布局中,不设置的话,404、401 界面将全屏显示
  167. // 关联问题 No match found for location with path 'xxx'
  168. filterRouteEnd[0].children = [...filterRouteEnd[0].children, ...notFoundAndNoPower];
  169. return filterRouteEnd;
  170. }
  171. /**
  172. * 添加动态路由
  173. * @method router.addRoute
  174. * @description 此处循环为 dynamicRoutes(/@/router/route)第一个顶级 children 的路由一维数组,非多级嵌套
  175. * @link 参考:https://next.router.vuejs.org/zh/api/#addroute
  176. */
  177. export async function setAddRoute() {
  178. await setFilterRouteEnd().forEach((route: RouteRecordRaw) => {
  179. router.addRoute(route);
  180. });
  181. }
  182. /**
  183. * 请求后端路由菜单接口
  184. * @description isRequestRoutes 为 true,则开启后端控制路由
  185. * @returns 返回后端路由菜单数据
  186. */
  187. export async function getBackEndControlRoutes() {
  188. var res = await getAPI(SysMenuApi).apiSysMenuLoginMenuTreeGet();
  189. // if (res.data.result == undefined || res.data.result.length < 1) {
  190. // ElMessage.error('没有任何菜单权限,请联系管理员!');
  191. // setTimeout(() => {
  192. // Session.removeToken();
  193. // window.location.reload();
  194. // }, 3000);
  195. // }
  196. return res.data.result;
  197. }
  198. /**
  199. * 重新请求后端路由菜单接口
  200. * @description 用于菜单管理界面刷新菜单(未进行测试)
  201. * @description 路径:/src/views/system/menu/component/addMenu.vue
  202. */
  203. export async function setBackEndControlRefreshRoutes() {
  204. await getBackEndControlRoutes();
  205. }
  206. /**
  207. * 后端路由 component 转换
  208. * @param routes 后端返回的路由表数组
  209. * @returns 返回处理成函数后的 component
  210. */
  211. export function backEndComponent(routes: any) {
  212. if (!routes) return;
  213. return routes.map((item: any) => {
  214. if (!item.path) item.path = ''; // 防止后端返回的路由没有path属性,导致路由报错
  215. if (item.component) item.component = dynamicImport(dynamicViewsModules, item.component as string);
  216. item.children && backEndComponent(item.children);
  217. return item;
  218. });
  219. }
  220. /**
  221. * 后端路由 component 转换函数
  222. * @param dynamicViewsModules 获取目录下的 .vue、.tsx 全部文件
  223. * @param component 当前要处理项 component
  224. * @returns 返回处理成函数后的 component
  225. */
  226. export function dynamicImport(dynamicViewsModules: Record<string, Function>, component: string) {
  227. if (component == null || component === '') return;
  228. const compTrim = String(component).trim();
  229. // 后端目录菜单 component 为「Layout」,实际文件在 layout/routerView/parent.vue(小写),不能仅靠 startsWith 匹配
  230. if (/^layout$/i.test(compTrim)) {
  231. const keys = Object.keys(dynamicViewsModules);
  232. const layoutKey = keys.find((key) => key.replace(/\\/g, '/').includes('/layout/routerView/parent.'));
  233. if (layoutKey) return dynamicViewsModules[layoutKey];
  234. }
  235. const keys = Object.keys(dynamicViewsModules);
  236. const matchKeys = keys.filter((key) => {
  237. const k = key.replace(/..\/views|../, '');
  238. return k.startsWith(`${component}`) || k.startsWith(`/${component}`);
  239. });
  240. if (matchKeys?.length === 1) {
  241. const matchKey = matchKeys[0];
  242. return dynamicViewsModules[matchKey];
  243. }
  244. if (matchKeys?.length > 1) {
  245. return false;
  246. }
  247. }