mirror of
https://github.com/jeecgboot/JeecgBoot.git
synced 2026-01-04 04:45:28 +08:00
前端和后端源码,合并到一个git仓库中,方便用户下载,避免前后端不匹配的问题
This commit is contained in:
133
jeecgboot-vue3/src/router/helper/menuHelper.ts
Normal file
133
jeecgboot-vue3/src/router/helper/menuHelper.ts
Normal file
@ -0,0 +1,133 @@
|
||||
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 { RouteParams } from 'vue-router';
|
||||
import { toRaw } from 'vue';
|
||||
|
||||
export function getAllParentPath<T = Recordable>(treeData: T[], path: string) {
|
||||
// update-begin--author:sunjianlei---date:220230426---for:【issues/478】修复菜单展开合并BUG
|
||||
// 原代码
|
||||
// const menuList = findPath(treeData, (n) => n.path === path) as Menu[];
|
||||
// 先匹配不包含隐藏菜单的路径
|
||||
let menuList = findMenuPath(treeData, path, false);
|
||||
// 如果没有匹配到,再匹配包含隐藏菜单的路径
|
||||
if(!(menuList?.length)) {
|
||||
menuList = findMenuPath(treeData, path, true)
|
||||
}
|
||||
// update-end--author:sunjianlei---date:220230426---for:【issues/478】修复菜单展开合并BUG
|
||||
return (menuList || []).map((item) => item.path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找菜单路径
|
||||
*
|
||||
* @param treeData
|
||||
* @param path
|
||||
* @param matchHide 是否匹配隐藏菜单
|
||||
*/
|
||||
function findMenuPath<T = Recordable>(treeData: T[], path: string, matchHide: boolean) {
|
||||
return findPath(treeData, (n) => {
|
||||
// 隐藏菜单不参与匹配
|
||||
if(!matchHide && n.hideMenu) {
|
||||
return false;
|
||||
}
|
||||
return n.path === path
|
||||
}) as Menu[];
|
||||
}
|
||||
|
||||
// 路径处理
|
||||
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.
|
||||
// 这允许你利用组件嵌套,而无需使用嵌套 URL。
|
||||
if (!(menu.path.startsWith('/') || isUrl(menu.path))) {
|
||||
// path doesn't start with /, nor is it a url, join parent path
|
||||
// 路径不以 / 开头,也不是 url,加入父路径
|
||||
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) {
|
||||
// 借助 lodash 深拷贝
|
||||
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,
|
||||
...(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));
|
||||
}
|
||||
240
jeecgboot-vue3/src/router/helper/routeHelper.ts
Normal file
240
jeecgboot-vue3/src/router/helper/routeHelper.ts
Normal file
@ -0,0 +1,240 @@
|
||||
import type { AppRouteModule, AppRouteRecordRaw } from '/@/router/types';
|
||||
import type { Router, RouteRecordNormalized } from 'vue-router';
|
||||
|
||||
import { getParentLayout, LAYOUT, EXCEPTION_COMPONENT } from '/@/router/constant';
|
||||
import { cloneDeep, omit } from 'lodash-es';
|
||||
import { warn } from '/@/utils/log';
|
||||
import { createRouter, createWebHashHistory } from 'vue-router';
|
||||
import { getTenantId, getToken } from "/@/utils/auth";
|
||||
import { URL_HASH_TAB, _eval } from '/@/utils';
|
||||
//引入online lib路由
|
||||
import { packageViews } from '/@/utils/monorepo/dynamicRouter';
|
||||
import {useI18n} from "/@/hooks/web/useI18n";
|
||||
|
||||
export type LayoutMapKey = 'LAYOUT';
|
||||
const IFRAME = () => import('/@/views/sys/iframe/FrameBlank.vue');
|
||||
const LayoutContent = () => import('/@/layouts/default/content/index.vue');
|
||||
|
||||
const LayoutMap = new Map<string, () => Promise<typeof import('*.vue')>>();
|
||||
|
||||
LayoutMap.set('LAYOUT', LAYOUT);
|
||||
LayoutMap.set('IFRAME', IFRAME);
|
||||
//微前端qiankun
|
||||
LayoutMap.set('LayoutsContent', LayoutContent);
|
||||
|
||||
let dynamicViewsModules: Record<string, () => Promise<Recordable>>;
|
||||
|
||||
// Dynamic introduction
|
||||
function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) {
|
||||
if (!dynamicViewsModules) {
|
||||
dynamicViewsModules = import.meta.glob('../../views/**/*.{vue,tsx}');
|
||||
//合并online lib路由
|
||||
dynamicViewsModules = Object.assign({}, dynamicViewsModules, packageViews);
|
||||
}
|
||||
if (!routes) return;
|
||||
routes.forEach((item) => {
|
||||
|
||||
//【jeecg-boot/issues/I5N2PN】左侧动态菜单怎么做国际化处理 2022-10-09
|
||||
//菜单支持国际化翻译
|
||||
if (item?.meta?.title) {
|
||||
const { t } = useI18n();
|
||||
if(item.meta.title.includes('t(\'') && t){
|
||||
// update-begin--author:liaozhiyang---date:20230906---for:【QQYUN-6390】eval替换成new Function,解决build警告
|
||||
item.meta.title = new Function('t', `return ${item.meta.title}`)(t);
|
||||
// update-end--author:liaozhiyang---date:20230906---for:【QQYUN-6390】eval替换成new Function,解决build警告
|
||||
}
|
||||
}
|
||||
// update-begin--author:sunjianlei---date:20210918---for:适配旧版路由选项 --------
|
||||
// @ts-ignore 适配隐藏路由
|
||||
if (item?.hidden) {
|
||||
item.meta.hideMenu = true;
|
||||
//是否隐藏面包屑
|
||||
item.meta.hideBreadcrumb = true;
|
||||
}
|
||||
// @ts-ignore 添加忽略路由配置
|
||||
if (item?.route == 0) {
|
||||
item.meta.ignoreRoute = true;
|
||||
}
|
||||
// @ts-ignore 添加是否缓存路由配置
|
||||
item.meta.ignoreKeepAlive = !item?.meta.keepAlive;
|
||||
let token = getToken();
|
||||
let tenantId = getTenantId();
|
||||
// URL支持{{ window.xxx }}占位符变量
|
||||
//update-begin---author:wangshuai ---date:20220711 for:[VUEN-1638]菜单tenantId需要动态生成------------
|
||||
item.component = (item.component || '').replace(/{{([^}}]+)?}}/g, (s1, s2) => _eval(s2)).replace('${token}', token).replace('${tenantId}', tenantId);
|
||||
//update-end---author:wangshuai ---date:20220711 for:[VUEN-1638]菜单tenantId需要动态生成------------
|
||||
// 适配 iframe
|
||||
if (/^\/?http(s)?/.test(item.component as string)) {
|
||||
item.component = item.component.substring(1, item.component.length);
|
||||
}
|
||||
if (/^http(s)?/.test(item.component as string)) {
|
||||
if (item.meta?.internalOrExternal) {
|
||||
// @ts-ignore 外部打开
|
||||
item.path = item.component;
|
||||
// update-begin--author:sunjianlei---date:20220408---for: 【VUEN-656】配置外部网址打不开,原因是带了#号,需要替换一下
|
||||
item.path = item.path.replace('#', URL_HASH_TAB);
|
||||
// update-end--author:sunjianlei---date:20220408---for: 【VUEN-656】配置外部网址打不开,原因是带了#号,需要替换一下
|
||||
} else {
|
||||
// @ts-ignore 内部打开
|
||||
item.meta.frameSrc = item.component;
|
||||
}
|
||||
delete item.component;
|
||||
}
|
||||
// update-end--author:sunjianlei---date:20210918---for:适配旧版路由选项 --------
|
||||
if (!item.component && item.meta?.frameSrc) {
|
||||
item.component = 'IFRAME';
|
||||
}
|
||||
let { component, name } = item;
|
||||
const { children } = item;
|
||||
if (component) {
|
||||
const layoutFound = LayoutMap.get(component.toUpperCase());
|
||||
if (layoutFound) {
|
||||
item.component = layoutFound;
|
||||
} else {
|
||||
// update-end--author:zyf---date:20220307--for:VUEN-219兼容后台返回动态首页,目的适配跟v2版本配置一致 --------
|
||||
if (component.indexOf('dashboard/') > -1) {
|
||||
//当数据标sys_permission中component没有拼接index时前端需要拼接
|
||||
if (component.indexOf('/index') < 0) {
|
||||
component = component + '/index';
|
||||
}
|
||||
}
|
||||
// update-end--author:zyf---date:20220307---for:VUEN-219兼容后台返回动态首页,目的适配跟v2版本配置一致 --------
|
||||
item.component = dynamicImport(dynamicViewsModules, component as string);
|
||||
}
|
||||
} else if (name) {
|
||||
item.component = getParentLayout();
|
||||
}
|
||||
children && asyncImportRoute(children);
|
||||
});
|
||||
}
|
||||
|
||||
function dynamicImport(dynamicViewsModules: Record<string, () => Promise<Recordable>>, component: string) {
|
||||
const keys = Object.keys(dynamicViewsModules);
|
||||
const matchKeys = keys.filter((key) => {
|
||||
const k = key.replace('../../views', '');
|
||||
const startFlag = component.startsWith('/');
|
||||
const endFlag = component.endsWith('.vue') || component.endsWith('.tsx');
|
||||
const startIndex = startFlag ? 0 : 1;
|
||||
const lastIndex = endFlag ? k.length : k.lastIndexOf('.');
|
||||
return k.substring(startIndex, lastIndex) === component;
|
||||
});
|
||||
if (matchKeys?.length === 1) {
|
||||
const matchKey = matchKeys[0];
|
||||
return dynamicViewsModules[matchKey];
|
||||
} else if (matchKeys?.length > 1) {
|
||||
warn(
|
||||
'Please do not create `.vue` and `.TSX` files with the same file name in the same hierarchical directory under the views folder. This will cause dynamic introduction failure'
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Turn background objects into routing objects
|
||||
export function transformObjToRoute<T = AppRouteModule>(routeList: AppRouteModule[]): T[] {
|
||||
routeList.forEach((route) => {
|
||||
const component = route.component as string;
|
||||
if (component) {
|
||||
if (component.toUpperCase() === 'LAYOUT') {
|
||||
route.component = LayoutMap.get(component.toUpperCase());
|
||||
} else {
|
||||
route.children = [cloneDeep(route)];
|
||||
route.component = LAYOUT;
|
||||
route.name = `${route.name}Parent`;
|
||||
route.path = '';
|
||||
const meta = route.meta || {};
|
||||
meta.single = true;
|
||||
meta.affix = false;
|
||||
route.meta = meta;
|
||||
}
|
||||
} else {
|
||||
warn('请正确配置路由:' + route?.name + '的component属性');
|
||||
}
|
||||
route.children && asyncImportRoute(route.children);
|
||||
});
|
||||
return routeList as unknown as T[];
|
||||
}
|
||||
|
||||
/**
|
||||
* 将多级路由转换为二级
|
||||
*/
|
||||
export function flatMultiLevelRoutes(routeModules: AppRouteModule[]) {
|
||||
const modules: AppRouteModule[] = cloneDeep(routeModules);
|
||||
for (let index = 0; index < modules.length; index++) {
|
||||
const routeModule = modules[index];
|
||||
if (!isMultipleRoute(routeModule)) {
|
||||
continue;
|
||||
}
|
||||
promoteRouteLevel(routeModule);
|
||||
}
|
||||
return modules;
|
||||
}
|
||||
|
||||
//提升路由级别
|
||||
function promoteRouteLevel(routeModule: AppRouteModule) {
|
||||
// Use vue-router to splice menus
|
||||
let router: Router | null = createRouter({
|
||||
routes: [routeModule as unknown as RouteRecordNormalized],
|
||||
history: createWebHashHistory(),
|
||||
});
|
||||
|
||||
const routes = router.getRoutes();
|
||||
addToChildren(routes, routeModule.children || [], routeModule);
|
||||
router = null;
|
||||
|
||||
routeModule.children = routeModule.children?.map((item) => omit(item, 'children'));
|
||||
}
|
||||
|
||||
// Add all sub-routes to the secondary route
|
||||
function addToChildren(routes: RouteRecordNormalized[], children: AppRouteRecordRaw[], routeModule: AppRouteModule) {
|
||||
for (let index = 0; index < children.length; index++) {
|
||||
const child = children[index];
|
||||
const route = routes.find((item) => item.name === child.name);
|
||||
if (!route) {
|
||||
continue;
|
||||
}
|
||||
routeModule.children = routeModule.children || [];
|
||||
if (!routeModule.children.find((item) => item.name === route.name)) {
|
||||
routeModule.children?.push(route as unknown as AppRouteModule);
|
||||
}
|
||||
if (child.children?.length) {
|
||||
addToChildren(routes, child.children, routeModule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Determine whether the level exceeds 2 levels
|
||||
function isMultipleRoute(routeModule: AppRouteModule) {
|
||||
if (!routeModule || !Reflect.has(routeModule, 'children') || !routeModule.children?.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const children = routeModule.children;
|
||||
|
||||
let flag = false;
|
||||
for (let index = 0; index < children.length; index++) {
|
||||
const child = children[index];
|
||||
if (child.children?.length) {
|
||||
flag = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
/**
|
||||
* 组件地址前加斜杠处理
|
||||
* @updateBy:lsq
|
||||
* @updateDate:2021-09-08
|
||||
*/
|
||||
export function addSlashToRouteComponent(routeList: AppRouteRecordRaw[]) {
|
||||
routeList.forEach((route) => {
|
||||
let component = route.component as string;
|
||||
if (component) {
|
||||
const layoutFound = LayoutMap.get(component);
|
||||
if (!layoutFound) {
|
||||
route.component = component.startsWith('/') ? component : `/${component}`;
|
||||
}
|
||||
}
|
||||
route.children && addSlashToRouteComponent(route.children);
|
||||
});
|
||||
return routeList as unknown as T[];
|
||||
}
|
||||
Reference in New Issue
Block a user