router.js 1.78 KB
Newer Older
1
import React from 'react';
2
import { routerRedux, Route, Switch } from 'dva/router';
陈帅's avatar
陈帅 committed
3
import { LocaleProvider, Spin } from 'antd';
WhatAKitty's avatar
WhatAKitty committed
4
import dynamic from 'dva/dynamic';
陈帅's avatar
陈帅 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';
陈帅's avatar
陈帅 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;
afc163's avatar
afc163 committed
15 16 17
dynamic.setDefaultLoadingComponent(() => {
  return <Spin size="large" className={styles.globalSpin} />;
});
WhatAKitty's avatar
WhatAKitty committed
18

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

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

export default RouterConfig;