forked from huge/frontEnd/hugeOA

1
liuwh
2020-04-06 6cb2044ba90ed2d3a43760f226eadcc65fa4e5a6
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 { menus, getMenuListByPermission } from '../../menu';
import fetch from '../../api/request';
 
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 {
  constructor(props) {
    super(props);
    this.state = {
      collapsed: true,
      data: []
    };
  }
 
  componentWillMount() {
    // 获取从登录页面记录在缓存里的菜单数据
    let menu = window.localStorage.getItem('menu') ? JSON.parse(window.localStorage.getItem('menu')) : [];
    let permList = menu && menu.map(({ symbol }) => (symbol)).concat(menu.map(({ moduleSymbol }) => (moduleSymbol))).reduce((p, n) => {
      if (p.indexOf(n) == -1) {
        return p.concat(n)
      } else {
        return p
      }
    }, []);
    console.log(getMenuListByPermission(menus, permList) )
    this.setState({ data: getMenuListByPermission(menus, permList) });
  }
  componentDidMount() { }
 
  onCollapse = collapsed => {
    this.setState({ collapsed });
  }
 
  render() {
    const { data, collapsed } = this.state;
    const pathname = selected(data, this.props.location.pathname);
    return (
      <Sider
        onCollapse={this.onCollapse}
        collapsed={collapsed}
        breakpoint="lg"
        collapsible={true}
      >
        <MenView
          history={this.props.history}
          pathname={pathname}
          key={data.length}
          menudata={data}
        />
      </Sider>
    );
  }
 
}