feature[Permission]: add role permission management page (#1605)
增加角色权限管理菜单
This commit is contained in:
		@@ -1,4 +1,4 @@
 | 
			
		||||
import { asyncRouterMap, constantRouterMap } from '@/router'
 | 
			
		||||
import { asyncRoutes, constantRoutes } from '@/router'
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 通过meta.role判断是否与当前用户权限匹配
 | 
			
		||||
@@ -15,17 +15,17 @@ function hasPermission(roles, route) {
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * 递归过滤异步路由表,返回符合用户角色权限的路由表
 | 
			
		||||
 * @param routes asyncRouterMap
 | 
			
		||||
 * @param routes asyncRoutes
 | 
			
		||||
 * @param roles
 | 
			
		||||
 */
 | 
			
		||||
function filterAsyncRouter(routes, roles) {
 | 
			
		||||
export function filterAsyncRoutes(routes, roles) {
 | 
			
		||||
  const res = []
 | 
			
		||||
 | 
			
		||||
  routes.forEach(route => {
 | 
			
		||||
    const tmp = { ...route }
 | 
			
		||||
    if (hasPermission(roles, tmp)) {
 | 
			
		||||
      if (tmp.children) {
 | 
			
		||||
        tmp.children = filterAsyncRouter(tmp.children, roles)
 | 
			
		||||
        tmp.children = filterAsyncRoutes(tmp.children, roles)
 | 
			
		||||
      }
 | 
			
		||||
      res.push(tmp)
 | 
			
		||||
    }
 | 
			
		||||
@@ -36,27 +36,27 @@ function filterAsyncRouter(routes, roles) {
 | 
			
		||||
 | 
			
		||||
const permission = {
 | 
			
		||||
  state: {
 | 
			
		||||
    routers: [],
 | 
			
		||||
    addRouters: []
 | 
			
		||||
    routes: [],
 | 
			
		||||
    addRoutes: []
 | 
			
		||||
  },
 | 
			
		||||
  mutations: {
 | 
			
		||||
    SET_ROUTERS: (state, routers) => {
 | 
			
		||||
      state.addRouters = routers
 | 
			
		||||
      state.routers = constantRouterMap.concat(routers)
 | 
			
		||||
    SET_ROUTES: (state, routes) => {
 | 
			
		||||
      state.addRoutes = routes
 | 
			
		||||
      state.routes = constantRoutes.concat(routes)
 | 
			
		||||
    }
 | 
			
		||||
  },
 | 
			
		||||
  actions: {
 | 
			
		||||
    GenerateRoutes({ commit }, data) {
 | 
			
		||||
      return new Promise(resolve => {
 | 
			
		||||
        const { roles } = data
 | 
			
		||||
        let accessedRouters
 | 
			
		||||
        let accessedRoutes
 | 
			
		||||
        if (roles.includes('admin')) {
 | 
			
		||||
          accessedRouters = asyncRouterMap
 | 
			
		||||
          accessedRoutes = asyncRoutes
 | 
			
		||||
        } else {
 | 
			
		||||
          accessedRouters = filterAsyncRouter(asyncRouterMap, roles)
 | 
			
		||||
          accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
 | 
			
		||||
        }
 | 
			
		||||
        commit('SET_ROUTERS', accessedRouters)
 | 
			
		||||
        resolve()
 | 
			
		||||
        commit('SET_ROUTES', accessedRoutes)
 | 
			
		||||
        resolve(accessedRoutes)
 | 
			
		||||
      })
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user