Commit a22d4003 authored by xiaohuoni's avatar xiaohuoni

use umi conventional routing

parent 6e97ea92
// https://umijs.org/config/ // https://umijs.org/config/
import os from 'os'; import os from 'os';
import pageRoutes from './router.config';
import webpackPlugin from './plugin.config'; import webpackPlugin from './plugin.config';
import defaultSettings from '../src/defaultSettings'; import defaultSettings from '../src/defaultSettings';
import slash from 'slash2'; import slash from 'slash2';
...@@ -21,6 +20,11 @@ const plugins = [ ...@@ -21,6 +20,11 @@ const plugins = [
default: 'zh-CN', // default zh-CN default: 'zh-CN', // default zh-CN
baseNavigator: true, // default true, when it is true, will use `navigator.language` overwrite default baseNavigator: true, // default true, when it is true, will use `navigator.language` overwrite default
}, },
routes: {
update(routes) {
return [...require('../src/pages/_routes'), ...routes];
},
},
dynamicImport: { dynamicImport: {
loadingComponent: './components/PageLoading/index', loadingComponent: './components/PageLoading/index',
webpackChunkName: true, webpackChunkName: true,
...@@ -45,6 +49,18 @@ const plugins = [ ...@@ -45,6 +49,18 @@ const plugins = [
: {}), : {}),
}, },
], ],
[
'umi-plugin-authorize',
{
authorize: [
{
guard: ['src/pages/Auth'],
include: /\//,
exclude: /\/user/i,
},
],
},
],
]; ];
// 针对 preview.pro.ant.design 的 GA 统计代码 // 针对 preview.pro.ant.design 的 GA 统计代码
...@@ -69,7 +85,7 @@ export default { ...@@ -69,7 +85,7 @@ export default {
ie: 11, ie: 11,
}, },
// 路由配置 // 路由配置
routes: pageRoutes, // routes: pageRoutes,
// Theme for antd // Theme for antd
// https://ant.design/docs/react/customize-theme-cn // https://ant.design/docs/react/customize-theme-cn
theme: { theme: {
......
...@@ -2,4 +2,196 @@ export default { ...@@ -2,4 +2,196 @@ export default {
'/api/auth_routes': { '/api/auth_routes': {
'/form/advanced-form': { authority: ['admin', 'user'] }, '/form/advanced-form': { authority: ['admin', 'user'] },
}, },
'/api/menus': {
routes: [
// dashboard
{
path: '/dashboard',
name: 'dashboard',
icon: 'dashboard',
routes: [
{
path: '/dashboard/analysis',
name: 'analysis',
},
{
path: '/dashboard/monitor',
name: 'monitor',
},
{
path: '/dashboard/workplace',
name: 'workplace',
},
],
},
// forms
{
path: '/forms',
icon: 'form',
name: 'form',
routes: [
{
path: '/forms/basicform',
name: 'basicform',
},
{
path: '/forms/stepform',
name: 'stepform',
hideChildrenInMenu: true,
routes: [
{
path: '/forms/stepform/info',
name: 'info',
},
{
path: '/forms/stepform/confirm',
name: 'confirm',
},
{
path: '/forms/stepform/result',
name: 'result',
},
],
},
{
path: '/forms/advancedform',
name: 'advancedform',
authority: ['admin'],
},
],
},
// list
{
path: '/list',
icon: 'table',
name: 'list',
routes: [
{
path: '/list/tablelist',
name: 'searchtable',
},
{
path: '/list/basiclist',
name: 'basiclist',
},
{
path: '/list/cardlist',
name: 'cardlist',
},
{
path: '/list/search',
name: 'searchlist',
routes: [
{
path: '/list/search/articles',
name: 'articles',
},
{
path: '/list/search/projects',
name: 'projects',
},
{
path: '/list/search/applications',
name: 'applications',
},
],
},
],
},
{
path: '/profile',
name: 'profile',
icon: 'profile',
routes: [
// profile
{
path: '/profile/basicprofile',
name: 'basic',
},
{
path: '/profile/advancedprofile',
name: 'advanced',
},
],
},
{
name: 'result',
icon: 'check-circle-o',
path: '/result',
routes: [
// result
{
path: '/result/success',
name: 'success',
},
{ path: '/result/fail', name: 'fail' },
],
},
{
name: 'exception',
icon: 'warning',
path: '/exception',
routes: [
// exception
{
path: '/exception/403',
name: 'not-permission',
},
{
path: '/exception/404',
name: 'not-find',
},
{
path: '/exception/500',
name: 'server-error',
},
{
path: '/exception/trigger',
name: 'trigger',
hideInMenu: true,
},
],
},
{
name: 'account',
icon: 'user',
path: '/account',
routes: [
{
path: '/account/center',
name: 'center',
routes: [
{
path: '/account/center/articles',
},
{
path: '/account/center/applications',
},
{
path: '/account/center/projects',
},
],
},
{
path: '/account/settings',
name: 'settings',
routes: [
{
path: '/account/settings/baseview',
},
{
path: '/account/settings/security',
},
{
path: '/account/settings/binding',
},
{
path: '/account/settings/notification',
},
],
},
],
},
],
},
}; };
...@@ -58,7 +58,8 @@ ...@@ -58,7 +58,8 @@
"react-dom": "^16.7.0", "react-dom": "^16.7.0",
"react-fittext": "^1.0.0", "react-fittext": "^1.0.0",
"react-media": "^1.9.2", "react-media": "^1.9.2",
"react-router-dom": "^4.3.1" "react-router-dom": "^4.3.1",
"umi-plugin-authorize": "^1.0.0"
}, },
"devDependencies": { "devDependencies": {
"@types/react": "^16.8.1", "@types/react": "^16.8.1",
......
import React from 'react';
import BasicLayout from './BasicLayout';
import UserLayout from './UserLayout';
export default props => {
const { location } = props;
const { pathname } = location;
if (/\/user/i.test(pathname)) {
return <UserLayout {...props} />;
}
return <BasicLayout {...props} />;
};
...@@ -3,6 +3,7 @@ import isEqual from 'lodash/isEqual'; ...@@ -3,6 +3,7 @@ import isEqual from 'lodash/isEqual';
import { formatMessage } from 'umi/locale'; import { formatMessage } from 'umi/locale';
import Authorized from '@/utils/Authorized'; import Authorized from '@/utils/Authorized';
import { menu } from '../defaultSettings'; import { menu } from '../defaultSettings';
import { queryMenu } from '@/services/user';
const { check } = Authorized; const { check } = Authorized;
...@@ -97,17 +98,18 @@ export default { ...@@ -97,17 +98,18 @@ export default {
state: { state: {
menuData: [], menuData: [],
routerData: [],
breadcrumbNameMap: {}, breadcrumbNameMap: {},
}, },
effects: { effects: {
*getMenuData({ payload }, { put }) { *getMenuData(_, { put, call }) {
const { routes, authority } = payload; const { routes, authority } = yield call(queryMenu);
const menuData = filterMenuData(memoizeOneFormatter(routes, authority)); const menuData = filterMenuData(memoizeOneFormatter(routes, authority));
const breadcrumbNameMap = memoizeOneGetBreadcrumbNameMap(menuData); const breadcrumbNameMap = memoizeOneGetBreadcrumbNameMap(menuData);
yield put({ yield put({
type: 'save', type: 'save',
payload: { menuData, breadcrumbNameMap }, payload: { menuData, breadcrumbNameMap, routerData: routes },
}); });
}, },
}, },
......
...@@ -3,7 +3,7 @@ import { List, Card, Icon, Dropdown, Menu, Avatar, Tooltip } from 'antd'; ...@@ -3,7 +3,7 @@ import { List, Card, Icon, Dropdown, Menu, Avatar, Tooltip } from 'antd';
import numeral from 'numeral'; import numeral from 'numeral';
import { connect } from 'dva'; import { connect } from 'dva';
import { formatWan } from '@/utils/utils'; import { formatWan } from '@/utils/utils';
import stylesApplications from '../../List/Applications.less'; import stylesApplications from '../../List/Search/Applications.less';
@connect(({ list }) => ({ @connect(({ list }) => ({
list, list,
......
...@@ -3,7 +3,7 @@ import { List, Card } from 'antd'; ...@@ -3,7 +3,7 @@ import { List, Card } from 'antd';
import moment from 'moment'; import moment from 'moment';
import { connect } from 'dva'; import { connect } from 'dva';
import AvatarList from '@/components/AvatarList'; import AvatarList from '@/components/AvatarList';
import stylesProjects from '../../List/Projects.less'; import stylesProjects from '../../List/Search/Projects.less';
@connect(({ list }) => ({ @connect(({ list }) => ({
list, list,
......
...@@ -16,14 +16,16 @@ class Info extends Component { ...@@ -16,14 +16,16 @@ class Info extends Component {
super(props); super(props);
const { match, location } = props; const { match, location } = props;
const menuMap = { const menuMap = {
base: <FormattedMessage id="app.settings.menuMap.basic" defaultMessage="Basic Settings" />, baseview: (
security: ( <FormattedMessage id="app.settings.menuMap.basic" defaultMessage="Basic Settings" />
),
securityview: (
<FormattedMessage id="app.settings.menuMap.security" defaultMessage="Security Settings" /> <FormattedMessage id="app.settings.menuMap.security" defaultMessage="Security Settings" />
), ),
binding: ( bindingview: (
<FormattedMessage id="app.settings.menuMap.binding" defaultMessage="Account Binding" /> <FormattedMessage id="app.settings.menuMap.binding" defaultMessage="Account Binding" />
), ),
notification: ( notificationview: (
<FormattedMessage <FormattedMessage
id="app.settings.menuMap.notification" id="app.settings.menuMap.notification"
defaultMessage="New Message Notification" defaultMessage="New Message Notification"
......
import React from 'react';
import pathToRegexp from 'path-to-regexp';
import Authorized from '@/utils/Authorized';
import Exception403 from './Exception/403';
export default ({ children, location }) => {
/* eslint-disable no-underscore-dangle */
const { routerData } = window.g_app._store.getState().menu;
const getRouteAuthority = (pathname, routeData) => {
const routes = routeData.slice(); // clone
const getAuthority = (routeDatas, path) => {
let authorities;
routeDatas.forEach(route => {
// check partial route
if (pathToRegexp(`${route.path}(.*)`).test(path)) {
if (route.authority) {
authorities = route.authority;
}
// is exact route?
if (!pathToRegexp(route.path).test(path) && route.routes) {
authorities = getAuthority(route.routes, path);
}
}
});
return authorities;
};
return getAuthority(routes, pathname);
};
return (
<Authorized
authority={getRouteAuthority(location.pathname, routerData)}
noMatch={<Exception403 />}
>
{children}
</Authorized>
);
};
...@@ -24,7 +24,7 @@ class Step2 extends React.PureComponent { ...@@ -24,7 +24,7 @@ class Step2 extends React.PureComponent {
const { form, data, dispatch, submitting } = this.props; const { form, data, dispatch, submitting } = this.props;
const { getFieldDecorator, validateFields } = form; const { getFieldDecorator, validateFields } = form;
const onPrev = () => { const onPrev = () => {
router.push('/form/step-form/info'); router.push('/forms/stepform/info');
}; };
const onValidateForm = e => { const onValidateForm = e => {
e.preventDefault(); e.preventDefault();
......
...@@ -30,7 +30,7 @@ class Step1 extends React.PureComponent { ...@@ -30,7 +30,7 @@ class Step1 extends React.PureComponent {
type: 'form/saveStepFormData', type: 'form/saveStepFormData',
payload: values, payload: values,
}); });
router.push('/form/step-form/confirm'); router.push('/forms/stepform/confirm');
} }
}); });
}; };
......
...@@ -12,7 +12,7 @@ class Step3 extends React.PureComponent { ...@@ -12,7 +12,7 @@ class Step3 extends React.PureComponent {
render() { render() {
const { data } = this.props; const { data } = this.props;
const onFinish = () => { const onFinish = () => {
router.push('/form/step-form/info'); router.push('/forms/stepform/info');
}; };
const information = ( const information = (
<div className={styles.information}> <div className={styles.information}>
......
...@@ -25,7 +25,7 @@ export default { ...@@ -25,7 +25,7 @@ export default {
type: 'saveStepFormData', type: 'saveStepFormData',
payload, payload,
}); });
yield put(routerRedux.push('/form/step-form/result')); yield put(routerRedux.push('/forms/stepform/result'));
}, },
*submitAdvancedForm({ payload }, { call }) { *submitAdvancedForm({ payload }, { call }) {
yield call(fakeSubmitForm, payload); yield call(fakeSubmitForm, payload);
......
[
{
"path": "/",
"exact": true,
"redirect": "/dashboard/analysis"
},
{
"path": "/dashboard",
"exact": true,
"redirect": "/dashboard/analysis"
},
{
"path": "/forms",
"exact": true,
"redirect": "/forms/basicform"
},
{
"path": "/forms/stepform",
"exact": true,
"redirect": "/forms/stepform/info"
},
{
"path": "/list",
"exact": true,
"redirect": "/list/tableList"
},
{
"path": "/list/search",
"exact": true,
"redirect": "/list/search/articles"
},
{
"path": "/profile",
"exact": true,
"redirect": "/profile/basicprofile"
},
{
"path": "/result",
"exact": true,
"redirect": "/result/success"
},
{
"path": "/exception",
"exact": true,
"redirect": "/exception/403"
},
{
"path": "/account/center",
"exact": true,
"redirect": "/account/center/articles"
},
{
"path": "/account/settings",
"exact": true,
"redirect": "/account/settings/baseview"
}
]
...@@ -7,3 +7,7 @@ export async function query() { ...@@ -7,3 +7,7 @@ export async function query() {
export async function queryCurrent() { export async function queryCurrent() {
return request('/api/currentUser'); return request('/api/currentUser');
} }
export async function queryMenu() {
return request('/api/menus');
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment