Info.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React, { Component } from 'react';
  2. import { connect } from 'dva';
  3. import { Route, routerRedux, Switch, Redirect } from 'dva/router';
  4. import { Menu } from 'antd';
  5. import styles from './Info.less';
  6. import { getRoutes } from '../../../utils/utils';
  7. const { Item } = Menu;
  8. const menuMap = {
  9. base: '基本设置',
  10. safe: '安全设置',
  11. account: '账号绑定',
  12. message: '新消息通知',
  13. };
  14. @connect(({ user }) => ({
  15. currentUser: user.currentUser,
  16. }))
  17. export default class Info extends Component {
  18. constructor(props) {
  19. super(props);
  20. const { match, location } = props;
  21. let key = location.pathname.replace(`${match.path}/`, '');
  22. key = menuMap[key] ? key : 'base';
  23. this.state = {
  24. selectKey: key,
  25. mode: 'inline',
  26. };
  27. }
  28. componentDidMount() {
  29. window.addEventListener('resize', this.resize);
  30. this.resize();
  31. }
  32. componentWillUnmount() {
  33. window.removeEventListener('resize', this.resize);
  34. }
  35. getmenu = () => {
  36. return Object.keys(menuMap).map(item => (
  37. <Item key={item}>{menuMap[item]}</Item>
  38. ));
  39. };
  40. getRightTitle = () => {
  41. return menuMap[this.state.selectKey];
  42. };
  43. selectKey = ({ key }) => {
  44. this.props.dispatch(routerRedux.push(`/user-profile/userinfo/${key}`));
  45. this.setState({
  46. selectKey: key,
  47. });
  48. };
  49. resize = () => {
  50. if (!this.main) {
  51. return;
  52. }
  53. let mode = 'inline';
  54. const { offsetWidth } = this.main;
  55. if (this.main.offsetWidth < 641 && offsetWidth > 400) {
  56. mode = 'horizontal';
  57. }
  58. if (window.innerWidth < 768 && offsetWidth > 400) {
  59. mode = 'horizontal';
  60. }
  61. this.setState({
  62. mode,
  63. });
  64. };
  65. render() {
  66. const { match, routerData, currentUser } = this.props;
  67. if (!currentUser.userid) {
  68. return '';
  69. }
  70. return (
  71. <div
  72. className={styles.main}
  73. ref={(ref) => {
  74. this.main = ref;
  75. }}
  76. >
  77. <div className={styles.leftmenu}>
  78. <Menu
  79. mode={this.state.mode}
  80. selectedKeys={[this.state.selectKey]}
  81. onClick={this.selectKey}
  82. >
  83. {this.getmenu()}
  84. </Menu>
  85. </div>
  86. <div className={styles.right}>
  87. <div className={styles.title}>{this.getRightTitle()}</div>
  88. <Switch>
  89. {getRoutes(match.path, routerData).map(item => (
  90. <Route
  91. key={item.key}
  92. path={item.path}
  93. render={props => (
  94. <item.component {...props} currentUser={currentUser} />
  95. )}
  96. exact={item.exact}
  97. />
  98. ))}
  99. <Redirect
  100. exact
  101. from="/user-profile/userinfo"
  102. to="/user-profile/userinfo/base"
  103. />
  104. <Redirect to="/exception/404" />
  105. </Switch>
  106. </div>
  107. </div>
  108. );
  109. }
  110. }