@antdp/basic-layouts
Version:
544 lines (531 loc) • 16.5 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import Icon from '@ant-design/icons';
import React from 'react';
import { NavLink } from 'react-router-dom';
// @ts-ignore
import { matchRoutes } from '@umijs/max';
import { jsx as _jsx, Fragment as _Fragment } from "react/jsx-runtime";
/**
*
* 1. 对路由权限进行判定
* 2. 保存每个子集对应的父级
* 3. 父级链
*/
export class HandleMenu {
constructor(props) {
this.authMenus = [];
/**所有直接渲染的父级菜单*/
this.parentMenu = [];
this.parentflatMenu = [];
/**子集key对应的父级key*/
this.childParent = new Map([]);
/**key对应子集数据*/
this.childMenu = new Map([]);
// 所有子集菜单 平铺
this.flatMenu = [];
// 所有菜单 平铺
this.flatAllMenu = [];
/**语言转换*/
this.intlLanguage = void 0;
/**路由数据*/
this.routers = [];
/**处理权限后的菜单**/
this.checkAuthMenus = [];
/**当前菜单对应的父级菜单*/
this.pathToParentMenus = new Map([]);
/**是否进行权限校验*/
this.isCheckAuth = false;
/**是否使用*/
this.isTOPLEFT = false;
/**记录上次父级菜单*/
this.preParentPath = '';
/**记录上次菜单*/
this.prePath = '';
var store = ANTD_IS_STORAGE ? sessionStorage : localStorage;
// 所有的 权限菜单
var auth_menus = store.getItem('authMenu');
if (auth_menus) {
this.authMenus = JSON.parse(auth_menus);
} else {
this.authMenus = [];
}
this.routers = props.routers;
this.intlLanguage = props.intlLanguage;
this.isCheckAuth = props.isCheckAuth;
this.isTOPLEFT = props.isTOPLEFT;
if (this.isCheckAuth) {
this.initAuth(this.routers);
} else {
this.checkAuthMenus = this.routers;
}
if (this.intlLanguage) {
this.initLanguage(this.checkAuthMenus);
}
this.initFlat(this.checkAuthMenus);
if (this.isTOPLEFT) {
this.handelSiderMenu(this.checkAuthMenus);
this.getTopMenus(this.checkAuthMenus);
}
/**默认 / 指定所有子集 */
this.childMenu.set('/', this.checkAuthMenus);
}
/**验证权限*/
checkAuth(path) {
if (['/404', '/403', '/500', '/', '/welcome'].includes(path)) {
return true;
}
var finx = this.authMenus.find(item => item === path);
if (finx) {
return true;
}
return false;
}
/**
* 1. 权限处理
* 1. 子集全部没权限,父级也没有权限
* 2. 子集存在权限,父级也存在权限
* 3. 没有权限的自动把 path 转换成 403
* */
initAuth(routers, isParent) {
if (isParent === void 0) {
isParent = false;
}
var child = [];
routers.sort(it => Number(it.order || 0)).forEach(item => {
var {
routes
} = item;
var newItem = _extends({}, item);
if (Array.isArray(routes)) {
newItem.routes = this.initAuth(routes, true);
}
if (!isParent) {
this.checkAuthMenus.push(_extends({}, item));
}
child.push(newItem);
});
return child;
}
/**
* 2. 国际化翻译
* */
initLanguage(menus, parentLocale) {
if (menus === void 0) {
menus = this.checkAuthMenus;
}
if (parentLocale === void 0) {
parentLocale = 'menu';
}
menus.forEach(item => {
var {
name,
locale,
routes
} = item;
var parentLocales = parentLocale;
if (name || locale) {
var _this$intlLanguage;
var _locale = parentLocale + "." + (item.locale || item.name);
parentLocales = _locale;
var localeName = (_this$intlLanguage = this.intlLanguage) == null ? void 0 : _this$intlLanguage.formatMessage({
id: _locale,
defaultMessage: item.name || item.locale
});
item.name = localeName;
item.locale = localeName;
}
if (Array.isArray(routes)) {
this.initLanguage(routes, parentLocales);
}
});
}
/**
* 3. 扁平化数据
*/
initFlat(menus, parentMenu) {
if (menus === void 0) {
menus = this.checkAuthMenus;
}
if (parentMenu === void 0) {
parentMenu = [];
}
menus.forEach(item => {
var {
routes
} = item;
var newparentMenu = [...parentMenu].concat([item]);
if (Array.isArray(routes)) {
this.initFlat(routes, newparentMenu);
}
if (item.path) {
this.flatAllMenu.push(_extends({}, item));
this.pathToParentMenus.set(item.path, newparentMenu);
}
if (item.path && !Array.isArray(routes)) {
this.flatMenu.push(_extends({}, item));
}
});
}
/**
* 4. 处理数据,用于顶部和侧边菜单展示联动
* */
handelSiderMenu(menus, isSider, parentPath) {
if (menus === void 0) {
menus = this.checkAuthMenus;
}
if (isSider === void 0) {
isSider = false;
}
if (parentPath === void 0) {
parentPath = '/';
}
menus.forEach(item => {
var {
routes,
side,
path = ''
} = item;
if (side) {
if (Array.isArray(routes) && routes.length) {
this.childMenu.set(path, routes);
this.handelSiderMenu(routes, true, path);
}
this.childParent.set(path, parentPath);
} else {
if (Array.isArray(routes) && routes.length) {
this.handelSiderMenu(routes);
}
}
if (isSider) {
this.childParent.set(path, parentPath);
} else {
this.childParent.set(path, path);
}
});
}
/**
* 5. 获取顶部渲染数据
* */
getTopMenus(menus, index) {
if (menus === void 0) {
menus = this.checkAuthMenus;
}
if (index === void 0) {
index = 0;
}
var topMenus = [];
menus.forEach(item => {
var {
side,
path,
routes
} = item;
var newItem = _extends({}, item);
if (Array.isArray(routes)) {
newItem.routes = this.getTopMenus(routes, index + 1);
}
if (!side && path) {
topMenus.push(_extends({}, newItem));
this.parentflatMenu.push(_extends({}, newItem));
}
if (index === 0) {
this.parentMenu.push(_extends({}, newItem));
}
});
return topMenus;
}
/**
* 6. 获取导航面包屑
* */
getBreadcrumb(path) {
return this.pathToParentMenus.get(path) || [];
}
/**7. 根据当前路由地址取父级key,再使用父级key进行取渲染内容,如果不是顶部渲染方式,则直接默认*/
getSiderMenus(path) {
if (this.isTOPLEFT) {
var siderMenus = this.childMenu.get(path);
if (!siderMenus) {
siderMenus = this.childMenu.get(this.getParentPath(path));
}
return siderMenus || [];
}
return this.checkAuthMenus;
}
/**8. 获取父级path*/
getParentPath(path) {
var parentPath = this.childParent.get(path) || '/';
return parentPath;
}
// 查询所有父节点和自己是否有权限
getCheckAuthAll(path) {
var node = this.flatAllMenu.find(item => item.path === path);
if (!node) {
return false;
}
var pathList = [];
var currentNode = node;
while (currentNode) {
pathList.push(currentNode.path || '');
currentNode = this.flatAllMenu.find(item => {
var _currentNode;
return item.id === ((_currentNode = currentNode) == null ? void 0 : _currentNode.parentId);
});
}
var hasPermission = true; // 假设有权限
// 从最上层父节点开始逐一判断权限
for (var i = pathList.length - 1; i >= 0; i--) {
var p = pathList[i];
if (!this.checkAuth(p)) {
hasPermission = false;
break;
}
}
return hasPermission;
}
/**9. 获取跳转地址*/
getToPath(path) {
var _currentItem, _currentItem2, _currentItem3;
var [currentItem] = matchRoutes(this.flatAllMenu, path);
if (currentItem && currentItem.route) {
currentItem = currentItem.route;
}
// 1. 先判断是不是 side === true
// const currentItem = this.flatAllMenu.find((item) => item.path === path);
if (((_currentItem = currentItem) == null ? void 0 : _currentItem.path) === '/') {
this.preParentPath = '';
this.prePath = currentItem.redirectTo;
return currentItem.redirectTo;
}
if (!currentItem) {
this.preParentPath = '';
if (this.prePath === '/404') {
return false;
}
this.prePath = '/404';
return '/404';
}
// 查询所有父节点和自己是否有权限
if (!!ANTD_AUTH_CONF && !this.getCheckAuthAll(((_currentItem2 = currentItem) == null ? void 0 : _currentItem2.path) || '')) {
this.prePath = '/403';
this.preParentPath = '';
return '/403';
}
if (!((_currentItem3 = currentItem) != null && _currentItem3.side)) {
this.prePath = currentItem.path || '';
this.preParentPath = '';
return false;
}
if (this.preParentPath === currentItem.path) {
this.prePath = currentItem.path;
return false;
}
this.preParentPath = currentItem.path;
var siderMenus = this.childMenu.get(path);
var findx = siderMenus == null ? void 0 : siderMenus.find(item => item.index || item.redirectTo);
var current = (findx == null ? void 0 : findx.path) || (findx == null ? void 0 : findx.redirectTo);
if (this.prePath === current) {
return false;
}
this.prePath = current;
return current;
}
/**10 更加path地址获取当前配置数据*/
getPathItem(path) {
var currentItem = this.flatAllMenu.find(item => item.path === path);
return currentItem;
}
}
export function isUrl(path) {
/* eslint no-useless-escape:0 */
return /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/.test(path);
}
// Allow menu.js config icon as string or ReactNode
// icon: 'setting',
// icon: 'icon-geren' #For Iconfont ,
// icon: 'http://demo.com/icon.png',
// icon: <Icon type="setting" />,
export function getIcon(icon) {
if (typeof icon === 'string') {
if (isUrl(icon)) {
return /*#__PURE__*/_jsx(Icon, {
component: () => /*#__PURE__*/_jsx("img", {
src: icon,
alt: "icon"
})
});
}
return /*#__PURE__*/_jsx(_Fragment, {});
}
return icon;
}
/**获取菜单渲染**/
export var getSiderMenus = function getSiderMenus(menus, isSider) {
if (menus === void 0) {
menus = [];
}
if (isSider === void 0) {
isSider = false;
}
var _loop = function loop(menus, parentSider) {
if (menus === void 0) {
menus = [];
}
if (parentSider === void 0) {
parentSider = false;
}
return menus.filter(items => {
if (items.hideInMenu || !items.name || isSider && parentSider) {
return false;
}
return true;
}).map(item => {
var {
path,
icon,
name,
routes
} = item;
var ite = {};
if (icon) {
ite.icon = getIcon(icon);
}
if (name) {
ite.label = name;
ite.title = name;
}
if (routes && routes.length && (!item.side || !isSider)) {
ite.children = _loop(routes, item.side);
} else if (path && name) {
ite.label = /*#__PURE__*/_jsx(NavLink, {
to: path,
children: name
});
}
ite.key = path;
return ite;
});
};
return _loop(menus);
};
export var defaultThemeColors = layout => {
if (layout === 'slider') {
return {
light: {
'--primary-slider-bg': 'rgb(36, 37, 37)',
'--primary-header-bg': '#fff',
'--primary-shadow': 'rgba(29,35,41,.05)',
'--primary-slider-trigger-border': '#fff',
'--primary-sider-trigger-text-color': '#fff',
'--primary-header-text-color': 'rgb(36, 37, 37)',
'--primary-title-text-color': '#fff',
'--primary-content-bg': '#f5f5f5',
'itemSelectedBg': 'rgb(24, 144, 255)',
'colorItemBgSelected': '#fff',
'itemActiveBg': '#fff',
'horizontalItemSelectedBg': '#fff',
'itemColor': 'rgba(229, 224, 216, 0.85)',
'itemHoverColor': 'rgba(229, 224, 216, 0.85)',
'itemSelectedColor': '#fff',
'colorBgElevated': 'rgba(229, 224, 216, 0.85)'
},
dark: {
'--primary-slider-bg': 'rgb(36, 37, 37)',
'--primary-header-bg': 'rgb(36, 37, 37)',
'--primary-shadow': 'rgba(13, 13, 13, 0.65)',
'--primary-slider-trigger-border': '#fff',
'--primary-sider-trigger-text-color': '#fff',
'--primary-header-text-color': '#fff',
'--primary-title-text-color': '#fff',
'--primary-content-bg': '#1d1d1d',
'itemSelectedBg': 'rgb(24, 144, 255)',
'colorItemBgSelected': '#fff',
'itemActiveBg': '#fff',
'horizontalItemSelectedBg': '#fff',
'itemColor': 'rgba(229, 224, 216, 0.85)',
'itemHoverColor': 'rgba(229, 224, 216, 0.85)',
'itemSelectedColor': '#fff',
'colorBgElevated': 'rgba(229, 224, 216, 0.85)'
}
};
}
if (layout === 'topleft') {
return {
light: {
'--primary-slider-bg': '#fff',
'--primary-header-bg': '#fff',
'--primary-shadow': 'rgba(0,21,41,.12)',
'--primary-slider-trigger-border': 'rgba(0,0,0,.06)',
'--primary-sider-trigger-text-color': '#1d1d1d',
'--primary-header-text-color': '#1d1d1d',
'--primary-title-text-color': '#1d1d1d',
'--primary-content-bg': '#f5f5f5',
'itemSelectedBg': '#e6f7ff',
'colorItemBgSelected': 'rgba(0, 0, 0, 0.06)',
'itemActiveBg': '#e6f7ff',
'horizontalItemSelectedBg': 'rgba(0, 0, 0, 0.06)',
'itemColor': 'rgba(0, 0, 0, 0.65)',
'itemHoverColor': 'rgba(0, 0, 0, 0.85)',
'itemSelectedColor': 'rgb(24, 144, 255)',
'colorBgElevated': '#fff'
},
dark: {
'--primary-slider-bg': 'rgb(36, 37, 37)',
'--primary-header-bg': 'rgb(36, 37, 37)',
'--primary-shadow': 'rgba(13, 13, 13, 0.65)',
'--primary-slider-trigger-border': '#fff',
'--primary-sider-trigger-text-color': '#fff',
'--primary-header-text-color': '#fff',
'--primary-title-text-color': '#fff',
'--primary-content-bg': '#1d1d1d',
'itemSelectedBg': 'rgb(24, 144, 255)',
'colorItemBgSelected': 'rgb(36, 37, 37)',
'itemActiveBg': '#fff',
'horizontalItemSelectedBg': 'rgb(36, 37, 37)',
'itemColor': 'rgba(229, 224, 216, 0.85)',
'itemHoverColor': 'rgba(229, 224, 216, 0.85)',
'itemSelectedColor': '#fff',
'colorBgElevated': 'rgba(229, 224, 216, 0.85)'
}
};
}
if (layout === 'mix') {
return {
light: {
'--primary-slider-bg': '#fff',
'--primary-header-bg': 'rgb(36, 37, 37)',
'--primary-shadow': 'rgba(0,21,41,.08)',
'--primary-slider-trigger-border': 'rgba(0,0,0,.06)',
'--primary-sider-trigger-text-color': 'rgb(36, 37, 37)',
'--primary-header-text-color': '#fff',
'--primary-title-text-color': '#fff',
'--primary-content-bg': '#f5f5f5',
'itemSelectedBg': '#e6f7ff',
'colorItemBgSelected': 'rgba(0, 0, 0, 0.06)',
'itemActiveBg': '#e6f7ff',
'horizontalItemSelectedBg': 'rgba(0, 0, 0, 0.06)',
'itemColor': 'rgba(0, 0, 0, 0.65)',
'itemHoverColor': 'rgba(0, 0, 0, 0.85)',
'itemSelectedColor': 'rgb(24, 144, 255)',
'colorBgElevated': '#fff'
},
dark: {
'--primary-slider-bg': 'rgb(36, 37, 37)',
'--primary-header-bg': 'rgb(15, 28, 41)',
'--primary-shadow': 'rgba(13, 13, 13, 0.65)',
'--primary-slider-trigger-border': '#fff',
'--primary-sider-trigger-text-color': '#fff',
'--primary-header-text-color': '#fff',
'--primary-title-text-color': '#fff',
'--primary-content-bg': '#1d1d1d',
'itemSelectedBg': 'rgb(24, 144, 255)',
'colorItemBgSelected': '#fff',
'itemActiveBg': '#fff',
'horizontalItemSelectedBg': '#fff',
'itemColor': 'rgba(229, 224, 216, 0.85)',
'itemHoverColor': 'rgba(229, 224, 216, 0.85)',
'itemSelectedColor': '#fff',
'colorBgElevated': 'rgba(229, 224, 216, 0.85)'
}
};
}
};