158 lines
4.2 KiB
JavaScript
Executable File
158 lines
4.2 KiB
JavaScript
Executable File
// import { getRoleMenuList } from '@/api/account/role'
|
|
import {
|
|
getAuth
|
|
} from '@/api/account/roleList'
|
|
import {
|
|
constantRoutes
|
|
} from '@/router'
|
|
import {
|
|
getRoleId
|
|
} from '@/utils/auth' // get roleId from cookie
|
|
import {
|
|
assemblyRoute
|
|
} from '@/utils/serviceRoute'
|
|
// import { reject } from 'q'
|
|
|
|
import {
|
|
serverRouter, filterRouts
|
|
} from '@/utils/roleTreeList'
|
|
import { getFilterRoutes } from '@/utils/get-page-title'
|
|
|
|
/**
|
|
* Use meta.role to determine if the current user 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
|
|
}
|
|
}
|
|
/**
|
|
* 组装路由
|
|
* @param {Obejct} params
|
|
*/
|
|
function assemblyRouteList (params) {
|
|
const router = []
|
|
params.forEach(e => {
|
|
if (assemblyRoute(e.name)) {
|
|
let e_new = {
|
|
path: e.path,
|
|
name: e.name,
|
|
meta: {
|
|
...e.meta,
|
|
...{
|
|
apiArr: e.api_url || []
|
|
}
|
|
},
|
|
component: assemblyRoute(e.name)
|
|
}
|
|
if (e.children && e.children.length > 0) {
|
|
e_new = Object.assign({}, e_new, {
|
|
children: assemblyRouteList(e.children)
|
|
})
|
|
}
|
|
if (e.redirect) {
|
|
e_new = Object.assign({}, e_new, {
|
|
redirect: e.redirect
|
|
})
|
|
}
|
|
if (e.alwaysShow) {
|
|
e_new = Object.assign({}, e_new, {
|
|
alwaysShow: e.alwaysShow
|
|
})
|
|
}
|
|
if (e.hidden) {
|
|
e_new = Object.assign({}, e_new, {
|
|
hidden: true
|
|
})
|
|
}
|
|
router.push(e_new)
|
|
}
|
|
})
|
|
return router
|
|
}
|
|
/**
|
|
* 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
|
|
}
|
|
const state = {
|
|
routes: [],
|
|
addRoutes: []
|
|
}
|
|
|
|
const mutations = {
|
|
SET_ROUTES: (state, routes) => {
|
|
// console.log('原有路由:')
|
|
// console.log(constantRoutes)
|
|
// console.log('服务端路由:')
|
|
// console.log(routes)
|
|
state.addRoutes = routes
|
|
state.routes = constantRoutes.concat(routes)
|
|
// console.log('正式路由:')
|
|
// console.log(state.routes)
|
|
}
|
|
}
|
|
const actions = {
|
|
resetRouter ({ commit }) {
|
|
commit('SET_ROUTES', [])
|
|
},
|
|
generateRoutes ({ commit }, { next, NProgress }) {
|
|
return new Promise(resolve => {
|
|
// const a = assemblyRouteList(serverRouter)
|
|
// console.log(a)
|
|
// a.push({
|
|
// path: '*',
|
|
// redirect: '/404',
|
|
// hidden: true
|
|
// })
|
|
// const newFilterRouts = getFilterRoutes(a, filterRouts)
|
|
// console.log(newFilterRouts)
|
|
|
|
// commit('SET_ROUTES', newFilterRouts)
|
|
// // commit('SET_ROUTES', a)
|
|
// resolve(newFilterRouts)
|
|
const r = parseInt(getRoleId())
|
|
getAuth({ id: r }).then(res => {
|
|
//
|
|
// 组装路由 res 是我服务端存的动态路由表,
|
|
// 经过这个方法 这个方法组件导入进去,然后设置SET_ROUTES 就生成了导航
|
|
const a = assemblyRouteList(res.data.router)
|
|
a.push({
|
|
path: '*',
|
|
redirect: '/404',
|
|
hidden: true
|
|
})
|
|
const newFilterRouts = getFilterRoutes(a, filterRouts)
|
|
commit('SET_ROUTES', newFilterRouts)
|
|
resolve(newFilterRouts)
|
|
})
|
|
})
|
|
}
|
|
}
|
|
|
|
export default {
|
|
namespaced: true,
|
|
state,
|
|
mutations,
|
|
actions
|
|
}
|