Header.js 4.16 KB
Newer Older
jim's avatar
jim committed
1 2 3 4
import React, { PureComponent } from 'react';
import { Layout, message } from 'antd';
import Animate from 'rc-animate';
import { connect } from 'dva';
zinkey's avatar
zinkey committed
5
import router from 'umi/router';
6 7
import GlobalHeader from '@/components/GlobalHeader';
import TopNavHeader from '@/components/TopNavHeader';
jim's avatar
jim committed
8
import styles from './Header.less';
9
import Authorized from '@/utils/Authorized';
jim's avatar
jim committed
10 11 12 13 14 15 16

const { Header } = Layout;

class HeaderView extends PureComponent {
  state = {
    visible: true,
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
17

18 19 20 21 22 23 24 25 26
  static getDerivedStateFromProps(props, state) {
    if (!props.autoHideHeader && !state.visible) {
      return {
        visible: true,
      };
    }
    return null;
  }

jim's avatar
jim committed
27
  componentDidMount() {
28
    document.addEventListener('scroll', this.handScroll, { passive: true });
jim's avatar
jim committed
29
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30

jim's avatar
jim committed
31
  componentWillUnmount() {
32
    document.removeEventListener('scroll', this.handScroll);
jim's avatar
jim committed
33
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
34

jim's avatar
jim committed
35
  getHeadWidth = () => {
36 37 38
    const { isMobile, collapsed, setting } = this.props;
    const { fixedHeader, layout } = setting;
    if (isMobile || !fixedHeader || layout === 'topmenu') {
jim's avatar
jim committed
39 40
      return '100%';
    }
41
    return collapsed ? 'calc(100% - 80px)' : 'calc(100% - 256px)';
jim's avatar
jim committed
42
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
43

jim's avatar
jim committed
44
  handleNoticeClear = type => {
jim's avatar
jim committed
45
    message.success(`ζΈ…η©ΊδΊ†${type}`);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
46 47
    const { dispatch } = this.props;
    dispatch({
jim's avatar
jim committed
48 49 50 51
      type: 'global/clearNotices',
      payload: type,
    });
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
52

jim's avatar
jim committed
53
  handleMenuClick = ({ key }) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
54
    const { dispatch } = this.props;
jim's avatar
jim committed
55
    if (key === 'userCenter') {
zinkey's avatar
zinkey committed
56
      router.push('/account/center');
jim's avatar
jim committed
57 58 59
      return;
    }
    if (key === 'triggerError') {
zinkey's avatar
zinkey committed
60
      router.push('/exception/trigger');
jim's avatar
jim committed
61 62 63
      return;
    }
    if (key === 'userinfo') {
zinkey's avatar
zinkey committed
64
      router.push('/account/settings/base');
jim's avatar
jim committed
65 66 67
      return;
    }
    if (key === 'logout') {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
68
      dispatch({
jim's avatar
jim committed
69 70 71 72
        type: 'login/logout',
      });
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
73

jim's avatar
jim committed
74
  handleNoticeVisibleChange = visible => {
jim's avatar
jim committed
75
    if (visible) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
76 77
      const { dispatch } = this.props;
      dispatch({
jim's avatar
jim committed
78 79 80 81
        type: 'global/fetchNotices',
      });
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
82

83
  handScroll = () => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
84 85 86
    const { autoHideHeader } = this.props;
    const { visible } = this.state;
    if (!autoHideHeader) {
jim's avatar
jim committed
87 88
      return;
    }
89
    const scrollTop = document.body.scrollTop + document.documentElement.scrollTop;
jim's avatar
jim committed
90 91
    if (!this.ticking) {
      requestAnimationFrame(() => {
92 93 94 95 96 97 98
        if (this.oldScrollTop > scrollTop) {
          this.setState({
            visible: true,
          });
          this.scrollTop = scrollTop;
          return;
        }
99
        if (scrollTop > 300 && visible) {
jim's avatar
jim committed
100 101 102 103
          this.setState({
            visible: false,
          });
        }
104
        if (scrollTop < 300 && !visible) {
jim's avatar
jim committed
105 106 107 108
          this.setState({
            visible: true,
          });
        }
109
        this.oldScrollTop = scrollTop;
jim's avatar
jim committed
110 111 112
        this.ticking = false;
      });
    }
113
    this.ticking = false;
jim's avatar
jim committed
114
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
115

jim's avatar
jim committed
116
  render() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
117
    const { isMobile, handleMenuCollapse, setting } = this.props;
afc163's avatar
afc163 committed
118
    const { navTheme, layout, fixedHeader } = setting;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
119
    const { visible } = this.state;
jim's avatar
jim committed
120
    const isTop = layout === 'topmenu';
121
    const width = this.getHeadWidth();
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
122
    const HeaderDom = visible ? (
123
      <Header style={{ padding: 0, width }} className={fixedHeader ? styles.fixedHeader : ''}>
jim's avatar
jim committed
124 125
        {isTop && !isMobile ? (
          <TopNavHeader
afc163's avatar
afc163 committed
126
            theme={navTheme}
jim's avatar
jim committed
127 128 129
            mode="horizontal"
            Authorized={Authorized}
            onCollapse={handleMenuCollapse}
jim's avatar
jim committed
130
            onNoticeClear={this.handleNoticeClear}
jim's avatar
jim committed
131 132 133 134 135 136 137
            onMenuClick={this.handleMenuClick}
            onNoticeVisibleChange={this.handleNoticeVisibleChange}
            {...this.props}
          />
        ) : (
          <GlobalHeader
            onCollapse={handleMenuCollapse}
jim's avatar
jim committed
138
            onNoticeClear={this.handleNoticeClear}
jim's avatar
jim committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
            onMenuClick={this.handleMenuClick}
            onNoticeVisibleChange={this.handleNoticeVisibleChange}
            {...this.props}
          />
        )}
      </Header>
    ) : null;
    return (
      <Animate component="" transitionName="fade">
        {HeaderDom}
      </Animate>
    );
  }
}

export default connect(({ user, global, setting, loading }) => ({
  currentUser: user.currentUser,
  collapsed: global.collapsed,
  fetchingNotices: loading.effects['global/fetchNotices'],
  notices: global.notices,
jim's avatar
jim committed
159
  setting,
jim's avatar
jim committed
160
}))(HeaderView);