/* eslint-disable */
|
/**柯礼钦
|
* 4/2/2020, 11:22:51 AM
|
* doc comment for the file goes here
|
*/
|
|
/** 菜单组件 */
|
|
import React from 'react';
|
import { Spin, Layout, Menu, Icon } from 'antd';
|
import MenView from '../../components/common/MenuView';
|
import { oaMenus, getMenuListByPermission } from '../../menu';
|
import fetch from '../../api/request';
|
import { Context } from '../../index';
|
|
const { Sider } = Layout;
|
|
function find(data, pathname) {
|
for (const { path, children } of data) {
|
if (path === pathname) return path;
|
if (children) {
|
const result = find(children, pathname);
|
if (result) return result;
|
}
|
}
|
return null;
|
}
|
|
function selected(data, pathname) {
|
if (!pathname || pathname === '/') return pathname;
|
return find(data, pathname) || selected(data, pathname.split('/').slice(0, -1).join('/'));
|
}
|
|
export default class MenuView extends React.Component {
|
// static contextType = context;
|
constructor(props) {
|
super(props);
|
this.state = {
|
collapsed: true,
|
menusListByRole: null,
|
menusList: null,
|
};
|
}
|
|
componentDidMount() {
|
// 获取从登录页面记录在缓存里的菜单数据
|
let menusListByRole = window.localStorage.getItem('menusListByRole') ? JSON.parse(window.localStorage.getItem('menusListByRole')) : null;
|
let menu = window.localStorage.getItem('menu') ? JSON.parse(window.localStorage.getItem('menu')) : [];//获取权限菜单
|
let administration = menu.find(({ moduleSymbol }) => moduleSymbol == 'administration');
|
let menusList = [];
|
if (administration) {
|
// setSelectList(selectList.concat({ name: '用户中心', key: 'user' }));
|
let temp = ["oa", "user"];
|
menusList = temp.reduce((p, n) => {
|
return p.concat(menusListByRole[n].menus)
|
}, []);
|
} else {
|
menusList = menusListByRole['oa'].menus
|
}
|
this.setState({
|
menusList
|
})
|
// this.setState({ menusListByRole });
|
}
|
|
onCollapse = collapsed => {
|
this.setState({ collapsed });
|
}
|
|
render() {
|
const { collapsed, menusListByRole, menusList } = this.state;
|
return (
|
<Context.Consumer>
|
{({ role }) => (
|
<React.Fragment>
|
<Sider
|
onCollapse={this.onCollapse}
|
collapsed={collapsed}
|
breakpoint="lg"
|
collapsible={true}
|
>
|
{
|
menusList &&
|
<MenView
|
history={this.props.history}
|
pathname={selected(menusList, this.props.location.pathname)}
|
menudata={menusList || []}
|
/>
|
}
|
</Sider>
|
</React.Fragment>
|
)}
|
</Context.Consumer>
|
);
|
}
|
|
}
|