menuHelper.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 { RouteParams } from 'vue-router';
  7. import { toRaw } from 'vue';
  8. import { defHttp } from '/@/utils/http/axios';
  9. let currentRouter = '';
  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(to, from) {
  91. let currentBrowseId = '';
  92. if (to.path !== '/sys/log/addBrowseLog') {
  93. const url = '/sys/log/addBrowseLog';
  94. // 生成时间戳函数
  95. const formatTimestamp = () => {
  96. const date = new Date();
  97. return [
  98. date.getFullYear(),
  99. String(date.getMonth() + 1).padStart(2, '0'),
  100. String(date.getDate()).padStart(2, '0'),
  101. String(date.getHours()).padStart(2, '0'),
  102. String(date.getMinutes()).padStart(2, '0'),
  103. String(date.getSeconds()).padStart(2, '0'),
  104. String(date.getMilliseconds()).padStart(3, '0'),
  105. ].join('');
  106. };
  107. // 2. 记录新页面进入日志
  108. currentBrowseId = formatTimestamp();
  109. if (!currentRouter) {
  110. currentRouter = to.fullPath;
  111. try {
  112. await defHttp.post({
  113. url,
  114. params: {
  115. browseId: currentBrowseId,
  116. isEnd: false,
  117. method: to.fullPath,
  118. },
  119. });
  120. console.log('进入页面日志记录成功');
  121. } catch (e) {
  122. console.error('进入页面日志记录失败:', e);
  123. }
  124. } else {
  125. if (from.fullPath === currentRouter) {
  126. try {
  127. currentRouter = '';
  128. await defHttp.post({
  129. url,
  130. params: {
  131. browseId: currentBrowseId,
  132. isEnd: true,
  133. method: from.fullPath,
  134. },
  135. });
  136. console.log('进入页面日志记录成功');
  137. } catch (e) {
  138. console.error('进入页面日志记录失败:', e);
  139. }
  140. }
  141. }
  142. }
  143. }