import { request } from '@umijs/max';
|
import React from 'react';
|
import { createIcon } from '@/utils/icon-util';
|
|
let remoteMenu = null;
|
|
export function getRemoteMenu() {
|
return remoteMenu;
|
}
|
|
export function setRemoteMenu(data) {
|
remoteMenu = data;
|
}
|
|
export async function getUserInfo(options) {
|
return request('/api/auth/user', {
|
method: 'GET',
|
...(options || {}),
|
});
|
}
|
|
export async function getRouters() {
|
return request('/api/auth/menu', {
|
});
|
}
|
|
export async function getRoutersInfo() {
|
return getRouters().then((res) => {
|
if (res.code === 200) {
|
// return res.data;
|
const routersInfo = convertCompatRouters(res.data);
|
return routersInfo;
|
} else {
|
return [];
|
}
|
});
|
}
|
|
export function convertCompatRouters(childrens) {
|
const result = childrens.map((item) => {
|
return {
|
name: item.name,
|
path: item.route,
|
component: item.component ? '.' + item.component : null,
|
icon: createIcon(item.icon),
|
// icon: item.icon,
|
// hideChildrenInMenu: item.hidden,
|
// hideInMenu: item.hidden,
|
// authority: item.perms,
|
routes: item.children ? convertCompatRouters(item.children) : undefined,
|
};
|
});
|
return result;
|
}
|
|
export function patchRouteWithRemoteMenus(routes) {
|
if (remoteMenu === null) { return; }
|
let proLayout = null;
|
for (const routeItem of routes) {
|
if (routeItem.id === 'ant-design-pro-layout') {
|
proLayout = routeItem;
|
break;
|
}
|
}
|
|
const rootMenu = { routes: [], children: [] }
|
addHomeMenu(remoteMenu);
|
addUserSettingMenu(remoteMenu);
|
patchRouteItems(rootMenu, remoteMenu);
|
|
proLayout.children = proLayout.children.concat(rootMenu.routes);
|
proLayout.routes = proLayout.routes.concat(rootMenu.routes);
|
}
|
|
function patchRouteItems(parent, children) {
|
for (const menu of children) {
|
if (menu.component !== null && menu.component !== undefined) {
|
// children
|
const Component = require(`@/pages${menu.path}/index.jsx`).default
|
const newRoute = {
|
name: menu.name,
|
path: menu.path,
|
element: <Component />,
|
}
|
parent.routes.push(newRoute);
|
parent.children.push(newRoute);
|
} else {
|
// parent
|
const newRoute = {
|
name: menu.name,
|
path: menu.path,
|
routes: [],
|
children: [],
|
}
|
parent.routes.push(newRoute);
|
parent.children.push(newRoute);
|
if (menu.routes && menu.routes.length > 0) {
|
for (const route of menu.routes) {
|
patchRouteItems(newRoute, [route]);
|
};
|
|
}
|
}
|
}
|
}
|
|
function addHomeMenu(remoteMenu) {
|
remoteMenu.unshift({
|
name: "首页",
|
path: "/home",
|
component: "/home",
|
icon: createIcon('HomeOutlined')
|
})
|
}
|
|
function addUserSettingMenu(remoteMenu) {
|
// const settingRoute = {
|
// name: "个人设置",
|
// path: "/account/setting",
|
// component: "/account/setting"
|
// }
|
// remoteMenu.push({
|
// name: "个人中心",
|
// path: "/account",
|
// component: null,
|
// routes: [settingRoute],
|
// icon: createIcon('UserOutlined')
|
// })
|
|
remoteMenu.push({
|
name: "个人中心",
|
path: "/account/setting",
|
component: "/account/setting",
|
icon: createIcon('UserOutlined')
|
})
|
}
|