修改分模块
This commit is contained in:
@@ -1,36 +1,38 @@
|
||||
import { dynamicRoutes, constantRoutes } from '@/router'
|
||||
import { getListItems } from '@/utils/tree'
|
||||
/*
|
||||
* @Author: zwq
|
||||
* @Date: 2021-09-18 16:09:08
|
||||
* @LastEditors: zwq
|
||||
* @LastEditTime: 2021-12-22 15:43:46
|
||||
* @Description:
|
||||
*/
|
||||
import { asyncRoutes, constantRoutes } from '@/router'
|
||||
|
||||
/**
|
||||
* Use meta.role to determine if the current user has permission
|
||||
* @param roles
|
||||
* @param route
|
||||
*/
|
||||
function hasPermission(menuList, route) {
|
||||
// if (route.meta && route.meta.roles) {
|
||||
// return roles.some(role => route.meta.roles.includes(role))
|
||||
// } else {
|
||||
// return true
|
||||
// }
|
||||
if (route.hidden) {
|
||||
function hasPermission(roles, route) {
|
||||
if (route.meta && route.meta.roles) {
|
||||
return roles.some(role => route.meta.roles.includes(role))
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
return menuList.some(m => route.path === m.h)
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter asynchronous routing tables by recursion
|
||||
* @param routes asyncRoutes
|
||||
* @param menuList
|
||||
* @param roles
|
||||
*/
|
||||
export function filterAsyncRoutes(routes, menuList) {
|
||||
export function filterAsyncRoutes(routes, roles) {
|
||||
const res = []
|
||||
|
||||
routes.forEach(route => {
|
||||
const tmp = { ...route }
|
||||
if (hasPermission(menuList, tmp)) {
|
||||
if (hasPermission(roles, tmp)) {
|
||||
if (tmp.children) {
|
||||
tmp.children = filterAsyncRoutes(tmp.children, menuList)
|
||||
tmp.children = filterAsyncRoutes(tmp.children, roles)
|
||||
}
|
||||
res.push(tmp)
|
||||
}
|
||||
@@ -52,19 +54,13 @@ const mutations = {
|
||||
}
|
||||
|
||||
const actions = {
|
||||
generateRoutes({ commit }, menus) {
|
||||
generateRoutes({ commit }, roles) {
|
||||
return new Promise(resolve => {
|
||||
let accessedRoutes
|
||||
console.log(menus)
|
||||
if (!menus) {
|
||||
accessedRoutes = []
|
||||
if (roles.includes('admin')) {
|
||||
accessedRoutes = asyncRoutes || []
|
||||
} else {
|
||||
const menuList = []
|
||||
getListItems(menus, node => menuList.push(node))
|
||||
console.log(menuList)
|
||||
console.log(dynamicRoutes)
|
||||
accessedRoutes = filterAsyncRoutes(dynamicRoutes, menuList)
|
||||
console.log(accessedRoutes)
|
||||
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
|
||||
}
|
||||
commit('SET_ROUTES', accessedRoutes)
|
||||
resolve(accessedRoutes)
|
||||
|
||||
Reference in New Issue
Block a user