Header.js 4.01 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

jim's avatar
jim committed
18
  componentDidMount() {
19
    document.addEventListener('scroll', this.handScroll, { passive: true });
jim's avatar
jim committed
20
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
21

jim's avatar
jim committed
22
  componentWillUnmount() {
23
    document.removeEventListener('scroll', this.handScroll);
jim's avatar
jim committed
24
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
25

jim's avatar
jim committed
26
  getHeadWidth = () => {
27 28 29
    const { isMobile, collapsed, setting } = this.props;
    const { fixedHeader, layout } = setting;
    if (isMobile || !fixedHeader || layout === 'topmenu') {
jim's avatar
jim committed
30 31
      return '100%';
    }
32
    return collapsed ? 'calc(100% - 80px)' : 'calc(100% - 256px)';
jim's avatar
jim committed
33
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
34

jim's avatar
jim committed
35
  handleNoticeClear = type => {
jim's avatar
jim committed
36
    message.success(`ζΈ…η©ΊδΊ†${type}`);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37 38
    const { dispatch } = this.props;
    dispatch({
jim's avatar
jim committed
39 40 41 42
      type: 'global/clearNotices',
      payload: type,
    });
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
43

jim's avatar
jim committed
44
  handleMenuClick = ({ key }) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
45
    const { dispatch } = this.props;
jim's avatar
jim committed
46
    if (key === 'userCenter') {
zinkey's avatar
zinkey committed
47
      router.push('/account/center');
jim's avatar
jim committed
48 49 50
      return;
    }
    if (key === 'triggerError') {
zinkey's avatar
zinkey committed
51
      router.push('/exception/trigger');
jim's avatar
jim committed
52 53 54
      return;
    }
    if (key === 'userinfo') {
zinkey's avatar
zinkey committed
55
      router.push('/account/settings/base');
jim's avatar
jim committed
56 57 58
      return;
    }
    if (key === 'logout') {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
59
      dispatch({
jim's avatar
jim committed
60 61 62 63
        type: 'login/logout',
      });
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
64

jim's avatar
jim committed
65
  handleNoticeVisibleChange = visible => {
jim's avatar
jim committed
66
    if (visible) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
67 68
      const { dispatch } = this.props;
      dispatch({
jim's avatar
jim committed
69 70 71 72
        type: 'global/fetchNotices',
      });
    }
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
73

74
  handScroll = e => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
75 76 77
    const { autoHideHeader } = this.props;
    const { visible } = this.state;
    if (!autoHideHeader) {
jim's avatar
jim committed
78 79
      return;
    }
80
    const scrollTop = document.body.scrollTop + document.documentElement.scrollTop;
jim's avatar
jim committed
81 82
    if (!this.ticking) {
      requestAnimationFrame(() => {
83 84 85 86 87 88 89
        if (this.oldScrollTop > scrollTop) {
          this.setState({
            visible: true,
          });
          this.scrollTop = scrollTop;
          return;
        }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
90
        if (scrollTop > 400 && visible) {
jim's avatar
jim committed
91 92 93 94
          this.setState({
            visible: false,
          });
        }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
95
        if (scrollTop < 400 && !visible) {
jim's avatar
jim committed
96 97 98 99
          this.setState({
            visible: true,
          });
        }
100
        this.oldScrollTop = scrollTop;
jim's avatar
jim committed
101
        this.ticking = false;
102
        return;
jim's avatar
jim committed
103 104
      });
    }
105
    this.ticking = false;
jim's avatar
jim committed
106
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
107

jim's avatar
jim committed
108
  render() {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
109 110 111
    const { isMobile, handleMenuCollapse, setting } = this.props;
    const { silderTheme, layout, fixedHeader } = setting;
    const { visible } = this.state;
jim's avatar
jim committed
112
    const isTop = layout === 'topmenu';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
113
    const HeaderDom = visible ? (
jim's avatar
jim committed
114 115 116 117 118 119 120 121 122 123
      <Header
        style={{ padding: 0, width: this.getHeadWidth() }}
        className={fixedHeader ? styles.fixedHeader : ''}
      >
        {isTop && !isMobile ? (
          <TopNavHeader
            theme={silderTheme}
            mode="horizontal"
            Authorized={Authorized}
            onCollapse={handleMenuCollapse}
jim's avatar
jim committed
124
            onNoticeClear={this.handleNoticeClear}
jim's avatar
jim committed
125 126 127 128 129 130 131
            onMenuClick={this.handleMenuClick}
            onNoticeVisibleChange={this.handleNoticeVisibleChange}
            {...this.props}
          />
        ) : (
          <GlobalHeader
            onCollapse={handleMenuCollapse}
jim's avatar
jim committed
132
            onNoticeClear={this.handleNoticeClear}
jim's avatar
jim committed
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
            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
153
  setting,
jim's avatar
jim committed
154
}))(HeaderView);