Header.js 4.07 KB
Newer Older
jim's avatar
jim committed
1 2 3 4 5
import React, { PureComponent } from 'react';
import { Layout, message } from 'antd';
import Animate from 'rc-animate';
import { connect } from 'dva';
import { routerRedux } from 'dva/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 = () => {
柴茂源's avatar
柴茂源 committed
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%';
    }
柴茂源's avatar
柴茂源 committed
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') {
陈帅's avatar
陈帅 committed
47
      dispatch(routerRedux.push('/account/center'));
jim's avatar
jim committed
48 49 50
      return;
    }
    if (key === 'triggerError') {
陈帅's avatar
陈帅 committed
51
      dispatch(routerRedux.push('/exception/trigger'));
jim's avatar
jim committed
52 53 54
      return;
    }
    if (key === 'userinfo') {
陈帅's avatar
陈帅 committed
55
      dispatch(routerRedux.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);