router.js 1.7 KB
Newer Older
1
import React from 'react';
2
import { routerRedux, Route, Switch } from 'dva/router';
afc163's avatar
afc163 committed
3
import { LocaleProvider, Spin } from 'antd';
WhatAKitty's avatar
WhatAKitty committed
4
import dynamic from 'dva/dynamic';
ddcat1115's avatar
ddcat1115 committed
5
import { addLocaleData, IntlProvider } from 'react-intl';
ddcat1115's avatar
ddcat1115 committed
6
import { getRouterData } from './common/router';
ddcat1115's avatar
ddcat1115 committed
7
import Authorized from './utils/Authorized';
8
import { getQueryPath } from './utils/utils';
afc163's avatar
afc163 committed
9
import styles from './index.less';
ddcat1115's avatar
ddcat1115 committed
10 11
import enLocale from './locale/en-US';
import cnLocale from './locale/zh-CN';
afc163's avatar
afc163 committed
12

13
const { ConnectedRouter } = routerRedux;
ddcat1115's avatar
ddcat1115 committed
14
const { AuthorizedRoute } = Authorized;
陈帅's avatar
陈帅 committed
15

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

ddcat1115's avatar
ddcat1115 committed
20 21 22 23 24 25 26
function getLang() {
  return (window.localStorage && localStorage.getItem('locale')) ||
    (navigator.language || navigator.browserLanguage).toLowerCase() === 'en-us'
    ? 'en-US'
    : 'zh-CN';
}

WhatAKitty's avatar
WhatAKitty committed
27
function RouterConfig({ history, app }) {
ddcat1115's avatar
ddcat1115 committed
28 29 30
  const routerData = getRouterData(app);
  const UserLayout = routerData['/user'].component;
  const BasicLayout = routerData['/'].component;
ddcat1115's avatar
ddcat1115 committed
31 32
  const appLocale = getLang() === 'zh-CN' ? cnLocale : enLocale;
  addLocaleData(appLocale.data);
33
  return (
ddcat1115's avatar
ddcat1115 committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
    <IntlProvider locale={appLocale.locale} messages={appLocale.messages}>
      <LocaleProvider locale={appLocale.antd}>
        <ConnectedRouter history={history}>
          <Switch>
            <Route path="/user" component={UserLayout} />
            <AuthorizedRoute
              path="/"
              render={props => <BasicLayout {...props} />}
              authority={['admin', 'user']}
              redirectPath="/user/login"
            />
          </Switch>
        </ConnectedRouter>
      </LocaleProvider>
    </IntlProvider>
49 50 51 52
  );
}

export default RouterConfig;