|
|
|
@ -1,4 +1,4 @@ |
|
|
|
import auth from '@/plugins/auth' |
|
|
|
import auth from '@/plugins/auth' |
|
|
|
import router, { constantRoutes, dynamicRoutes } from '@/router' |
|
|
|
import { getRouters } from '@/api/menu' |
|
|
|
import Layout from '@/layout/index' |
|
|
|
@ -30,7 +30,7 @@ const permission = { |
|
|
|
}, |
|
|
|
actions: { |
|
|
|
// 生成路由
|
|
|
|
GenerateRoutes({ commit }) { |
|
|
|
GenerateRoutes({ commit, rootState }) { |
|
|
|
return new Promise(resolve => { |
|
|
|
// 向后端请求路由数据
|
|
|
|
getRouters().then(res => { |
|
|
|
@ -40,9 +40,14 @@ const permission = { |
|
|
|
.split("\"component\":\"/").join("\"component\":\"") |
|
|
|
const sdata = JSON.parse(routeJson); |
|
|
|
const rdata = JSON.parse(routeJson) |
|
|
|
const isAdmin = rootState && rootState.user ? rootState.user.isAdmin === true : false |
|
|
|
const parentId = rootState && rootState.user ? String(rootState.user.parentId || '') : '' |
|
|
|
const canShowSystemMenu = isAdmin || parentId === '0' |
|
|
|
const sidebarSource = filterSystemRoutesByUser(sdata, canShowSystemMenu) |
|
|
|
const rewriteSource = filterSystemRoutesByUser(rdata, canShowSystemMenu) |
|
|
|
|
|
|
|
const sidebarRoutes = filterAsyncRouter(sdata) |
|
|
|
const rewriteRoutes = filterAsyncRouter(rdata, false, true) |
|
|
|
const sidebarRoutes = filterAsyncRouter(sidebarSource) |
|
|
|
const rewriteRoutes = filterAsyncRouter(rewriteSource, false, true) |
|
|
|
const first = findFirstLeafRoute(sidebarRoutes) |
|
|
|
if (first) { |
|
|
|
console.log(first) |
|
|
|
@ -80,14 +85,24 @@ const permission = { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// 遍历后台传来的路由字符串,转换为组件对象
|
|
|
|
function filterSystemRoutesByUser(routes, canShowSystemMenu) { |
|
|
|
if (!Array.isArray(routes)) { |
|
|
|
return [] |
|
|
|
} |
|
|
|
if (canShowSystemMenu) { |
|
|
|
return routes |
|
|
|
} |
|
|
|
return routes.filter(route => route && route.path !== '/system') |
|
|
|
} |
|
|
|
|
|
|
|
// 遍历后端传来的路由字符串,转换为组件对象
|
|
|
|
function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) { |
|
|
|
return asyncRouterMap.filter(route => { |
|
|
|
if (type && route.children) { |
|
|
|
route.children = filterChildren(route.children) |
|
|
|
} |
|
|
|
if (route.component) { |
|
|
|
// Layout ParentView 组件特殊处理
|
|
|
|
// Layout / ParentView 组件特殊处理
|
|
|
|
if (route.component === 'Layout') { |
|
|
|
route.component = Layout |
|
|
|
} else if (route.component === 'ParentView') { |
|
|
|
@ -162,3 +177,4 @@ export const loadView = (view) => { // 路由懒加载 |
|
|
|
} |
|
|
|
|
|
|
|
export default permission |
|
|
|
|
|
|
|
|