LoadingPage.js 2.98 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';
6 7 8 9
// TODO: should use this.props.routes
import config from '../../../config/config';
const menuData = config['routes'];

10
// Conversion router to menu.
11 12
function formatter(data, parentPath = '', parentAuthority, parentName) {
  return data.map(item => {
陈帅's avatar
陈帅 committed
13 14 15 16 17 18 19 20
    let locale = 'menu';
    if (parentName && item.name) {
      locale = `${parentName}.${item.name}`;
    } else if (item.name) {
      locale = `menu.${item.name}`;
    } else if (parentName) {
      locale = parentName;
    }
21 22
    const result = {
      ...item,
陈帅's avatar
陈帅 committed
23
      locale,
24 25 26
      authority: item.authority || parentAuthority,
    };
    if (item.routes) {
陈帅's avatar
陈帅 committed
27
      const children = formatter(item.routes, `${parentPath}${item.path}/`, item.authority, locale);
28 29
      // Reduce memory usage
      result.children = children;
30
    }
31
    delete result.routes;
32 33 34
    return result;
  });
}
jim's avatar
jim committed
35 36 37
/**
 * 根据菜单取得重定向地址.
 */
38 39
const MenuData = formatter(menuData[1].routes);
const routerData = config.routes;
jim's avatar
jim committed
40 41
const getRedirectData = () => {
  const redirectData = [];
jim's avatar
jim committed
42
  const getRedirect = item => {
jim's avatar
jim committed
43 44 45 46 47 48
    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
49
        item.children.forEach(children => {
jim's avatar
jim committed
50 51 52 53 54 55 56 57 58 59
          getRedirect(children);
        });
      }
    }
  };
  MenuData.forEach(getRedirect);
  return redirectData;
};
const redirectData = getRedirectData();

lijiehua's avatar
lijiehua committed
60
class LoadingPage extends PureComponent {
jim's avatar
jim committed
61 62 63 64
  state = {
    loading: true,
    isMobile: false,
  };
65

jim's avatar
jim committed
66
  componentDidMount() {
67
    const { dispatch } = this.props;
jim's avatar
jim committed
68
    this.enquireHandler = enquireScreen(mobile => {
陈帅's avatar
陈帅 committed
69 70 71 72 73 74
      const { isMobile } = this.state;
      if (isMobile !== mobile) {
        this.setState({
          isMobile: mobile,
        });
      }
jim's avatar
jim committed
75
    });
76
    dispatch({
jim's avatar
jim committed
77 78 79
      type: 'user/fetchCurrent',
    });
    this.hideLoading();
jim's avatar
jim committed
80
    this.initSetting();
jim's avatar
jim committed
81
  }
82

jim's avatar
jim committed
83 84 85
  componentWillUnmount() {
    unenquireScreen(this.enquireHandler);
  }
86

jim's avatar
jim committed
87 88 89 90 91
  hideLoading() {
    this.setState({
      loading: false,
    });
  }
92

jim's avatar
jim committed
93 94 95
  /**
   * get setting from url params
   */
jim's avatar
jim committed
96
  initSetting() {
97 98
    const { dispatch } = this.props;
    dispatch({
jim's avatar
jim committed
99
      type: 'setting/getSetting',
jim's avatar
jim committed
100 101
    });
  }
102

jim's avatar
jim committed
103
  render() {
104 105
    const { loading, isMobile } = this.state;
    if (loading) {
jim's avatar
jim committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
      return (
        <div
          style={{
            width: '100%',
            height: '100%',
            margin: 'auto',
            paddingTop: 50,
            textAlign: 'center',
          }}
        >
          <Spin size="large" />
        </div>
      );
    }
    return (
      <BasicLayout
122
        isMobile={isMobile}
jim's avatar
jim committed
123
        menuData={MenuData}
愚道's avatar
愚道 committed
124
        routerData={routerData}
jim's avatar
jim committed
125 126 127 128 129 130 131
        redirectData={redirectData}
        {...this.props}
      />
    );
  }
}

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