LoadingPage.js 2.05 KB
Newer Older
jim's avatar
jim committed
1 2 3
import React, { PureComponent } from 'react';
import { Spin } from 'antd';
import { connect } from 'dva';
jim's avatar
jim committed
4
import { enquireScreen, unenquireScreen } from 'enquire-js';
jim's avatar
jim committed
5 6 7 8 9 10 11 12 13
import BasicLayout from './BasicLayout';
import { getMenuData } from '../common/menu';
/**
 * 根据菜单取得重定向地址.
 */

const MenuData = getMenuData();
const getRedirectData = () => {
  const redirectData = [];
jim's avatar
jim committed
14
  const getRedirect = item => {
jim's avatar
jim committed
15 16 17 18 19 20
    if (item && item.children) {
      if (item.children[0] && item.children[0].path) {
        redirectData.push({
          from: `${item.path}`,
          to: `${item.children[0].path}`,
        });
jim's avatar
jim committed
21
        item.children.forEach(children => {
jim's avatar
jim committed
22 23 24 25 26 27 28 29 30 31
          getRedirect(children);
        });
      }
    }
  };
  MenuData.forEach(getRedirect);
  return redirectData;
};
const redirectData = getRedirectData();

lijiehua's avatar
lijiehua committed
32
class LoadingPage extends PureComponent {
jim's avatar
jim committed
33 34 35 36
  state = {
    loading: true,
    isMobile: false,
  };
37

jim's avatar
jim committed
38
  componentDidMount() {
39
    const { dispatch } = this.props;
jim's avatar
jim committed
40
    this.enquireHandler = enquireScreen(mobile => {
jim's avatar
jim committed
41 42 43 44
      this.setState({
        isMobile: mobile,
      });
    });
45
    dispatch({
jim's avatar
jim committed
46 47 48
      type: 'user/fetchCurrent',
    });
    this.hideLoading();
jim's avatar
jim committed
49
    this.initSetting();
jim's avatar
jim committed
50
  }
51

jim's avatar
jim committed
52 53 54
  componentWillUnmount() {
    unenquireScreen(this.enquireHandler);
  }
55

jim's avatar
jim committed
56 57 58 59 60
  hideLoading() {
    this.setState({
      loading: false,
    });
  }
61

jim's avatar
jim committed
62 63 64
  /**
   * get setting from url params
   */
jim's avatar
jim committed
65
  initSetting() {
66 67
    const { dispatch } = this.props;
    dispatch({
jim's avatar
jim committed
68
      type: 'setting/getSetting',
jim's avatar
jim committed
69 70
    });
  }
71

jim's avatar
jim committed
72
  render() {
73 74
    const { loading, isMobile } = this.state;
    if (loading) {
jim's avatar
jim committed
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
      return (
        <div
          style={{
            width: '100%',
            height: '100%',
            margin: 'auto',
            paddingTop: 50,
            textAlign: 'center',
          }}
        >
          <Spin size="large" />
        </div>
      );
    }
    return (
      <BasicLayout
91
        isMobile={isMobile}
jim's avatar
jim committed
92 93 94 95 96 97 98 99
        menuData={MenuData}
        redirectData={redirectData}
        {...this.props}
      />
    );
  }
}

lijiehua's avatar
lijiehua committed
100
export default connect()(LoadingPage);