-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
184 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { | ||
asyncRouterMap, | ||
constantRouterMap | ||
} from '@/router/index' | ||
|
||
/** | ||
* 通过meta.role判断是否与当前用户权限匹配 | ||
* @param roles | ||
* @param route | ||
*/ | ||
function hasPermission(roles, route) { | ||
if (route.name) { | ||
return roles.some(role => route.name.includes(role)) | ||
} else { | ||
return true | ||
} | ||
} | ||
|
||
/** | ||
* 递归过滤异步路由表,返回符合用户角色权限的路由表 | ||
* @param asyncRouterMap | ||
* @param roles | ||
*/ | ||
function filterAsyncRouter(asyncRouterMap, roleauthname) { | ||
const accessedRouters = asyncRouterMap.filter(route => { | ||
if (hasPermission(roleauthname, route)) { | ||
if (route.children && route.children.length) { | ||
route.children = filterAsyncRouter(route.children, roleauthname) | ||
} | ||
return true | ||
} | ||
return false | ||
}) | ||
return accessedRouters | ||
} | ||
|
||
const permission = { | ||
state: { | ||
routers: constantRouterMap, | ||
addRouters: [], | ||
}, | ||
mutations: { | ||
SET_ROUTERS: (state, routers) => { | ||
state.addRouters = routers | ||
state.routers = constantRouterMap.concat(routers) | ||
}, | ||
}, | ||
actions: { | ||
GenerateRoutes({ | ||
commit | ||
}, data) { | ||
return new Promise(resolve => { | ||
const { | ||
roleauthname | ||
} = data | ||
let accessedRouters | ||
|
||
accessedRouters = filterAsyncRouter(asyncRouterMap, roleauthname) | ||
|
||
commit('SET_ROUTERS', accessedRouters) | ||
resolve() | ||
}) | ||
} | ||
} | ||
} | ||
|
||
export default permission |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.