如何加权限
songyanbo opened this issue · 1 comments
可以参考ant-design-pro的权限,只需要在utils/core.js做如下更改即可实现,还有记得在具体的页面路由配置中增加authority: ['admin'],代码如下:
`import React from 'react';
import dynamic from 'dva/dynamic';
import { Route, Switch, Redirect } from 'dva/router';
import DocumentTitle from 'react-document-title';
import assign from 'object-assign';
import $$ from 'cmn-utils';
import config from '@/config';
import Authorized from '../utils/Authorized';
const { AuthorizedRoute } = Authorized;
/**
- 生成动态组件
- @param {*} app
- @param {*} models
- @param {*} component
*/
export const dynamicWrapper = (app, models, component) =>
dynamic({
app,
models: () => models,
component
});
/**
- 生成一组路由
- @param {*} app
- @param {*} routesConfig
/
export const createRoutes = (app, routesConfig) => {
return (
{routesConfig(app).map(config => createRoute(app, () => config))}
);
};
// 路由映射表
window.dva_router_pathMap = {};
/* - 生成单个路由
- @param {*} app
- @param {*} routesConfig
/
export const createRoute = (app, routesConfig) => {
const {
component: Comp,
path,
indexRoute,
title,
exact,
authority,
...otherProps
} = routesConfig(app);
if (path && path !== '/') {
window.dva_router_pathMap[path] = { path, title, ...otherProps };
// 为子路由增加parentPath
if (otherProps.childRoutes && otherProps.childRoutes.length) {
otherProps.childRoutes.forEach(item => {
if (window.dva_router_pathMap[item.key]) {
window.dva_router_pathMap[item.key].parentPath = path;
}
});
}
}
const routeProps = assign(
{
key: path || $$.randomStr(4),
render: props => (
<DocumentTitle
title={
config.htmlTitle ? config.htmlTitle.replace(/{.}/gi, title) : title
}
>
<Comp routerData={otherProps} {...props} />
)
},
path && {
path: path
},
exact && {
exact: exact
},
authority&&{
authority: authority
}
);
// before add authorized
// if (indexRoute) {
// return [
// <Redirect key={path + '_redirect'} exact from={path} to={indexRoute} />,
// <Route {...routeProps} />
// ];
// }
// return <Route {...routeProps} />;
if (indexRoute) {
return [
<Redirect key={path + '_redirect'} exact from={path} to={indexRoute} />,
<AuthorizedRoute
{...routeProps}
redirectPath="/403"
/>
];
}
return <AuthorizedRoute
{...routeProps}
redirectPath="/403"
/>;
};
页面路由配置更改:
import { dynamicWrapper, createRoute } from '@/utils/core';
const routesConfig = app => ({
path: '/myproject',
title: '我的项目',
component: dynamicWrapper(app, [import('./model')], () => import('./components')),
exact: true,
authority: ['admin','audit'],
});
export default app => createRoute(app, routesConfig);
`