Header.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import React, { PureComponent } from 'react';
  2. import { Layout, message } from 'antd';
  3. import Animate from 'rc-animate';
  4. import { connect } from 'dva';
  5. import router from 'umi/router';
  6. import GlobalHeader from '@/components/GlobalHeader';
  7. import TopNavHeader from '@/components/TopNavHeader';
  8. import styles from './Header.less';
  9. import Authorized from '@/utils/Authorized';
  10. const { Header } = Layout;
  11. class HeaderView extends PureComponent {
  12. state = {
  13. visible: true,
  14. };
  15. componentDidMount() {
  16. document.addEventListener('scroll', this.handScroll, { passive: true });
  17. }
  18. componentWillUnmount() {
  19. document.removeEventListener('scroll', this.handScroll);
  20. }
  21. getHeadWidth = () => {
  22. const { isMobile, collapsed, setting } = this.props;
  23. const { fixedHeader, layout } = setting;
  24. if (isMobile || !fixedHeader || layout === 'topmenu') {
  25. return '100%';
  26. }
  27. return collapsed ? 'calc(100% - 80px)' : 'calc(100% - 256px)';
  28. };
  29. handleNoticeClear = type => {
  30. message.success(`清空了${type}`);
  31. const { dispatch } = this.props;
  32. dispatch({
  33. type: 'global/clearNotices',
  34. payload: type,
  35. });
  36. };
  37. handleMenuClick = ({ key }) => {
  38. const { dispatch } = this.props;
  39. if (key === 'userCenter') {
  40. router.push('/account/center');
  41. return;
  42. }
  43. if (key === 'triggerError') {
  44. router.push('/exception/trigger');
  45. return;
  46. }
  47. if (key === 'userinfo') {
  48. router.push('/account/settings/base');
  49. return;
  50. }
  51. if (key === 'logout') {
  52. dispatch({
  53. type: 'login/logout',
  54. });
  55. }
  56. };
  57. handleNoticeVisibleChange = visible => {
  58. if (visible) {
  59. const { dispatch } = this.props;
  60. dispatch({
  61. type: 'global/fetchNotices',
  62. });
  63. }
  64. };
  65. handScroll = () => {
  66. const { autoHideHeader } = this.props;
  67. const { visible } = this.state;
  68. if (!autoHideHeader) {
  69. return;
  70. }
  71. const scrollTop = document.body.scrollTop + document.documentElement.scrollTop;
  72. if (!this.ticking) {
  73. requestAnimationFrame(() => {
  74. if (this.oldScrollTop > scrollTop) {
  75. this.setState({
  76. visible: true,
  77. });
  78. this.scrollTop = scrollTop;
  79. return;
  80. }
  81. if (scrollTop > 400 && visible) {
  82. this.setState({
  83. visible: false,
  84. });
  85. }
  86. if (scrollTop < 400 && !visible) {
  87. this.setState({
  88. visible: true,
  89. });
  90. }
  91. this.oldScrollTop = scrollTop;
  92. this.ticking = false;
  93. });
  94. }
  95. this.ticking = false;
  96. };
  97. render() {
  98. const { isMobile, handleMenuCollapse, setting } = this.props;
  99. const { silderTheme, layout, fixedHeader } = setting;
  100. const { visible } = this.state;
  101. const isTop = layout === 'topmenu';
  102. const HeaderDom = visible ? (
  103. <Header
  104. style={{ padding: 0, width: this.getHeadWidth() }}
  105. className={fixedHeader ? styles.fixedHeader : ''}
  106. >
  107. {isTop && !isMobile ? (
  108. <TopNavHeader
  109. theme={silderTheme}
  110. mode="horizontal"
  111. Authorized={Authorized}
  112. onCollapse={handleMenuCollapse}
  113. onNoticeClear={this.handleNoticeClear}
  114. onMenuClick={this.handleMenuClick}
  115. onNoticeVisibleChange={this.handleNoticeVisibleChange}
  116. {...this.props}
  117. />
  118. ) : (
  119. <GlobalHeader
  120. onCollapse={handleMenuCollapse}
  121. onNoticeClear={this.handleNoticeClear}
  122. onMenuClick={this.handleMenuClick}
  123. onNoticeVisibleChange={this.handleNoticeVisibleChange}
  124. {...this.props}
  125. />
  126. )}
  127. </Header>
  128. ) : null;
  129. return (
  130. <Animate component="" transitionName="fade">
  131. {HeaderDom}
  132. </Animate>
  133. );
  134. }
  135. }
  136. export default connect(({ user, global, setting, loading }) => ({
  137. currentUser: user.currentUser,
  138. collapsed: global.collapsed,
  139. fetchingNotices: loading.effects['global/fetchNotices'],
  140. notices: global.notices,
  141. setting,
  142. }))(HeaderView);