LoadingPage.js 2.17 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
import BasicLayout from './BasicLayout';
愚道's avatar
愚道 committed
6 7
import { getMenuData } from '../../common/menu';
import { getRouterData } from '../../common/router';
jim's avatar
jim committed
8 9 10 11 12
/**
 * 根据菜单取得重定向地址.
 */

const MenuData = getMenuData();
愚道's avatar
愚道 committed
13
const routerData = getRouterData({});
jim's avatar
jim committed
14 15
const getRedirectData = () => {
  const redirectData = [];
jim's avatar
jim committed
16
  const getRedirect = item => {
jim's avatar
jim committed
17 18 19 20 21 22
    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
23
        item.children.forEach(children => {
jim's avatar
jim committed
24 25 26 27 28 29 30 31 32 33
          getRedirect(children);
        });
      }
    }
  };
  MenuData.forEach(getRedirect);
  return redirectData;
};
const redirectData = getRedirectData();

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

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

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

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

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

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

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