Commit 4b12cc9e authored by 陈小聪's avatar 陈小聪 Committed by 陈帅

🐛 fix: #2964 and fix 403page no work when refresh the page (#2983)

* 🐛 fix: #2964 and fix 403page no work when refresh the page

* 🐛 add getPageAuthority

* 当访问的路由都不在菜单内时,直接过滤掉会有其他问题
parent 02c9740d
...@@ -52,8 +52,6 @@ class BasicLayout extends React.PureComponent { ...@@ -52,8 +52,6 @@ class BasicLayout extends React.PureComponent {
constructor(props) { constructor(props) {
super(props); super(props);
this.getPageTitle = memoizeOne(this.getPageTitle); this.getPageTitle = memoizeOne(this.getPageTitle);
this.getBreadcrumbNameMap = memoizeOne(this.getBreadcrumbNameMap, isEqual);
this.breadcrumbNameMap = this.getBreadcrumbNameMap();
this.matchParamsPath = memoizeOne(this.matchParamsPath, isEqual); this.matchParamsPath = memoizeOne(this.matchParamsPath, isEqual);
} }
...@@ -77,7 +75,6 @@ class BasicLayout extends React.PureComponent { ...@@ -77,7 +75,6 @@ class BasicLayout extends React.PureComponent {
componentDidUpdate(preProps) { componentDidUpdate(preProps) {
// After changing to phone mode, // After changing to phone mode,
// if collapsed is true, you need to click twice to display // if collapsed is true, you need to click twice to display
this.breadcrumbNameMap = this.getBreadcrumbNameMap();
const { collapsed, isMobile } = this.props; const { collapsed, isMobile } = this.props;
if (isMobile && !preProps.isMobile && !collapsed) { if (isMobile && !preProps.isMobile && !collapsed) {
this.handleMenuCollapse(false); this.handleMenuCollapse(false);
...@@ -85,10 +82,10 @@ class BasicLayout extends React.PureComponent { ...@@ -85,10 +82,10 @@ class BasicLayout extends React.PureComponent {
} }
getContext() { getContext() {
const { location } = this.props; const { location, breadcrumbNameMap } = this.props;
return { return {
location, location,
breadcrumbNameMap: this.breadcrumbNameMap, breadcrumbNameMap,
}; };
} }
...@@ -112,15 +109,13 @@ class BasicLayout extends React.PureComponent { ...@@ -112,15 +109,13 @@ class BasicLayout extends React.PureComponent {
return routerMap; return routerMap;
} }
matchParamsPath = pathname => { matchParamsPath = (pathname, breadcrumbNameMap) => {
const pathKey = Object.keys(this.breadcrumbNameMap).find(key => const pathKey = Object.keys(breadcrumbNameMap).find(key => pathToRegexp(key).test(pathname));
pathToRegexp(key).test(pathname) return breadcrumbNameMap[pathKey];
);
return this.breadcrumbNameMap[pathKey];
}; };
getPageTitle = pathname => { getPageTitle = (pathname, breadcrumbNameMap) => {
const currRouterData = this.matchParamsPath(pathname); const currRouterData = this.matchParamsPath(pathname, breadcrumbNameMap);
if (!currRouterData) { if (!currRouterData) {
return 'Ant Design Pro'; return 'Ant Design Pro';
...@@ -129,6 +124,7 @@ class BasicLayout extends React.PureComponent { ...@@ -129,6 +124,7 @@ class BasicLayout extends React.PureComponent {
id: currRouterData.locale || currRouterData.name, id: currRouterData.locale || currRouterData.name,
defaultMessage: currRouterData.name, defaultMessage: currRouterData.name,
}); });
return `${pageName} - Ant Design Pro`; return `${pageName} - Ant Design Pro`;
}; };
...@@ -175,9 +171,11 @@ class BasicLayout extends React.PureComponent { ...@@ -175,9 +171,11 @@ class BasicLayout extends React.PureComponent {
location: { pathname }, location: { pathname },
isMobile, isMobile,
menuData, menuData,
breadcrumbNameMap,
} = this.props; } = this.props;
const isTop = PropsLayout === 'topmenu'; const isTop = PropsLayout === 'topmenu';
const routerConfig = this.matchParamsPath(pathname); const routerConfig = this.matchParamsPath(pathname, breadcrumbNameMap);
const layout = ( const layout = (
<Layout> <Layout>
{isTop && !isMobile ? null : ( {isTop && !isMobile ? null : (
...@@ -217,7 +215,7 @@ class BasicLayout extends React.PureComponent { ...@@ -217,7 +215,7 @@ class BasicLayout extends React.PureComponent {
); );
return ( return (
<React.Fragment> <React.Fragment>
<DocumentTitle title={this.getPageTitle(pathname)}> <DocumentTitle title={this.getPageTitle(pathname, breadcrumbNameMap)}>
<ContainerQuery query={query}> <ContainerQuery query={query}>
{params => ( {params => (
<Context.Provider value={this.getContext()}> <Context.Provider value={this.getContext()}>
...@@ -236,6 +234,7 @@ export default connect(({ global, setting, menu }) => ({ ...@@ -236,6 +234,7 @@ export default connect(({ global, setting, menu }) => ({
collapsed: global.collapsed, collapsed: global.collapsed,
layout: setting.layout, layout: setting.layout,
menuData: menu.menuData, menuData: menu.menuData,
breadcrumbNameMap: menu.breadcrumbNameMap,
...setting, ...setting,
}))(props => ( }))(props => (
<Media query="(max-width: 599px)"> <Media query="(max-width: 599px)">
......
...@@ -70,20 +70,44 @@ const filterMenuData = menuData => { ...@@ -70,20 +70,44 @@ const filterMenuData = menuData => {
}) })
.filter(item => item); .filter(item => item);
}; };
/**
* 获取面包屑映射
* @param {Object} menuData 菜单配置
*/
const getBreadcrumbNameMap = menuData => {
const routerMap = {};
const flattenMenuData = data => {
data.forEach(menuItem => {
if (menuItem.children) {
flattenMenuData(menuItem.children);
}
// Reduce memory usage
routerMap[menuItem.path] = menuItem;
});
};
flattenMenuData(menuData);
return routerMap;
};
const memoizeOneGetBreadcrumbNameMap = memoizeOne(getBreadcrumbNameMap, isEqual);
export default { export default {
namespace: 'menu', namespace: 'menu',
state: { state: {
menuData: [], menuData: [],
breadcrumbNameMap: {},
}, },
effects: { effects: {
*getMenuData({ payload }, { put }) { *getMenuData({ payload }, { put }) {
const { routes, authority } = payload; const { routes, authority } = payload;
const menuData = filterMenuData(memoizeOneFormatter(routes, authority));
const breadcrumbNameMap = memoizeOneGetBreadcrumbNameMap(menuData);
yield put({ yield put({
type: 'save', type: 'save',
payload: filterMenuData(memoizeOneFormatter(routes, authority)), payload: { menuData, breadcrumbNameMap },
}); });
}, },
}, },
...@@ -92,7 +116,7 @@ export default { ...@@ -92,7 +116,7 @@ export default {
save(state, action) { save(state, action) {
return { return {
...state, ...state,
menuData: action.payload, ...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