Skip to content

Commit a22d400

Browse files
committed
use umi conventional routing
1 parent 6e97ea9 commit a22d400

File tree

24 files changed

+342
-16
lines changed

24 files changed

+342
-16
lines changed

config/config.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// https://umijs.org/config/
22
import os from 'os';
3-
import pageRoutes from './router.config';
43
import webpackPlugin from './plugin.config';
54
import defaultSettings from '../src/defaultSettings';
65
import slash from 'slash2';
@@ -21,6 +20,11 @@ const plugins = [
2120
default: 'zh-CN', // default zh-CN
2221
baseNavigator: true, // default true, when it is true, will use `navigator.language` overwrite default
2322
},
23+
routes: {
24+
update(routes) {
25+
return [...require('../src/pages/_routes'), ...routes];
26+
},
27+
},
2428
dynamicImport: {
2529
loadingComponent: './components/PageLoading/index',
2630
webpackChunkName: true,
@@ -45,6 +49,18 @@ const plugins = [
4549
: {}),
4650
},
4751
],
52+
[
53+
'umi-plugin-authorize',
54+
{
55+
authorize: [
56+
{
57+
guard: ['src/pages/Auth'],
58+
include: /\//,
59+
exclude: /\/user/i,
60+
},
61+
],
62+
},
63+
],
4864
];
4965

5066
// 针对 preview.pro.ant.design 的 GA 统计代码
@@ -69,7 +85,7 @@ export default {
6985
ie: 11,
7086
},
7187
// 路由配置
72-
routes: pageRoutes,
88+
// routes: pageRoutes,
7389
// Theme for antd
7490
// https://ant.design/docs/react/customize-theme-cn
7591
theme: {

mock/route.js

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,196 @@ export default {
22
'/api/auth_routes': {
33
'/form/advanced-form': { authority: ['admin', 'user'] },
44
},
5+
'/api/menus': {
6+
routes: [
7+
// dashboard
8+
{
9+
path: '/dashboard',
10+
name: 'dashboard',
11+
icon: 'dashboard',
12+
routes: [
13+
{
14+
path: '/dashboard/analysis',
15+
name: 'analysis',
16+
},
17+
{
18+
path: '/dashboard/monitor',
19+
name: 'monitor',
20+
},
21+
{
22+
path: '/dashboard/workplace',
23+
name: 'workplace',
24+
},
25+
],
26+
},
27+
// forms
28+
{
29+
path: '/forms',
30+
icon: 'form',
31+
name: 'form',
32+
routes: [
33+
{
34+
path: '/forms/basicform',
35+
name: 'basicform',
36+
},
37+
{
38+
path: '/forms/stepform',
39+
name: 'stepform',
40+
hideChildrenInMenu: true,
41+
routes: [
42+
{
43+
path: '/forms/stepform/info',
44+
name: 'info',
45+
},
46+
{
47+
path: '/forms/stepform/confirm',
48+
name: 'confirm',
49+
},
50+
{
51+
path: '/forms/stepform/result',
52+
name: 'result',
53+
},
54+
],
55+
},
56+
{
57+
path: '/forms/advancedform',
58+
name: 'advancedform',
59+
authority: ['admin'],
60+
},
61+
],
62+
},
63+
// list
64+
{
65+
path: '/list',
66+
icon: 'table',
67+
name: 'list',
68+
routes: [
69+
{
70+
path: '/list/tablelist',
71+
name: 'searchtable',
72+
},
73+
{
74+
path: '/list/basiclist',
75+
name: 'basiclist',
76+
},
77+
{
78+
path: '/list/cardlist',
79+
name: 'cardlist',
80+
},
81+
{
82+
path: '/list/search',
83+
name: 'searchlist',
84+
routes: [
85+
{
86+
path: '/list/search/articles',
87+
name: 'articles',
88+
},
89+
{
90+
path: '/list/search/projects',
91+
name: 'projects',
92+
},
93+
{
94+
path: '/list/search/applications',
95+
name: 'applications',
96+
},
97+
],
98+
},
99+
],
100+
},
101+
{
102+
path: '/profile',
103+
name: 'profile',
104+
icon: 'profile',
105+
routes: [
106+
// profile
107+
{
108+
path: '/profile/basicprofile',
109+
name: 'basic',
110+
},
111+
{
112+
path: '/profile/advancedprofile',
113+
name: 'advanced',
114+
},
115+
],
116+
},
117+
{
118+
name: 'result',
119+
icon: 'check-circle-o',
120+
path: '/result',
121+
routes: [
122+
// result
123+
{
124+
path: '/result/success',
125+
name: 'success',
126+
},
127+
{ path: '/result/fail', name: 'fail' },
128+
],
129+
},
130+
{
131+
name: 'exception',
132+
icon: 'warning',
133+
path: '/exception',
134+
routes: [
135+
// exception
136+
{
137+
path: '/exception/403',
138+
name: 'not-permission',
139+
},
140+
{
141+
path: '/exception/404',
142+
name: 'not-find',
143+
},
144+
{
145+
path: '/exception/500',
146+
name: 'server-error',
147+
},
148+
{
149+
path: '/exception/trigger',
150+
name: 'trigger',
151+
hideInMenu: true,
152+
},
153+
],
154+
},
155+
{
156+
name: 'account',
157+
icon: 'user',
158+
path: '/account',
159+
routes: [
160+
{
161+
path: '/account/center',
162+
name: 'center',
163+
routes: [
164+
{
165+
path: '/account/center/articles',
166+
},
167+
{
168+
path: '/account/center/applications',
169+
},
170+
{
171+
path: '/account/center/projects',
172+
},
173+
],
174+
},
175+
{
176+
path: '/account/settings',
177+
name: 'settings',
178+
routes: [
179+
{
180+
path: '/account/settings/baseview',
181+
},
182+
{
183+
path: '/account/settings/security',
184+
},
185+
{
186+
path: '/account/settings/binding',
187+
},
188+
{
189+
path: '/account/settings/notification',
190+
},
191+
],
192+
},
193+
],
194+
},
195+
],
196+
},
5197
};

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@
5858
"react-dom": "^16.7.0",
5959
"react-fittext": "^1.0.0",
6060
"react-media": "^1.9.2",
61-
"react-router-dom": "^4.3.1"
61+
"react-router-dom": "^4.3.1",
62+
"umi-plugin-authorize": "^1.0.0"
6263
},
6364
"devDependencies": {
6465
"@types/react": "^16.8.1",

src/layouts/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import React from 'react';
2+
import BasicLayout from './BasicLayout';
3+
import UserLayout from './UserLayout';
4+
5+
export default props => {
6+
const { location } = props;
7+
const { pathname } = location;
8+
if (/\/user/i.test(pathname)) {
9+
return <UserLayout {...props} />;
10+
}
11+
return <BasicLayout {...props} />;
12+
};

src/models/menu.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import isEqual from 'lodash/isEqual';
33
import { formatMessage } from 'umi/locale';
44
import Authorized from '@/utils/Authorized';
55
import { menu } from '../defaultSettings';
6+
import { queryMenu } from '@/services/user';
67

78
const { check } = Authorized;
89

@@ -97,17 +98,18 @@ export default {
9798

9899
state: {
99100
menuData: [],
101+
routerData: [],
100102
breadcrumbNameMap: {},
101103
},
102104

103105
effects: {
104-
*getMenuData({ payload }, { put }) {
105-
const { routes, authority } = payload;
106+
*getMenuData(_, { put, call }) {
107+
const { routes, authority } = yield call(queryMenu);
106108
const menuData = filterMenuData(memoizeOneFormatter(routes, authority));
107109
const breadcrumbNameMap = memoizeOneGetBreadcrumbNameMap(menuData);
108110
yield put({
109111
type: 'save',
110-
payload: { menuData, breadcrumbNameMap },
112+
payload: { menuData, breadcrumbNameMap, routerData: routes },
111113
});
112114
},
113115
},

src/pages/Account/Center/Applications.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { List, Card, Icon, Dropdown, Menu, Avatar, Tooltip } from 'antd';
33
import numeral from 'numeral';
44
import { connect } from 'dva';
55
import { formatWan } from '@/utils/utils';
6-
import stylesApplications from '../../List/Applications.less';
6+
import stylesApplications from '../../List/Search/Applications.less';
77

88
@connect(({ list }) => ({
99
list,

src/pages/Account/Center/Projects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { List, Card } from 'antd';
33
import moment from 'moment';
44
import { connect } from 'dva';
55
import AvatarList from '@/components/AvatarList';
6-
import stylesProjects from '../../List/Projects.less';
6+
import stylesProjects from '../../List/Search/Projects.less';
77

88
@connect(({ list }) => ({
99
list,
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ class Info extends Component {
1616
super(props);
1717
const { match, location } = props;
1818
const menuMap = {
19-
base: <FormattedMessage id="app.settings.menuMap.basic" defaultMessage="Basic Settings" />,
20-
security: (
19+
baseview: (
20+
<FormattedMessage id="app.settings.menuMap.basic" defaultMessage="Basic Settings" />
21+
),
22+
securityview: (
2123
<FormattedMessage id="app.settings.menuMap.security" defaultMessage="Security Settings" />
2224
),
23-
binding: (
25+
bindingview: (
2426
<FormattedMessage id="app.settings.menuMap.binding" defaultMessage="Account Binding" />
2527
),
26-
notification: (
28+
notificationview: (
2729
<FormattedMessage
2830
id="app.settings.menuMap.notification"
2931
defaultMessage="New Message Notification"

src/pages/Auth.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import React from 'react';
2+
import pathToRegexp from 'path-to-regexp';
3+
import Authorized from '@/utils/Authorized';
4+
import Exception403 from './Exception/403';
5+
6+
export default ({ children, location }) => {
7+
/* eslint-disable no-underscore-dangle */
8+
const { routerData } = window.g_app._store.getState().menu;
9+
const getRouteAuthority = (pathname, routeData) => {
10+
const routes = routeData.slice(); // clone
11+
12+
const getAuthority = (routeDatas, path) => {
13+
let authorities;
14+
routeDatas.forEach(route => {
15+
// check partial route
16+
if (pathToRegexp(`${route.path}(.*)`).test(path)) {
17+
if (route.authority) {
18+
authorities = route.authority;
19+
}
20+
// is exact route?
21+
if (!pathToRegexp(route.path).test(path) && route.routes) {
22+
authorities = getAuthority(route.routes, path);
23+
}
24+
}
25+
});
26+
return authorities;
27+
};
28+
29+
return getAuthority(routes, pathname);
30+
};
31+
32+
return (
33+
<Authorized
34+
authority={getRouteAuthority(location.pathname, routerData)}
35+
noMatch={<Exception403 />}
36+
>
37+
{children}
38+
</Authorized>
39+
);
40+
};

0 commit comments

Comments
 (0)