问题描述
我给每个菜单设置了权限,但是实际登陆以后是按照路由里面的顺序去找到登陆以后跳转的页面,如果恰好第一个是没有权限的,那就会跳到403页面,但是实际情况是我想让它能跳到有权限的第一个菜单。
问题出现的环境背景及自己尝试过哪些方法
我通过在siderMenu文件里面获取当前用户的权限,然后和菜单列表里面的权限做对比,然后找到有权限的第一个菜单,去加载它,但是会出现各种问题,比如刷新页面url地址不变却跳到其他页面,子菜单点击以后会隐藏,点击首页也会跳到403,刷新页面会跳到第一个菜单,等等一系列问题。
相关代码
// 请把代码文本粘贴到下方(请勿用图片代替代码)
// 处理一进页面403
const getAuthorityArr = getAuthority().split(',');
const menuArr = this.props.menuData;
if (this.props !== nextProps) {
let clog = '0'; // 1有权限,0没权限
let currentPathAuthorized = []; // 获取当前当前path的权限
const getPathAuthority = (menuData, location) => {
menuData.forEach((item) => {
if (item.path === location.pathname) {
currentPathAuthorized = item.authority;
}
if (item.children) {
item.children.forEach((childrenItem) => {
if (childrenItem.path === location.pathname) {
currentPathAuthorized = childrenItem.authority;
}
});
}
})
}
getPathAuthority(nextProps.menuData, nextProps.location);
for (let i = 0; i < currentPathAuthorized.length; i += 1) {
for (let j = 0; j < nextProps.menuData.length; j += 1) {
if (nextProps.menuData[j].authority.indexOf(currentPathAuthorized[i]) > -1) {
clog = '1';
break;
}
}
}
// if (clog === '1') {
if (true) {
this.setState({
openKeys: this.getDefaultCollapsedSubMenus(nextProps),
});
} else {
let menuPath = '';
for (let i = 0; i < getAuthorityArr.length; i += 1) {
for (let j = 0; j < menuArr.length; j += 1) {
if (menuArr[j].authority.indexOf(getAuthorityArr[i]) > -1) {
menuPath = menuArr[j].path;
break;
}
}
}
const newProps = { ...this.props }
newProps.location.pathname = menuPath;
this.setState({
openKeys: this.getDefaultCollapsedSubMenus(newProps),
});
}
}
你期待的结果是什么?实际看到的错误信息又是什么?
期待能一进入页面会根据权限找到相应的页面,而不是403。
其实 antd在srclayoutsBasicLayout.js 里面是有对于权限的判断的,我出现问题的原因是menu里面加了权限,但是router里面有的地方没有加权限,导致以为我的某些菜单是开放权限的,所以匹配出错,只要在router里面加上权限就好了