router.js 1.84 KB
Newer Older
1
import React from 'react';
afc163's avatar
afc163 committed
2
import { Router, Route, Switch } from 'dva/router';
afc163's avatar
afc163 committed
3
import { LocaleProvider, Spin } from 'antd';
afc163's avatar
afc163 committed
4
import zhCN from 'antd/lib/locale-provider/zh_CN';
WhatAKitty's avatar
WhatAKitty committed
5
import dynamic from 'dva/dynamic';
6 7 8 9
import cloneDeep from 'lodash/cloneDeep';
import { getNavData } from './common/nav';
import { getPlainNode } from './utils/utils';

afc163's avatar
afc163 committed
10 11 12 13 14
import styles from './index.less';

dynamic.setDefaultLoadingComponent(() => {
  return <Spin size="large" className={styles.globalSpin} />;
});
WhatAKitty's avatar
WhatAKitty committed
15

16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
function getRouteData(navData, path) {
  if (!navData.some(item => item.layout === path) ||
    !(navData.filter(item => item.layout === path)[0].children)) {
    return null;
  }
  const route = cloneDeep(navData.filter(item => item.layout === path)[0]);
  const nodeList = getPlainNode(route.children);
  return nodeList;
}

function getLayout(navData, path) {
  if (!navData.some(item => item.layout === path) ||
    !(navData.filter(item => item.layout === path)[0].children)) {
    return null;
  }
  const route = navData.filter(item => item.layout === path)[0];
  return {
    component: route.component,
    layout: route.layout,
    name: route.name,
    path: route.path,
  };
}

WhatAKitty's avatar
WhatAKitty committed
40
function RouterConfig({ history, app }) {
41 42 43 44 45
  const navData = getNavData(app);
  const UserLayout = getLayout(navData, 'UserLayout').component;
  const BasicLayout = getLayout(navData, 'BasicLayout').component;

  const passProps = {
WhatAKitty's avatar
WhatAKitty committed
46
    app,
47 48 49 50 51
    navData,
    getRouteData: (path) => {
      return getRouteData(navData, path);
    },
  };
52 53

  return (
afc163's avatar
afc163 committed
54 55
    <LocaleProvider locale={zhCN}>
      <Router history={history}>
ddcat1115's avatar
ddcat1115 committed
56
        <Switch>
57 58
          <Route path="/user" render={props => <UserLayout {...props} {...passProps} />} />
          <Route path="/" render={props => <BasicLayout {...props} {...passProps} />} />
ddcat1115's avatar
ddcat1115 committed
59
        </Switch>
afc163's avatar
afc163 committed
60 61
      </Router>
    </LocaleProvider>
62 63 64 65
  );
}

export default RouterConfig;