Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

如何加权限 #115

Open
songyanbo opened this issue Apr 28, 2021 · 1 comment
Open

如何加权限 #115

songyanbo opened this issue Apr 28, 2021 · 1 comment

Comments

@songyanbo
Copy link

No description provided.

@songyanbo
Copy link
Author

songyanbo commented Apr 29, 2021

可以参考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);
`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant