router.js 1.75 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
function getLang() {
陈帅's avatar
陈帅 committed
21 22
  if (window.localStorage && localStorage.getItem('locale')) {
    return localStorage.getItem('locale');
陈帅's avatar
陈帅 committed
23
  }
陈帅's avatar
陈帅 committed
24
  return (navigator.language || navigator.browserLanguage).toLowerCase() === 'en-us'
ddcat1115's avatar
ddcat1115 committed
25 26 27 28
    ? 'en-US'
    : 'zh-CN';
}

WhatAKitty's avatar
WhatAKitty committed
29
function RouterConfig({ history, app }) {
ddcat1115's avatar
ddcat1115 committed
30 31 32
  const routerData = getRouterData(app);
  const UserLayout = routerData['/user'].component;
  const BasicLayout = routerData['/'].component;
ddcat1115's avatar
ddcat1115 committed
33 34
  const appLocale = getLang() === 'zh-CN' ? cnLocale : enLocale;
  addLocaleData(appLocale.data);
35
  return (
ddcat1115's avatar
ddcat1115 committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
    <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>
51 52 53 54
  );
}

export default RouterConfig;