From 1072572ac6ebd149a3425a5e1a112c254d844936 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AD=99=E6=99=A8=E5=85=89?= <809200299@qq.com> Date: Fri, 23 Jun 2017 14:45:59 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E9=80=92=E5=BD=92=E8=BF=87=E6=BB=A4?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E8=B7=AF=E7=94=B1=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/store/modules/permission.js | 43 ++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/store/modules/permission.js b/src/store/modules/permission.js index 926c89b0..93996741 100644 --- a/src/store/modules/permission.js +++ b/src/store/modules/permission.js @@ -8,6 +8,26 @@ function hasPermission(roles, route) { } } +/** + * 递归过滤异步路由表,返回符合用户角色权限的路由表 + * @param asyncRouterMap + * @param roles + * @returns {Array.|*} + */ +function filterAsyncRouter(asyncRouterMap, roles) { + let accessedRouters = asyncRouterMap.filter(route => { + if(hasPermission(roles, route)) { + if(route.children && route.children.length) { + route.children = filterAsyncRouter(route.children, roles) + } + return true + } + return false + }) + return accessedRouters +} + + const permission = { state: { routers: constantRouterMap, @@ -25,23 +45,12 @@ const permission = { GenerateRoutes({ commit }, data) { return new Promise(resolve => { const { roles } = data; - const accessedRouters = asyncRouterMap.filter(v => { - if (roles.indexOf('admin') >= 0) return true; - if (hasPermission(roles, v)) { - if (v.children && v.children.length > 0) { - v.children = v.children.filter(child => { - if (hasPermission(roles, child)) { - return child - } - return false; - }); - return v - } else { - return v - } - } - return false; - }); + let accessedRouters + if (roles.indexOf('admin') >= 0) { + accessedRouters = asyncRouterMap + } else { + accessedRouters = filterAsyncRouter(asyncRouterMap, roles) + } commit('SET_ROUTERS', accessedRouters); resolve(); })