提交 | 用户 | 时间
|
e57a89
|
1 |
import auth from '@/plugins/auth' |
懒 |
2 |
import router, { constantRoutes, dynamicRoutes } from '@/router' |
|
3 |
import { getRouters } from '@/api/menu' |
|
4 |
import Layout from '@/layout/index' |
|
5 |
import ParentView from '@/components/ParentView' |
|
6 |
import InnerLink from '@/layout/components/InnerLink' |
|
7 |
|
|
8 |
const permission = { |
|
9 |
state: { |
|
10 |
routes: [], |
|
11 |
addRoutes: [], |
|
12 |
defaultRoutes: [], |
|
13 |
topbarRouters: [], |
|
14 |
sidebarRouters: [] |
|
15 |
}, |
|
16 |
mutations: { |
|
17 |
SET_ROUTES: (state, routes) => { |
|
18 |
state.addRoutes = routes |
|
19 |
state.routes = constantRoutes.concat(routes) |
|
20 |
}, |
|
21 |
SET_DEFAULT_ROUTES: (state, routes) => { |
|
22 |
state.defaultRoutes = constantRoutes.concat(routes) |
|
23 |
}, |
|
24 |
SET_TOPBAR_ROUTES: (state, routes) => { |
|
25 |
state.topbarRouters = routes |
|
26 |
}, |
|
27 |
SET_SIDEBAR_ROUTERS: (state, routes) => { |
|
28 |
state.sidebarRouters = routes |
|
29 |
}, |
|
30 |
}, |
|
31 |
actions: { |
|
32 |
// 生成路由 |
|
33 |
GenerateRoutes({ commit }) { |
|
34 |
return new Promise(resolve => { |
|
35 |
// 向后端请求路由数据 |
|
36 |
getRouters().then(res => { |
|
37 |
const sdata = JSON.parse(JSON.stringify(res.data)) |
|
38 |
const rdata = JSON.parse(JSON.stringify(res.data)) |
|
39 |
const sidebarRoutes = filterAsyncRouter(sdata) |
|
40 |
const rewriteRoutes = filterAsyncRouter(rdata, false, true) |
|
41 |
const asyncRoutes = filterDynamicRoutes(dynamicRoutes); |
|
42 |
rewriteRoutes.push({ path: '*', redirect: '/404', hidden: true }) |
|
43 |
router.addRoutes(asyncRoutes); |
|
44 |
commit('SET_ROUTES', rewriteRoutes) |
|
45 |
commit('SET_SIDEBAR_ROUTERS', constantRoutes.concat(sidebarRoutes)) |
|
46 |
commit('SET_DEFAULT_ROUTES', sidebarRoutes) |
|
47 |
commit('SET_TOPBAR_ROUTES', sidebarRoutes) |
|
48 |
resolve(rewriteRoutes) |
|
49 |
}) |
|
50 |
}) |
|
51 |
} |
|
52 |
} |
|
53 |
} |
|
54 |
|
|
55 |
// 遍历后台传来的路由字符串,转换为组件对象 |
|
56 |
function filterAsyncRouter(asyncRouterMap, lastRouter = false, type = false) { |
|
57 |
return asyncRouterMap.filter(route => { |
|
58 |
if (type && route.children) { |
|
59 |
route.children = filterChildren(route.children) |
|
60 |
} |
|
61 |
if (route.component) { |
|
62 |
// Layout ParentView 组件特殊处理 |
|
63 |
if (route.component === 'Layout') { |
|
64 |
route.component = Layout |
|
65 |
} else if (route.component === 'ParentView') { |
|
66 |
route.component = ParentView |
|
67 |
} else if (route.component === 'InnerLink') { |
|
68 |
route.component = InnerLink |
|
69 |
} else { |
|
70 |
route.component = loadView(route.component) |
|
71 |
} |
|
72 |
} |
|
73 |
if (route.children != null && route.children && route.children.length) { |
|
74 |
route.children = filterAsyncRouter(route.children, route, type) |
|
75 |
} else { |
|
76 |
delete route['children'] |
|
77 |
delete route['redirect'] |
|
78 |
} |
|
79 |
return true |
|
80 |
}) |
|
81 |
} |
|
82 |
|
|
83 |
function filterChildren(childrenMap, lastRouter = false) { |
|
84 |
var children = [] |
|
85 |
childrenMap.forEach((el, index) => { |
|
86 |
if (el.children && el.children.length) { |
|
87 |
if (el.component === 'ParentView' && !lastRouter) { |
|
88 |
el.children.forEach(c => { |
|
89 |
c.path = el.path + '/' + c.path |
|
90 |
if (c.children && c.children.length) { |
|
91 |
children = children.concat(filterChildren(c.children, c)) |
|
92 |
return |
|
93 |
} |
|
94 |
children.push(c) |
|
95 |
}) |
|
96 |
return |
|
97 |
} |
|
98 |
} |
|
99 |
if (lastRouter) { |
|
100 |
el.path = lastRouter.path + '/' + el.path |
|
101 |
if (el.children && el.children.length) { |
|
102 |
children = children.concat(filterChildren(el.children, el)) |
|
103 |
return |
|
104 |
} |
|
105 |
} |
|
106 |
children = children.concat(el) |
|
107 |
}) |
|
108 |
return children |
|
109 |
} |
|
110 |
|
|
111 |
// 动态路由遍历,验证是否具备权限 |
|
112 |
export function filterDynamicRoutes(routes) { |
|
113 |
const res = [] |
|
114 |
routes.forEach(route => { |
|
115 |
if (route.permissions) { |
|
116 |
if (auth.hasPermiOr(route.permissions)) { |
|
117 |
res.push(route) |
|
118 |
} |
|
119 |
} else if (route.roles) { |
|
120 |
if (auth.hasRoleOr(route.roles)) { |
|
121 |
res.push(route) |
|
122 |
} |
|
123 |
} |
|
124 |
}) |
|
125 |
return res |
|
126 |
} |
|
127 |
|
|
128 |
export const loadView = (view) => { |
|
129 |
if (process.env.NODE_ENV === 'development') { |
|
130 |
return (resolve) => require([`@/views/${view}`], resolve) |
|
131 |
} else { |
|
132 |
// 使用 import 实现生产环境的路由懒加载 |
|
133 |
return () => import(`@/views/${view}`) |
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 |
export default permission |