2019-10-08 09:14:38 +08:00
|
|
|
import { constantRoutes } from '@/router'
|
|
|
|
import { getRouters } from '@/api/menu'
|
|
|
|
import Layout from '@/layout/index'
|
2020-11-28 20:39:03 +08:00
|
|
|
import ParentView from '@/components/ParentView';
|
2019-10-08 09:14:38 +08:00
|
|
|
|
|
|
|
const permission = {
|
|
|
|
state: {
|
|
|
|
routes: [],
|
|
|
|
addRoutes: []
|
|
|
|
},
|
|
|
|
mutations: {
|
|
|
|
SET_ROUTES: (state, routes) => {
|
|
|
|
state.addRoutes = routes
|
|
|
|
state.routes = constantRoutes.concat(routes)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
// 生成路由
|
|
|
|
GenerateRoutes({ commit }) {
|
|
|
|
return new Promise(resolve => {
|
|
|
|
// 向后端请求路由数据
|
|
|
|
getRouters().then(res => {
|
|
|
|
const accessedRoutes = filterAsyncRouter(res.data)
|
2019-12-03 18:02:35 +08:00
|
|
|
accessedRoutes.push({ path: '*', redirect: '/404', hidden: true })
|
2019-10-08 09:14:38 +08:00
|
|
|
commit('SET_ROUTES', accessedRoutes)
|
|
|
|
resolve(accessedRoutes)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 遍历后台传来的路由字符串,转换为组件对象
|
|
|
|
function filterAsyncRouter(asyncRouterMap) {
|
|
|
|
return asyncRouterMap.filter(route => {
|
|
|
|
if (route.component) {
|
2020-11-28 20:39:03 +08:00
|
|
|
// Layout ParentView 组件特殊处理
|
2019-10-08 09:14:38 +08:00
|
|
|
if (route.component === 'Layout') {
|
|
|
|
route.component = Layout
|
2020-11-28 20:39:03 +08:00
|
|
|
} else if (route.component === 'ParentView') {
|
|
|
|
route.component = ParentView
|
2019-10-08 09:14:38 +08:00
|
|
|
} else {
|
|
|
|
route.component = loadView(route.component)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (route.children != null && route.children && route.children.length) {
|
|
|
|
route.children = filterAsyncRouter(route.children)
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export const loadView = (view) => { // 路由懒加载
|
2020-04-21 14:48:34 +08:00
|
|
|
return (resolve) => require([`@/views/${view}`], resolve)
|
2019-10-08 09:14:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export default permission
|