menu.js 3.4 KB
import axios from '../../util/http'
import {asyncRouterMap, constantRouterMap } from '@/router'
import {url} from '../../util/api'
/**
 * 获取路由列表
 */
function getMenuInfo(token) {
  return  new Promise((reslove, reject) =>{
    axios.get(url + "/auth/apps/" + 'e38b9e91e27df7295282cd1d876f5f26' + "/menus?shape=list").then(response => {
      reslove(response);
    })
  });
}

/**
 * cyl add
 * 用于判断父级菜单是否有子级菜单被选中
 * 如果有,那么该父节菜单也需要选中
 */
function existsChildren(router,roles){
  for(let i = 0; i < roles.length; i++){
    for(let k = 0; k < router.children.length; k++){
      if(router.children[k].children && router.children[k].children.length>0){
        existsChildren(router.children[k],roles);
      }else{
        if(roles[i].path === router.children[k].path){
          return true;
        }
      }
    }
  }
  return false;
}

/**
 * 判断是否有权限
 * @param {*} router
 * @param {*} roles
 */
function hasPerminssion(router, roles) {
  if(router.children && router.children.length>0){
    if(existsChildren(router,roles)){
        return true;
    }
  }
  // let status = false
  let status = true
  if (roles) {
    for(let i = 0; i < roles.length; i++){
      if(roles[i].path === router.path){
        status = true;
        break;
      }
    }
  }
  return status
}

/**
 * 根据后台返回的路由权限
 * 动态匹配路由
 * @param {*} asyncRouterMap
 * @param {*} roles
 */
function filterAsyncRouter(asyncRouterMap, roles) {
  const newData = [];
  // return routers
  asyncRouterMap.forEach(ele => {
    let obj = {};
    if (hasPerminssion(ele, roles)) {
      obj['path'] = ele.path;
      obj['name'] = ele.name;
      obj['meta'] = ele.meta || {}
      obj['component'] = ele.component;
      if (ele.children && ele.children.length > 0) {
        obj['children'] = filterAsyncRouter(ele.children, roles)
      } else {
        obj['children'] = []
      }
    } else {
      return false
    }
    newData.push(obj)
  });
  return newData
}
/**
 * 根据路由和当前跳转key(路由路径)
 * 获取第三级路由
 * @param {*} router
 * @param {*} key
 */
function filterTwoMenu(router, key) {
  let data = [],
  state = true;
  router.forEach(e => {
    if(e.path == key)
      if(e.children) {
        data = e.children
      } else {
        data = [];
      }
  })
  return data;
}

const menu = {
  state: {
    routers: constantRouterMap,
    addRouter: [],
    threeMenu:[],
  },
  mutations:{
    SET_ROUTERS: (state, routers) => {
      state.addRouter = routers;
      state.routers = constantRouterMap.concat(routers);
    },
    SET_THREEMENU: (state, routers) => {
      state.threeMenu = routers;
    }
  },
  actions: {
    GetMenuRole({ commit, state},data) {
      if (data === ''){
        commit('SET_ROUTERS', []);
        return
      }
      return new Promise((resolve, reject) => {

        getMenuInfo().then(response => {
          let accessedRouters = filterAsyncRouter(asyncRouterMap,response.data.list_data);
          // commit('SET_ROUTERS', accessedRouters);
          commit('SET_ROUTERS', accessedRouters);

          resolve(accessedRouters);
        })
      })
    },
    GetTwoMenu({commit, state},data) {
      return new Promise((reslove,reject) => {
        const tmenus = filterTwoMenu(state.routers, data);
        commit('SET_THREEMENU', tmenus);
        reslove(tmenus);
      })
    }
  },
}

export default menu