| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- import React, { PureComponent } from 'react';
- import { Layout, message } from 'antd';
- import Animate from 'rc-animate';
- import { connect } from 'dva';
- import router from 'umi/router';
- import GlobalHeader from '@/components/GlobalHeader';
- import TopNavHeader from '@/components/TopNavHeader';
- import styles from './Header.less';
- import Authorized from '@/utils/Authorized';
- const { Header } = Layout;
- class HeaderView extends PureComponent {
- state = {
- visible: true,
- };
- componentDidMount() {
- document.addEventListener('scroll', this.handScroll, { passive: true });
- }
- componentWillUnmount() {
- document.removeEventListener('scroll', this.handScroll);
- }
- getHeadWidth = () => {
- const { isMobile, collapsed, setting } = this.props;
- const { fixedHeader, layout } = setting;
- if (isMobile || !fixedHeader || layout === 'topmenu') {
- return '100%';
- }
- return collapsed ? 'calc(100% - 80px)' : 'calc(100% - 256px)';
- };
- handleNoticeClear = type => {
- message.success(`清空了${type}`);
- const { dispatch } = this.props;
- dispatch({
- type: 'global/clearNotices',
- payload: type,
- });
- };
- handleMenuClick = ({ key }) => {
- const { dispatch } = this.props;
- if (key === 'userCenter') {
- router.push('/account/center');
- return;
- }
- if (key === 'triggerError') {
- router.push('/exception/trigger');
- return;
- }
- if (key === 'userinfo') {
- router.push('/account/settings/base');
- return;
- }
- if (key === 'logout') {
- dispatch({
- type: 'login/logout',
- });
- }
- };
- handleNoticeVisibleChange = visible => {
- if (visible) {
- const { dispatch } = this.props;
- dispatch({
- type: 'global/fetchNotices',
- });
- }
- };
- handScroll = () => {
- const { autoHideHeader } = this.props;
- const { visible } = this.state;
- if (!autoHideHeader) {
- return;
- }
- const scrollTop = document.body.scrollTop + document.documentElement.scrollTop;
- if (!this.ticking) {
- requestAnimationFrame(() => {
- if (this.oldScrollTop > scrollTop) {
- this.setState({
- visible: true,
- });
- this.scrollTop = scrollTop;
- return;
- }
- if (scrollTop > 400 && visible) {
- this.setState({
- visible: false,
- });
- }
- if (scrollTop < 400 && !visible) {
- this.setState({
- visible: true,
- });
- }
- this.oldScrollTop = scrollTop;
- this.ticking = false;
- });
- }
- this.ticking = false;
- };
- render() {
- const { isMobile, handleMenuCollapse, setting } = this.props;
- const { silderTheme, layout, fixedHeader } = setting;
- const { visible } = this.state;
- const isTop = layout === 'topmenu';
- const HeaderDom = visible ? (
- <Header
- style={{ padding: 0, width: this.getHeadWidth() }}
- className={fixedHeader ? styles.fixedHeader : ''}
- >
- {isTop && !isMobile ? (
- <TopNavHeader
- theme={silderTheme}
- mode="horizontal"
- Authorized={Authorized}
- onCollapse={handleMenuCollapse}
- onNoticeClear={this.handleNoticeClear}
- onMenuClick={this.handleMenuClick}
- onNoticeVisibleChange={this.handleNoticeVisibleChange}
- {...this.props}
- />
- ) : (
- <GlobalHeader
- onCollapse={handleMenuCollapse}
- onNoticeClear={this.handleNoticeClear}
- 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,
- setting,
- }))(HeaderView);
|