鍍金池/ 問答/HTML/ antd 進(jìn)入菜單沒有權(quán)限回跳到403頁(yè)面,但是實(shí)際這不是我們想要的。

antd 進(jìn)入菜單沒有權(quán)限回跳到403頁(yè)面,但是實(shí)際這不是我們想要的。

問題描述

我給每個(gè)菜單設(shè)置了權(quán)限,但是實(shí)際登陸以后是按照路由里面的順序去找到登陸以后跳轉(zhuǎn)的頁(yè)面,如果恰好第一個(gè)是沒有權(quán)限的,那就會(huì)跳到403頁(yè)面,但是實(shí)際情況是我想讓它能跳到有權(quán)限的第一個(gè)菜單。

問題出現(xiàn)的環(huán)境背景及自己嘗試過哪些方法

我通過在siderMenu文件里面獲取當(dāng)前用戶的權(quán)限,然后和菜單列表里面的權(quán)限做對(duì)比,然后找到有權(quán)限的第一個(gè)菜單,去加載它,但是會(huì)出現(xiàn)各種問題,比如刷新頁(yè)面url地址不變卻跳到其他頁(yè)面,子菜單點(diǎn)擊以后會(huì)隱藏,點(diǎn)擊首頁(yè)也會(huì)跳到403,刷新頁(yè)面會(huì)跳到第一個(gè)菜單,等等一系列問題。

相關(guān)代碼

// 請(qǐng)把代碼文本粘貼到下方(請(qǐng)勿用圖片代替代碼)

//  處理一進(jìn)頁(yè)面403
const getAuthorityArr = getAuthority().split(',');
const menuArr = this.props.menuData;
if (this.props !== nextProps) {
  let clog = '0'; // 1有權(quán)限,0沒權(quán)限
  let currentPathAuthorized = []; //  獲取當(dāng)前當(dāng)前path的權(quán)限
  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),
    });
  }
}

你期待的結(jié)果是什么?實(shí)際看到的錯(cuò)誤信息又是什么?

期待能一進(jìn)入頁(yè)面會(huì)根據(jù)權(quán)限找到相應(yīng)的頁(yè)面,而不是403。

回答
編輯回答
柒喵

其實(shí) antd在srclayoutsBasicLayout.js 里面是有對(duì)于權(quán)限的判斷的,我出現(xiàn)問題的原因是menu里面加了權(quán)限,但是router里面有的地方?jīng)]有加權(quán)限,導(dǎo)致以為我的某些菜單是開放權(quán)限的,所以匹配出錯(cuò),只要在router里面加上權(quán)限就好了

2017年11月28日 14:41