|
|
|
@ -1,43 +1,13 @@
|
|
|
|
|
import { asyncRoutes, constantRoutes } from '@/router'
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Use meta.role to determine if the current userinfo has permission
|
|
|
|
|
* @param roles
|
|
|
|
|
* @param route
|
|
|
|
|
*/
|
|
|
|
|
function hasPermission(roles, route) {
|
|
|
|
|
if (route.meta && route.meta.roles) {
|
|
|
|
|
return roles.some(role => route.meta.roles.includes(role))
|
|
|
|
|
} else {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Filter asynchronous routing tables by recursion
|
|
|
|
|
* @param routes asyncRoutes
|
|
|
|
|
* @param roles
|
|
|
|
|
*/
|
|
|
|
|
export function filterAsyncRoutes(routes, roles) {
|
|
|
|
|
const res = []
|
|
|
|
|
|
|
|
|
|
routes.forEach(route => {
|
|
|
|
|
const tmp = { ...route }
|
|
|
|
|
if (hasPermission(roles, tmp)) {
|
|
|
|
|
if (tmp.children) {
|
|
|
|
|
tmp.children = filterAsyncRoutes(tmp.children, roles)
|
|
|
|
|
}
|
|
|
|
|
res.push(tmp)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
}
|
|
|
|
|
import router, { constantRoutes } from '@/router'
|
|
|
|
|
import { user_power } from '@/api/user/user'
|
|
|
|
|
import { getALLRouter } from '@/utils'
|
|
|
|
|
|
|
|
|
|
const state = {
|
|
|
|
|
routes: [],
|
|
|
|
|
addRoutes: [],
|
|
|
|
|
roleData: {}
|
|
|
|
|
// 存储home页区块路由,下拉选项路由
|
|
|
|
|
roleData: {},
|
|
|
|
|
opts: []
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const mutations = {
|
|
|
|
@ -47,20 +17,25 @@ const mutations = {
|
|
|
|
|
},
|
|
|
|
|
SET_ROLE_DATA: (state, data) => {
|
|
|
|
|
state.roleData = data
|
|
|
|
|
},
|
|
|
|
|
SET_OPTS: (state, opt) => {
|
|
|
|
|
state.opts = opt
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const actions = {
|
|
|
|
|
generateRoutes({ commit }, roles) {
|
|
|
|
|
return new Promise(resolve => {
|
|
|
|
|
let accessedRoutes
|
|
|
|
|
if (roles.includes('admin')) {
|
|
|
|
|
accessedRoutes = asyncRoutes || []
|
|
|
|
|
} else {
|
|
|
|
|
accessedRoutes = filterAsyncRoutes(asyncRoutes, roles)
|
|
|
|
|
}
|
|
|
|
|
commit('SET_ROUTES', accessedRoutes)
|
|
|
|
|
resolve(accessedRoutes)
|
|
|
|
|
generateRoutes({ commit }) {
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
user_power().then(res => {
|
|
|
|
|
const { asyncRoutes, option } = getALLRouter(res.data)
|
|
|
|
|
commit('SET_OPTS', option)
|
|
|
|
|
commit('SET_ROLE_DATA', res.data)
|
|
|
|
|
commit('SET_ROUTES', asyncRoutes)
|
|
|
|
|
router.addRoutes(asyncRoutes)
|
|
|
|
|
resolve()
|
|
|
|
|
}).catch(error => {
|
|
|
|
|
reject(error)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|