- 修改后台配置菜单

This commit is contained in:
sin 2019-03-12 22:53:39 +08:00
parent 2385f239ab
commit 8d6cfce873

View File

@ -72,20 +72,87 @@ const filterMenuData = menuData => {
.filter(item => item);
};
/**
* 递归构建服务端配置的菜单
* @param resultMenuData
*/
const recursionBuildResultMenu = resultMenuData => {
const res = {};
for (let i = 0; i < resultMenuData.length; i += 1) {
const menuItem = resultMenuData[i];
// 存在子节点
res[menuItem.handler] = {
...menuItem,
// 用于生成uuid
function S4() {
return ((1 + Math.random()) * 0x10000 || 0).toString(16).substring(1);
}
function guid() {
return S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4();
}
const findRootMenu = (antDataMenus, rootAntDataMenu, requestDataMenu) => {
let res;
for (let i = 0; i < antDataMenus.length; i += 1) {
const antDataMenu = antDataMenus[i];
if (antDataMenu.path === requestDataMenu.handler) {
res = rootAntDataMenu;
break;
}
if (antDataMenu.children) {
res = findRootMenu(antDataMenu.children, antDataMenu, requestDataMenu);
break;
}
}
return res;
};
const buildTreeMenu = (antMenuData, moveChildrenMenusData, requestDataMenus) => {
return requestDataMenus.map(item => {
if (!item.handler) {
// root 节点
const uuid = `sms${guid()}`;
const res = {
icon: 'user',
name: item.displayName,
path: uuid,
};
// 子节点
if (item.children) {
// 通过子节点找到对于的父节点,设置 path没有则是 uuid
const rootMenu = findRootMenu(antMenuData, {}, item.children[0]);
if (rootMenu) {
res.path = rootMenu.path;
}
// 开始递归构建数据结构
const childrenMenus = buildTreeMenu(antMenuData, moveChildrenMenusData, item.children);
res.children = childrenMenus;
}
return res;
}
// moveChildrenMenusData 是一个 map对比 url 地址是否存在,不存在就给一个 404 的页面
const handleMapperData = moveChildrenMenusData[item.handler];
if (handleMapperData) {
return {
...handleMapperData,
icon: 'user',
name: item.displayName,
path: item.handler,
};
}
// 没有就返回404页面
return moveChildrenMenusData['/exception/404'];
});
};
const moveChildrenMenus = antDataMenus => {
let res = {};
for (let i = 0; i < antDataMenus.length; i += 1) {
const antDataMenu = antDataMenus[i];
res[antDataMenu.path] = {
...res,
...antDataMenu,
};
if (menuItem.children) {
res[menuItem.handler].children = recursionBuildResultMenu(menuItem.children);
if (antDataMenu.children) {
const childrenMenus = moveChildrenMenus(antDataMenu.children);
res = {
...res,
...childrenMenus,
};
}
}
return res;
@ -126,21 +193,15 @@ export default {
*getMenuData({ payload }, { put, call }) {
const { data } = yield call(getAdminMenus);
const { routes, authority } = payload;
// authority 已经不适用
const antMenuData = filterMenuData(memoizeOneFormatter(routes, authority));
let menuData = antMenuData;
const resultMenuData = data;
// const resultMenuData = data;
if (data !== 'all') {
// 处理后台数据结构
const buildResultMenu = recursionBuildResultMenu(resultMenuData);
// 过滤没有权限的菜单
menuData = antMenuData.filter(item => {
if (buildResultMenu[item.path]) {
return item;
}
return false;
});
const moveChildrenMenusData = moveChildrenMenus(antMenuData);
const buildTreeMenuData = buildTreeMenu(antMenuData, moveChildrenMenusData, data);
menuData = buildTreeMenuData;
}
// 生成 menu 和 router mapping