menuHelper.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { AppRouteModule } from '/@/router/types';
  2. import type { MenuModule, Menu, AppRouteRecordRaw } from '/@/router/types';
  3. import { findPath, treeMap } from '/@/utils/helper/treeHelper';
  4. import { cloneDeep } from 'lodash-es';
  5. import { isUrl } from '/@/utils/is';
  6. import { RouteLocationNormalized, RouteParams } from 'vue-router';
  7. import { toRaw } from 'vue';
  8. import { defHttp } from '/@/utils/http/axios';
  9. import { Form } from 'ant-design-vue';
  10. export function getAllParentPath<T = Recordable>(treeData: T[], path: string) {
  11. const menuList = findPath(treeData, (n) => n.path === path) as Menu[];
  12. return (menuList || []).map((item) => item.path);
  13. }
  14. function joinParentPath(menus: Menu[], parentPath = '') {
  15. for (let index = 0; index < menus.length; index++) {
  16. const menu = menus[index];
  17. // https://next.router.vuejs.org/guide/essentials/nested-routes.html
  18. // Note that nested paths that start with / will be treated as a root path.
  19. // This allows you to leverage the component nesting without having to use a nested URL.
  20. if (!(menu.path.startsWith('/') || isUrl(menu.path))) {
  21. // path doesn't start with /, nor is it a url, join parent path
  22. menu.path = `${parentPath}/${menu.path}`;
  23. }
  24. if (menu?.children?.length) {
  25. joinParentPath(menu.children, menu.meta?.hidePathForChildren ? parentPath : menu.path);
  26. }
  27. }
  28. }
  29. // Parsing the menu module
  30. export function transformMenuModule(menuModule: MenuModule): Menu {
  31. const { menu } = menuModule;
  32. const menuList = [menu];
  33. joinParentPath(menuList);
  34. return menuList[0];
  35. }
  36. export function transformRouteToMenu(routeModList: AppRouteModule[], routerMapping = false) {
  37. const cloneRouteModList = cloneDeep(routeModList);
  38. const routeList: AppRouteRecordRaw[] = [];
  39. cloneRouteModList.forEach((item) => {
  40. if (routerMapping && item.meta.hideChildrenInMenu && typeof item.redirect === 'string') {
  41. item.path = item.redirect;
  42. }
  43. if (item.meta?.single) {
  44. const realItem = item?.children?.[0];
  45. realItem && routeList.push(realItem);
  46. } else {
  47. routeList.push(item);
  48. }
  49. });
  50. const list = treeMap(routeList, {
  51. conversion: (node: AppRouteRecordRaw) => {
  52. const { meta: { title, hideMenu = false } = {} } = node;
  53. return {
  54. ...(node.meta || {}),
  55. meta: node.meta,
  56. name: title,
  57. hideMenu,
  58. alwaysShow: node.alwaysShow || false,
  59. path: node.path,
  60. ver: node.ver,
  61. ...(node.redirect ? { redirect: node.redirect } : {}),
  62. };
  63. },
  64. });
  65. joinParentPath(list);
  66. return cloneDeep(list);
  67. }
  68. /**
  69. * config menu with given params
  70. */
  71. const menuParamRegex = /(?::)([\s\S]+?)((?=\/)|$)/g;
  72. export function configureDynamicParamsMenu(menu: Menu, params: RouteParams) {
  73. const { path, paramPath } = toRaw(menu);
  74. let realPath = paramPath ? paramPath : path;
  75. const matchArr = realPath.match(menuParamRegex);
  76. matchArr?.forEach((it) => {
  77. const realIt = it.substr(1);
  78. if (params[realIt]) {
  79. realPath = realPath.replace(`:${realIt}`, params[realIt] as string);
  80. }
  81. });
  82. // save original param path.
  83. if (!paramPath && matchArr && matchArr.length > 0) {
  84. menu.paramPath = path;
  85. }
  86. menu.path = realPath;
  87. // children
  88. menu.children?.forEach((item) => configureDynamicParamsMenu(item, params));
  89. }
  90. export async function addBrowseLog(
  91. to: AppRouteRecordRaw | RouteLocationNormalized,
  92. from: AppRouteRecordRaw | RouteLocationNormalized,
  93. ignorePaths: string[]
  94. ): Promise<void> {
  95. const currentPort = window.location.port;
  96. if (currentPort === '8062') {
  97. return Promise.resolve();
  98. }
  99. // 在尝试添加浏览器记录时,应该先发出一个结束上次记录的请求(如有)再发出一个开发此次记录的请求
  100. const url = '/sys/log/addBrowseLog';
  101. // meta.browseId 是该方法动态添加的内容,可用于判断该路径是否记录
  102. if (from.meta.browseId) {
  103. await defHttp.post(
  104. {
  105. url,
  106. params: {
  107. browseId: from.meta.browseId,
  108. isEnd: true,
  109. method: from.fullPath,
  110. },
  111. },
  112. {
  113. errorMessageMode: 'none',
  114. successMessageMode: 'none',
  115. }
  116. );
  117. }
  118. if (!ignorePaths.includes(to.path)) {
  119. const timestamp = Date.now();
  120. to.meta.browseId = timestamp;
  121. await defHttp.post(
  122. {
  123. url,
  124. params: {
  125. browseId: timestamp,
  126. isEnd: false,
  127. method: to.fullPath,
  128. },
  129. },
  130. {
  131. errorMessageMode: 'none',
  132. successMessageMode: 'none',
  133. }
  134. );
  135. }
  136. // if (to.path !== '/sys/log/addBrowseLog') {
  137. // // 2. 记录新页面进入日志
  138. // currentBrowseId = formatTimestamp();
  139. // if (!currentRouter) {
  140. // currentRouter = to.fullPath;
  141. // try {
  142. // console.log('进入页面日志记录成功');
  143. // } catch (e) {
  144. // console.error('进入页面日志记录失败:', e);
  145. // }
  146. // } else {
  147. // if (from.fullPath === currentRouter) {
  148. // try {
  149. // currentRouter = '';
  150. // await defHttp.post({
  151. // url,
  152. // params: {
  153. // browseId: currentBrowseId,
  154. // isEnd: true,
  155. // method: from.fullPath,
  156. // },
  157. // });
  158. // console.log('进入页面日志记录成功');
  159. // } catch (e) {
  160. // console.error('进入页面日志记录失败:', e);
  161. // }
  162. // }
  163. // }
  164. // }
  165. }