LoadingPage.js 2.43 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
// TODO: should use this.props.routes
陈帅's avatar
陈帅 committed
7
import routerConfig from '../../../config/router.config';
8

9
// Conversion router to menu.
10 11
function formatter(data, parentPath = '', parentAuthority, parentName) {
  return data.map(item => {
陈帅's avatar
陈帅 committed
12 13 14 15 16 17 18 19
    let locale = 'menu';
    if (parentName && item.name) {
      locale = `${parentName}.${item.name}`;
    } else if (item.name) {
      locale = `menu.${item.name}`;
    } else if (parentName) {
      locale = parentName;
    }
20 21
    const result = {
      ...item,
陈帅's avatar
陈帅 committed
22
      locale,
23 24 25
      authority: item.authority || parentAuthority,
    };
    if (item.routes) {
陈帅's avatar
陈帅 committed
26
      const children = formatter(item.routes, `${parentPath}${item.path}/`, item.authority, locale);
27 28
      // Reduce memory usage
      result.children = children;
29
    }
30
    delete result.routes;
31 32 33
    return result;
  });
}
jim's avatar
jim committed
34 35 36
/**
 * 根据菜单取得重定向地址.
 */
陈帅's avatar
陈帅 committed
37
const MenuData = formatter(routerConfig[1].routes);
jim's avatar
jim committed
38

lijiehua's avatar
lijiehua committed
39
class LoadingPage extends PureComponent {
jim's avatar
jim committed
40 41 42 43
  state = {
    loading: true,
    isMobile: false,
  };
44

jim's avatar
jim committed
45
  componentDidMount() {
46
    const { dispatch } = this.props;
jim's avatar
jim committed
47
    this.enquireHandler = enquireScreen(mobile => {
陈帅's avatar
陈帅 committed
48 49 50 51 52 53
      const { isMobile } = this.state;
      if (isMobile !== mobile) {
        this.setState({
          isMobile: mobile,
        });
      }
jim's avatar
jim committed
54
    });
55
    dispatch({
jim's avatar
jim committed
56 57 58
      type: 'user/fetchCurrent',
    });
    this.hideLoading();
jim's avatar
jim committed
59
    this.initSetting();
jim's avatar
jim committed
60
  }
61

jim's avatar
jim committed
62 63 64
  componentWillUnmount() {
    unenquireScreen(this.enquireHandler);
  }
65

jim's avatar
jim committed
66 67 68 69 70
  hideLoading() {
    this.setState({
      loading: false,
    });
  }
71

jim's avatar
jim committed
72 73 74
  /**
   * get setting from url params
   */
jim's avatar
jim committed
75
  initSetting() {
76 77
    const { dispatch } = this.props;
    dispatch({
jim's avatar
jim committed
78
      type: 'setting/getSetting',
jim's avatar
jim committed
79 80
    });
  }
81

jim's avatar
jim committed
82
  render() {
83 84
    const { loading, isMobile } = this.state;
    if (loading) {
jim's avatar
jim committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
      return (
        <div
          style={{
            width: '100%',
            height: '100%',
            margin: 'auto',
            paddingTop: 50,
            textAlign: 'center',
          }}
        >
          <Spin size="large" />
        </div>
      );
    }
    return (
      <BasicLayout
101
        isMobile={isMobile}
jim's avatar
jim committed
102
        menuData={MenuData}
陈帅's avatar
陈帅 committed
103 104
        routerData={routerConfig}
        redirectData={[]}
jim's avatar
jim committed
105 106 107 108 109 110
        {...this.props}
      />
    );
  }
}

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