Unverified Commit 91be534a authored by 陈小聪's avatar 陈小聪 Committed by GitHub

remove menu authorized (#2911)

* Close: https://github.com/ant-design/ant-design-pro/issues/2910
* remove menu authorized to menu-model
parent c1ce6ec8
...@@ -113,7 +113,9 @@ class Login extends Component { ...@@ -113,7 +113,9 @@ class Login extends Component {
</Tabs> </Tabs>
{otherChildren} {otherChildren}
</React.Fragment> </React.Fragment>
) : children} ) : (
children
)}
</Form> </Form>
</div> </div>
</LoginContext.Provider> </LoginContext.Provider>
......
...@@ -40,11 +40,7 @@ export default class BaseMenu extends PureComponent { ...@@ -40,11 +40,7 @@ export default class BaseMenu extends PureComponent {
} }
return menusData return menusData
.filter(item => item.name && !item.hideInMenu) .filter(item => item.name && !item.hideInMenu)
.map(item => { .map(item => this.getSubMenuOrItem(item, parent))
// make dom
const ItemDom = this.getSubMenuOrItem(item, parent);
return this.checkPermissionItem(item.authority, ItemDom);
})
.filter(item => item); .filter(item => item);
}; };
...@@ -121,16 +117,6 @@ export default class BaseMenu extends PureComponent { ...@@ -121,16 +117,6 @@ export default class BaseMenu extends PureComponent {
); );
}; };
// permission to check
checkPermissionItem = (authority, ItemDom) => {
const { Authorized } = this.props;
if (Authorized && Authorized.check) {
const { check } = Authorized;
return check(authority, ItemDom);
}
return ItemDom;
};
conversionPath = path => { conversionPath = path => {
if (path && path.indexOf('http') === 0) { if (path && path.indexOf('http') === 0) {
return path; return path;
......
...@@ -23,39 +23,6 @@ const SettingDrawer = React.lazy(() => import('@/components/SettingDrawer')); ...@@ -23,39 +23,6 @@ const SettingDrawer = React.lazy(() => import('@/components/SettingDrawer'));
const { Content } = Layout; const { Content } = Layout;
function mapRoutesToMenu(routes, parentAuthority, parentName) {
return routes
.map(item => {
if (!item.name || !item.path) {
return null;
}
let locale = 'menu';
if (parentName) {
locale = `${parentName}.${item.name}`;
} else {
locale = `menu.${item.name}`;
}
const result = {
...item,
name: formatMessage({ id: locale, defaultMessage: item.name }),
locale,
authority: item.authority || parentAuthority,
};
if (item.routes) {
const children = mapRoutesToMenu(item.routes, item.authority, locale);
// Reduce memory usage
result.children = children;
}
delete result.routes;
return result;
})
.filter(item => item);
}
const memoizedMapRoutesToMenu = memoizeOne(mapRoutesToMenu, isEqual);
const query = { const query = {
'screen-xs': { 'screen-xs': {
maxWidth: 575, maxWidth: 575,
...@@ -90,18 +57,21 @@ class BasicLayout extends React.PureComponent { ...@@ -90,18 +57,21 @@ class BasicLayout extends React.PureComponent {
this.matchParamsPath = memoizeOne(this.matchParamsPath, isEqual); this.matchParamsPath = memoizeOne(this.matchParamsPath, isEqual);
} }
state = {
menuData: this.getMenuData(),
};
componentDidMount() { componentDidMount() {
const { dispatch } = this.props; const {
dispatch,
route: { routes, authority },
} = this.props;
dispatch({ dispatch({
type: 'user/fetchCurrent', type: 'user/fetchCurrent',
}); });
dispatch({ dispatch({
type: 'setting/getSetting', type: 'setting/getSetting',
}); });
dispatch({
type: 'menu/getMenuData',
payload: { routes, authority },
});
} }
componentDidUpdate(preProps) { componentDidUpdate(preProps) {
...@@ -114,10 +84,6 @@ class BasicLayout extends React.PureComponent { ...@@ -114,10 +84,6 @@ class BasicLayout extends React.PureComponent {
} }
} }
componentWillUnmount() {
cancelAnimationFrame(this.renderRef);
}
getContext() { getContext() {
const { location } = this.props; const { location } = this.props;
return { return {
...@@ -126,19 +92,13 @@ class BasicLayout extends React.PureComponent { ...@@ -126,19 +92,13 @@ class BasicLayout extends React.PureComponent {
}; };
} }
getMenuData() {
const {
route: { routes, authority },
} = this.props;
return memoizedMapRoutesToMenu(routes, authority);
}
/** /**
* 获取面包屑映射 * 获取面包屑映射
* @param {Object} menuData 菜单配置 * @param {Object} menuData 菜单配置
*/ */
getBreadcrumbNameMap() { getBreadcrumbNameMap() {
const routerMap = {}; const routerMap = {};
const { menuData } = this.props;
const flattenMenuData = data => { const flattenMenuData = data => {
data.forEach(menuItem => { data.forEach(menuItem => {
if (menuItem.children) { if (menuItem.children) {
...@@ -148,7 +108,7 @@ class BasicLayout extends React.PureComponent { ...@@ -148,7 +108,7 @@ class BasicLayout extends React.PureComponent {
routerMap[menuItem.path] = menuItem; routerMap[menuItem.path] = menuItem;
}); });
}; };
flattenMenuData(this.getMenuData()); flattenMenuData(menuData);
return routerMap; return routerMap;
} }
...@@ -214,8 +174,8 @@ class BasicLayout extends React.PureComponent { ...@@ -214,8 +174,8 @@ class BasicLayout extends React.PureComponent {
children, children,
location: { pathname }, location: { pathname },
isMobile, isMobile,
menuData,
} = this.props; } = this.props;
const { menuData } = this.state;
const isTop = PropsLayout === 'topmenu'; const isTop = PropsLayout === 'topmenu';
const routerConfig = this.matchParamsPath(pathname); const routerConfig = this.matchParamsPath(pathname);
const layout = ( const layout = (
...@@ -223,7 +183,6 @@ class BasicLayout extends React.PureComponent { ...@@ -223,7 +183,6 @@ class BasicLayout extends React.PureComponent {
{isTop && !isMobile ? null : ( {isTop && !isMobile ? null : (
<SiderMenu <SiderMenu
logo={logo} logo={logo}
Authorized={Authorized}
theme={navTheme} theme={navTheme}
onCollapse={this.handleMenuCollapse} onCollapse={this.handleMenuCollapse}
menuData={menuData} menuData={menuData}
...@@ -273,9 +232,10 @@ class BasicLayout extends React.PureComponent { ...@@ -273,9 +232,10 @@ class BasicLayout extends React.PureComponent {
} }
} }
export default connect(({ global, setting }) => ({ export default connect(({ global, setting, menu }) => ({
collapsed: global.collapsed, collapsed: global.collapsed,
layout: setting.layout, layout: setting.layout,
menuData: menu.menuData,
...setting, ...setting,
}))(props => ( }))(props => (
<Media query="(max-width: 599px)"> <Media query="(max-width: 599px)">
......
...@@ -7,7 +7,6 @@ import router from 'umi/router'; ...@@ -7,7 +7,6 @@ import router from 'umi/router';
import GlobalHeader from '@/components/GlobalHeader'; import GlobalHeader from '@/components/GlobalHeader';
import TopNavHeader from '@/components/TopNavHeader'; import TopNavHeader from '@/components/TopNavHeader';
import styles from './Header.less'; import styles from './Header.less';
import Authorized from '@/utils/Authorized';
const { Header } = Layout; const { Header } = Layout;
...@@ -128,7 +127,6 @@ class HeaderView extends PureComponent { ...@@ -128,7 +127,6 @@ class HeaderView extends PureComponent {
<TopNavHeader <TopNavHeader
theme={navTheme} theme={navTheme}
mode="horizontal" mode="horizontal"
Authorized={Authorized}
onCollapse={handleMenuCollapse} onCollapse={handleMenuCollapse}
onNoticeClear={this.handleNoticeClear} onNoticeClear={this.handleNoticeClear}
onMenuClick={this.handleMenuClick} onMenuClick={this.handleMenuClick}
......
import memoizeOne from 'memoize-one';
import isEqual from 'lodash/isEqual';
import { formatMessage } from 'umi/locale';
import Authorized from '@/utils/Authorized';
const { check } = Authorized;
// Conversion router to menu.
function formatter(data, parentAuthority, parentName) {
return data
.map(item => {
if (!item.name || !item.path) {
return null;
}
let locale = 'menu';
if (parentName) {
locale = `${parentName}.${item.name}`;
} else {
locale = `menu.${item.name}`;
}
const result = {
...item,
name: formatMessage({ id: locale, defaultMessage: item.name }),
locale,
authority: item.authority || parentAuthority,
};
if (item.routes) {
const children = formatter(item.routes, item.authority, locale);
// Reduce memory usage
result.children = children;
}
delete result.routes;
return result;
})
.filter(item => item);
}
const memoizeOneFormatter = memoizeOne(formatter, isEqual);
/**
* get SubMenu or Item
*/
const getSubMenu = item => {
// doc: add hideChildrenInMenu
if (item.children && !item.hideChildrenInMenu && item.children.some(child => child.name)) {
return {
...item,
children: filterMenuData(item.children), // eslint-disable-line
};
}
return item;
};
/**
* filter menuData
*/
const filterMenuData = menuData => {
if (!menuData) {
return [];
}
return menuData
.filter(item => item.name && !item.hideInMenu)
.map(item => {
// make dom
const ItemDom = getSubMenu(item);
const data = check(item.authority, ItemDom);
return data;
})
.filter(item => item);
};
export default {
namespace: 'menu',
state: {
menuData: [],
},
effects: {
*getMenuData({ payload }, { put }) {
const { routes, authority } = payload;
yield put({
type: 'save',
payload: filterMenuData(memoizeOneFormatter(routes, authority)),
});
},
},
reducers: {
save(state, action) {
return {
...state,
menuData: action.payload,
};
},
},
};
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