forked from huge/frontEnd/hugeOA

Mr Ke
2020-05-27 4991bc08e414f371c624151f671236fcfed0bf01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/* 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
    };
  }
 
  componentDidMount() {
    // 获取从登录页面记录在缓存里的菜单数据
    let menusListByRole = window.localStorage.getItem('menusListByRole') ? JSON.parse(window.localStorage.getItem('menusListByRole')) : null;
    this.setState({ menusListByRole });
  }
 
  onCollapse = collapsed => {
    this.setState({ collapsed });
  }
 
  render() {
    const { collapsed, menusListByRole } = this.state;
    return (
      <Context.Consumer>
        {({ role }) => (
          <React.Fragment>
            <Sider
              onCollapse={this.onCollapse}
              collapsed={collapsed}
              breakpoint="lg"
              collapsible={true}
            >
              {
                menusListByRole && menusListByRole[role] && 
                <MenView
                  history={this.props.history}
                  pathname={selected(menusListByRole[role].menus, this.props.location.pathname)}
                  menudata={menusListByRole[role].menus || []}
                />
              }
            </Sider>
          </React.Fragment>
        )}
      </Context.Consumer>
    );
  }
 
}