route.ts
10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import { cloneDeep } from 'lodash-es'
import type { RouteMeta, RouteRecordRaw } from 'vue-router'
import useSettingsStore from './settings'
import useUserStore from './user'
import { resolveRoutePath } from '@/utils'
import { getImageContent } from "@/api/modules/queryService";
import {createSVG } from "@/utils/common"
import { systemRoutes } from '@/router/routes'
import apiApp from '@/api/modules/app'
import type { Route } from '#/global'
import { getDownloadUrl, download } from "@/utils/common";
const useRouteStore = defineStore(
// 唯一ID
'route',
() => {
const settingsStore = useSettingsStore()
const userStore = useUserStore()
const isGenerate = ref(false)
const routesRaw = ref<Route.recordMainRaw[]>([])
const filesystemRoutesRaw = ref<RouteRecordRaw[]>([])
const currentRemoveRoutes = ref<Function[]>([])
// 将多层嵌套路由处理成两层,保留顶层和最子层路由,中间层级将被拍平
function flatAsyncRoutes<T extends RouteRecordRaw>(route: T): T {
if (route.children) {
route.children = flatAsyncRoutesRecursive(route.children, [{
path: route.path,
hide: !route.meta?.breadcrumb && route.meta?.breadcrumb === false,
}], route.path, route.meta?.auth)
}
return route
}
function flatAsyncRoutesRecursive(routes: RouteRecordRaw[], breadcrumb: Route.breadcrumb[] = [], baseUrl = '', baseAuth: RouteMeta['auth']): RouteRecordRaw[] {
const res: RouteRecordRaw[] = []
routes.forEach((route) => {
if (route.children) {
const childrenBaseUrl = resolveRoutePath(baseUrl, route.path)
const childrenBaseAuth = baseAuth ?? route.meta?.auth
const tmpBreadcrumb = cloneDeep(breadcrumb)
tmpBreadcrumb.push({
path: childrenBaseUrl,
hide: !route.meta?.breadcrumb && route.meta?.breadcrumb === false,
})
const tmpRoute = cloneDeep(route)
tmpRoute.path = childrenBaseUrl
if (!tmpRoute.meta) {
tmpRoute.meta = {}
}
tmpRoute.meta.auth = childrenBaseAuth
tmpRoute.meta.breadcrumbNeste = tmpBreadcrumb
delete tmpRoute.children
res.push(tmpRoute)
const childrenRoutes = flatAsyncRoutesRecursive(route.children, tmpBreadcrumb, childrenBaseUrl, childrenBaseAuth)
childrenRoutes.forEach((item) => {
// 如果 path 一样则覆盖,因为子路由的 path 可能设置为空,导致和父路由一样,直接注册会提示路由重复
if (res.some(v => v.path === item.path)) {
res.forEach((v, i) => {
if (v.path === item.path) {
res[i] = item
}
})
}
else {
res.push(item)
}
})
}
else {
const tmpRoute = cloneDeep(route)
tmpRoute.path = resolveRoutePath(baseUrl, tmpRoute.path)
// 处理面包屑导航
const tmpBreadcrumb = cloneDeep(breadcrumb)
tmpBreadcrumb.push({
path: tmpRoute.path,
hide: !tmpRoute.meta?.breadcrumb && tmpRoute.meta?.breadcrumb === false,
})
if (!tmpRoute.meta) {
tmpRoute.meta = {}
}
tmpRoute.meta.auth = baseAuth ?? tmpRoute.meta?.auth
tmpRoute.meta.breadcrumbNeste = tmpBreadcrumb
res.push(tmpRoute)
}
})
return res
}
// 扁平化路由(将三级及以上路由数据拍平成二级)
const flatRoutes = computed(() => {
const settingsStore = useSettingsStore()
const returnRoutes: RouteRecordRaw[] = []
if (routesRaw.value) {
if (settingsStore.settings.app.routeBaseOn !== 'filesystem') {
routesRaw.value.forEach((item) => {
const tmpRoutes = cloneDeep(item.children) as RouteRecordRaw[]
tmpRoutes.map((v) => {
if (!v.meta) {
v.meta = {}
}
v.meta.auth = item.meta?.auth ?? v.meta?.auth
return v
})
returnRoutes.push(...tmpRoutes)
})
returnRoutes.forEach(item => flatAsyncRoutes(item))
}
else {
returnRoutes.push(...cloneDeep(filesystemRoutesRaw.value))
}
}
return returnRoutes
})
const flatSystemRoutes = computed(() => {
const routes = [...systemRoutes]
routes.forEach(item => flatAsyncRoutes(item))
return routes
})
// 判断是否有权限
function hasPermission(permissions: string[], route: Route.recordMainRaw | RouteRecordRaw) {
let isAuth = false
if (route.meta?.auth) {
isAuth = permissions.some((auth) => {
if (typeof route.meta?.auth === 'string') {
return route.meta.auth !== '' ? route.meta.auth === auth : true
}
else if (typeof route.meta?.auth === 'object') {
return route.meta.auth.length > 0 ? route.meta.auth.includes(auth) : true
}
else {
return false
}
})
}
else {
isAuth = true
}
return isAuth
}
// 根据权限过滤路由
function filterAsyncRoutes<T extends Route.recordMainRaw[] | RouteRecordRaw[]>(routes: T, permissions: string[], menuList: any[], parentPath?: string): T {
parentPath = parentPath ?? "";
const res: any = [];
menuList?.forEach( async(m) => {
let r: any = null;
if (m.menuType === 'C') {
if (m.visible === 'N') {
return;
}
let path = m.path;
if (parentPath) {
path = parentPath + path;
} else if (m.productGuid) {
let p = userStore.userInfoData?.find(d => d.guid === m.productGuid);
if (p && p.productRoute) {
path = p.productRoute + p.path;
}
}
r = routes.find((route: any) => route.path === path || route.path === m.path || `/${route.path}` === m.path);
if(r && m.icon && m.icon !== "{}") {
r.meta.title = m.menuName || r.meta.title;
r.meta.icon = m.icon
r.meta.isEle = true
}
} else {
r = routes.find((route: any) => route.meta.title === m.productName);
if(r && m.icon && m.icon !== "{}") {
r.meta.title = m.menuName || r.meta.title;
r.meta.icon = m.icon
r.meta.isEle = true
}
}
if (r && hasPermission(permissions, r)) {
const tmpRoute = cloneDeep(r);
if (m.menuType === 'C') {
tmpRoute.meta.title = m.menuName;
}
if (tmpRoute.children) {
if (m.menuType === 'C') {
tmpRoute.children = tmpRoute.children.filter(child => {
if (!child.path) {
return true;
}
if (child.sidebar === false) {
return true;
}
return filterAsyncRoutes([child], permissions, m.children || [], parentPath + m.path)?.length > 0;
});
} else {
tmpRoute.children = filterAsyncRoutes(tmpRoute.children, permissions, (m.productName ? m.menuList : m.children) || [], m.productRoute ?? "")
}
tmpRoute.children.length && res.push(tmpRoute)
}
else {
res.push(tmpRoute)
}
}
})
return res
}
const routes = computed(() => {
let returnRoutes: Route.recordMainRaw[]
// 如果权限功能开启,则需要对路由数据进行筛选过滤
if (settingsStore.settings.app.enablePermission) {
returnRoutes = filterAsyncRoutes(routesRaw.value as any, userStore.permissions, userStore.userInfoData);
}
else {
returnRoutes = cloneDeep(routesRaw.value) as any
}
return returnRoutes
})
// 根据权限动态生成路由(前端生成)
async function generateRoutesAtFront(asyncRoutes: Route.recordMainRaw[]) {
// 设置 routes 数据
routesRaw.value = cloneDeep(asyncRoutes) as any
if (settingsStore.settings.app.enablePermission) {
await userStore.getPermissions()
await userStore.setTabbarInfo(asyncRoutes)
}
isGenerate.value = true
}
// 格式化后端路由数据
function formatBackRoutes(routes: any, views = import.meta.glob('../../views/**/*.vue')): Route.recordMainRaw[] {
return routes.map((route: any) => {
switch (route.component) {
case 'Layout':
route.component = () => import('@/layouts/index.vue')
break
default:
if (route.component) {
route.component = views[`../../views/${route.component}`]
}
}
if (route.children) {
route.children = formatBackRoutes(route.children, views)
}
return route
})
}
// 根据权限动态生成路由(后端获取)
async function generateRoutesAtBack() {
await apiApp.routeList().then(async (res) => {
// 设置 routes 数据
routesRaw.value = formatBackRoutes(res.data)
if (settingsStore.settings.app.enablePermission) {
userStore.getPermissions()
}
isGenerate.value = true
}).catch(() => {})
}
// 根据权限动态生成路由(文件系统生成)
async function generateRoutesAtFilesystem(asyncRoutes: RouteRecordRaw[]) {
// 设置 routes 数据
filesystemRoutesRaw.value = cloneDeep(asyncRoutes) as any
// 如果权限功能开启,则需要对路由数据进行筛选过滤
if (settingsStore.settings.app.enablePermission) {
await userStore.getPermissions()
}
isGenerate.value = true
}
// 记录 accessRoutes 路由,用于登出时删除路由
function setCurrentRemoveRoutes(routes: Function[]) {
currentRemoveRoutes.value = routes
}
// 清空动态路由
function removeRoutes() {
isGenerate.value = false
routesRaw.value = []
filesystemRoutesRaw.value = []
currentRemoveRoutes.value.forEach((removeRoute) => {
removeRoute()
})
currentRemoveRoutes.value = []
}
return {
isGenerate,
routes,
currentRemoveRoutes,
flatRoutes,
flatSystemRoutes,
generateRoutesAtFront,
generateRoutesAtBack,
generateRoutesAtFilesystem,
setCurrentRemoveRoutes,
removeRoutes,
}
},
)
export default useRouteStore