Info.js 2.83 KB
Newer Older
jim's avatar
jim committed
1
import React, { Component } from 'react';
陈帅's avatar
陈帅 committed
2 3 4 5
import { connect } from 'dva';
import { Route, routerRedux, Switch, Redirect } from 'dva/router';
import { Menu } from 'antd';
import styles from './Info.less';
ddcat1115's avatar
ddcat1115 committed
6
import { getRoutes } from '../../../utils/utils';
陈帅's avatar
陈帅 committed
7 8 9 10 11 12 13 14 15 16 17 18 19

const { Item } = Menu;

const menuMap = {
  base: '基本设置',
  safe: '安全设置',
  account: '账号绑定',
  message: '新消息通知',
};

@connect(({ user }) => ({
  currentUser: user.currentUser,
}))
jim's avatar
jim committed
20
export default class Info extends Component {
陈帅's avatar
陈帅 committed
21 22 23 24 25 26 27
  constructor(props) {
    super(props);
    const { match, location } = props;
    let key = location.pathname.replace(`${match.path}/`, '');
    key = menuMap[key] ? key : 'base';
    this.state = {
      selectKey: key,
jim's avatar
jim committed
28
      mode: 'inline',
陈帅's avatar
陈帅 committed
29 30
    };
  }
jim's avatar
jim committed
31 32 33 34 35 36 37
  componentDidMount() {
    window.addEventListener('resize', this.resize);
    this.resize();
  }
  componentWillUnmount() {
    window.removeEventListener('resize', this.resize);
  }
陈帅's avatar
陈帅 committed
38
  getmenu = () => {
jim's avatar
jim committed
39 40 41
    return Object.keys(menuMap).map(item => (
      <Item key={item}>{menuMap[item]}</Item>
    ));
陈帅's avatar
陈帅 committed
42 43 44 45 46
  };
  getRightTitle = () => {
    return menuMap[this.state.selectKey];
  };
  selectKey = ({ key }) => {
ddcat1115's avatar
ddcat1115 committed
47
    this.props.dispatch(routerRedux.push(`/user-profile/userinfo/${key}`));
陈帅's avatar
陈帅 committed
48 49 50 51
    this.setState({
      selectKey: key,
    });
  };
jim's avatar
jim committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  resize = () => {
    if (!this.main) {
      return;
    }
    let mode = 'inline';
    const { offsetWidth } = this.main;
    if (this.main.offsetWidth < 641 && offsetWidth > 400) {
      mode = 'horizontal';
    }
    if (window.innerWidth < 768 && offsetWidth > 400) {
      mode = 'horizontal';
    }
    this.setState({
      mode,
    });
  };
陈帅's avatar
陈帅 committed
68 69 70 71 72 73
  render() {
    const { match, routerData, currentUser } = this.props;
    if (!currentUser.userid) {
      return '';
    }
    return (
jim's avatar
jim committed
74 75 76 77 78 79
      <div
        className={styles.main}
        ref={(ref) => {
          this.main = ref;
        }}
      >
陈帅's avatar
陈帅 committed
80 81
        <div className={styles.leftmenu}>
          <Menu
jim's avatar
jim committed
82
            mode={this.state.mode}
陈帅's avatar
陈帅 committed
83 84 85 86 87 88 89 90 91
            selectedKeys={[this.state.selectKey]}
            onClick={this.selectKey}
          >
            {this.getmenu()}
          </Menu>
        </div>
        <div className={styles.right}>
          <div className={styles.title}>{this.getRightTitle()}</div>
          <Switch>
jim's avatar
jim committed
92 93 94 95 96 97 98 99 100 101
            {getRoutes(match.path, routerData).map(item => (
              <Route
                key={item.key}
                path={item.path}
                render={props => (
                  <item.component {...props} currentUser={currentUser} />
                )}
                exact={item.exact}
              />
            ))}
jim's avatar
jim committed
102 103 104 105 106
            <Redirect
              exact
              from="/user-profile/userinfo"
              to="/user-profile/userinfo/base"
            />
陈帅's avatar
陈帅 committed
107 108 109 110 111 112 113
            <Redirect to="/exception/404" />
          </Switch>
        </div>
      </div>
    );
  }
}