- 修改后台配置菜单

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); .filter(item => item);
}; };
/** // 用于生成uuid
* 递归构建服务端配置的菜单 function S4() {
* @param resultMenuData return ((1 + Math.random()) * 0x10000 || 0).toString(16).substring(1);
*/ }
const recursionBuildResultMenu = resultMenuData => { function guid() {
const res = {}; return S4() + S4() + S4() + S4() + S4() + S4() + S4() + S4();
for (let i = 0; i < resultMenuData.length; i += 1) { }
const menuItem = resultMenuData[i];
// 存在子节点 const findRootMenu = (antDataMenus, rootAntDataMenu, requestDataMenu) => {
res[menuItem.handler] = { let res;
...menuItem, 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 (antDataMenu.children) {
const childrenMenus = moveChildrenMenus(antDataMenu.children);
res = {
...res,
...childrenMenus,
}; };
if (menuItem.children) {
res[menuItem.handler].children = recursionBuildResultMenu(menuItem.children);
} }
} }
return res; return res;
@ -126,21 +193,15 @@ export default {
*getMenuData({ payload }, { put, call }) { *getMenuData({ payload }, { put, call }) {
const { data } = yield call(getAdminMenus); const { data } = yield call(getAdminMenus);
const { routes, authority } = payload; const { routes, authority } = payload;
// authority 已经不适用 // authority 已经不适用
const antMenuData = filterMenuData(memoizeOneFormatter(routes, authority)); const antMenuData = filterMenuData(memoizeOneFormatter(routes, authority));
let menuData = antMenuData; let menuData = antMenuData;
const resultMenuData = data; // const resultMenuData = data;
if (data !== 'all') { if (data !== 'all') {
// 处理后台数据结构 const moveChildrenMenusData = moveChildrenMenus(antMenuData);
const buildResultMenu = recursionBuildResultMenu(resultMenuData); const buildTreeMenuData = buildTreeMenu(antMenuData, moveChildrenMenusData, data);
// 过滤没有权限的菜单 menuData = buildTreeMenuData;
menuData = antMenuData.filter(item => {
if (buildResultMenu[item.path]) {
return item;
}
return false;
});
} }
// 生成 menu 和 router mapping // 生成 menu 和 router mapping