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 = () => {
柴茂源's avatar
柴茂源 committed
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%';
    }
柴茂源's avatar
柴茂源 committed
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);