| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- import { AppRouteModule } from '/@/router/types';
- import type { MenuModule, Menu, AppRouteRecordRaw } from '/@/router/types';
- import { findPath, treeMap } from '/@/utils/helper/treeHelper';
- import { cloneDeep } from 'lodash-es';
- import { isUrl } from '/@/utils/is';
- import { RouteLocationNormalized, RouteParams } from 'vue-router';
- import { toRaw } from 'vue';
- import { defHttp } from '/@/utils/http/axios';
- import { Form } from 'ant-design-vue';
- export function getAllParentPath<T = Recordable>(treeData: T[], path: string) {
- const menuList = findPath(treeData, (n) => n.path === path) as Menu[];
- return (menuList || []).map((item) => item.path);
- }
- function joinParentPath(menus: Menu[], parentPath = '') {
- for (let index = 0; index < menus.length; index++) {
- const menu = menus[index];
- // https://next.router.vuejs.org/guide/essentials/nested-routes.html
- // Note that nested paths that start with / will be treated as a root path.
- // This allows you to leverage the component nesting without having to use a nested URL.
- if (!(menu.path.startsWith('/') || isUrl(menu.path))) {
- // path doesn't start with /, nor is it a url, join parent path
- menu.path = `${parentPath}/${menu.path}`;
- }
- if (menu?.children?.length) {
- joinParentPath(menu.children, menu.meta?.hidePathForChildren ? parentPath : menu.path);
- }
- }
- }
- // Parsing the menu module
- export function transformMenuModule(menuModule: MenuModule): Menu {
- const { menu } = menuModule;
- const menuList = [menu];
- joinParentPath(menuList);
- return menuList[0];
- }
- export function transformRouteToMenu(routeModList: AppRouteModule[], routerMapping = false) {
- const cloneRouteModList = cloneDeep(routeModList);
- const routeList: AppRouteRecordRaw[] = [];
- cloneRouteModList.forEach((item) => {
- if (routerMapping && item.meta.hideChildrenInMenu && typeof item.redirect === 'string') {
- item.path = item.redirect;
- }
- if (item.meta?.single) {
- const realItem = item?.children?.[0];
- realItem && routeList.push(realItem);
- } else {
- routeList.push(item);
- }
- });
- const list = treeMap(routeList, {
- conversion: (node: AppRouteRecordRaw) => {
- const { meta: { title, hideMenu = false } = {} } = node;
- return {
- ...(node.meta || {}),
- meta: node.meta,
- name: title,
- hideMenu,
- alwaysShow: node.alwaysShow || false,
- path: node.path,
- ver: node.ver,
- ...(node.redirect ? { redirect: node.redirect } : {}),
- };
- },
- });
- joinParentPath(list);
- return cloneDeep(list);
- }
- /**
- * config menu with given params
- */
- const menuParamRegex = /(?::)([\s\S]+?)((?=\/)|$)/g;
- export function configureDynamicParamsMenu(menu: Menu, params: RouteParams) {
- const { path, paramPath } = toRaw(menu);
- let realPath = paramPath ? paramPath : path;
- const matchArr = realPath.match(menuParamRegex);
- matchArr?.forEach((it) => {
- const realIt = it.substr(1);
- if (params[realIt]) {
- realPath = realPath.replace(`:${realIt}`, params[realIt] as string);
- }
- });
- // save original param path.
- if (!paramPath && matchArr && matchArr.length > 0) {
- menu.paramPath = path;
- }
- menu.path = realPath;
- // children
- menu.children?.forEach((item) => configureDynamicParamsMenu(item, params));
- }
- export async function addBrowseLog(
- to: AppRouteRecordRaw | RouteLocationNormalized,
- from: AppRouteRecordRaw | RouteLocationNormalized,
- ignorePaths: string[]
- ): Promise<void> {
- const currentPort = window.location.port;
- if (currentPort === '8062') {
- return Promise.resolve();
- }
- // 在尝试添加浏览器记录时,应该先发出一个结束上次记录的请求(如有)再发出一个开发此次记录的请求
- const url = '/sys/log/addBrowseLog';
- // meta.browseId 是该方法动态添加的内容,可用于判断该路径是否记录
- if (from.meta.browseId) {
- await defHttp.post(
- {
- url,
- params: {
- browseId: from.meta.browseId,
- isEnd: true,
- method: from.fullPath,
- },
- },
- {
- errorMessageMode: 'none',
- successMessageMode: 'none',
- }
- );
- }
- if (!ignorePaths.includes(to.path)) {
- const timestamp = Date.now();
- to.meta.browseId = timestamp;
- await defHttp.post(
- {
- url,
- params: {
- browseId: timestamp,
- isEnd: false,
- method: to.fullPath,
- },
- },
- {
- errorMessageMode: 'none',
- successMessageMode: 'none',
- }
- );
- }
- // if (to.path !== '/sys/log/addBrowseLog') {
- // // 2. 记录新页面进入日志
- // currentBrowseId = formatTimestamp();
- // if (!currentRouter) {
- // currentRouter = to.fullPath;
- // try {
- // console.log('进入页面日志记录成功');
- // } catch (e) {
- // console.error('进入页面日志记录失败:', e);
- // }
- // } else {
- // if (from.fullPath === currentRouter) {
- // try {
- // currentRouter = '';
- // await defHttp.post({
- // url,
- // params: {
- // browseId: currentBrowseId,
- // isEnd: true,
- // method: from.fullPath,
- // },
- // });
- // console.log('进入页面日志记录成功');
- // } catch (e) {
- // console.error('进入页面日志记录失败:', e);
- // }
- // }
- // }
- // }
- }
|