1
1
import { cloneDeep } from 'lodash-es'
2
+ import type { RouteRecordRaw } from 'vue-router'
2
3
import useSettingsStore from './settings'
3
4
import useUserStore from './user'
4
5
import api from '@/api'
5
6
import { resolveRoutePath } from '@/utils'
6
7
import { systemRoutes } from '@/router/routes'
7
8
import type { Route } from '@/global'
8
9
9
- function hasPermission ( permissions : string [ ] , route : Route . recordMainRaw | Route . recordRaw ) {
10
+ function hasPermission ( permissions : string [ ] , route : Route . recordMainRaw | RouteRecordRaw ) {
10
11
let isAuth = false
11
12
if ( route . meta ?. auth ) {
12
13
isAuth = permissions . some ( ( auth ) => {
13
- return typeof route . meta . auth === 'string'
14
+ return typeof route . meta ? .auth === 'string'
14
15
? route . meta . auth === auth
15
- : typeof route . meta . auth === 'object'
16
+ : typeof route . meta ? .auth === 'object'
16
17
? route . meta . auth . includes ( auth )
17
18
: false
18
19
} )
@@ -23,7 +24,7 @@ function hasPermission(permissions: string[], route: Route.recordMainRaw | Route
23
24
return isAuth
24
25
}
25
26
26
- function filterAsyncRoutes < T extends Route . recordMainRaw [ ] | Route . recordRaw [ ] > ( routes : T , permissions : string [ ] ) : T {
27
+ function filterAsyncRoutes < T extends Route . recordMainRaw [ ] | RouteRecordRaw [ ] > ( routes : T , permissions : string [ ] ) : T {
27
28
const res : any = [ ]
28
29
routes . forEach ( ( route ) => {
29
30
if ( hasPermission ( permissions , route ) ) {
@@ -59,31 +60,34 @@ function formatBackRoutes(routes: any, views = import.meta.glob('../../views/**/
59
60
}
60
61
61
62
// 将多层嵌套路由处理成两层,保留顶层和最子层路由,中间层级将被拍平
62
- function flatAsyncRoutes < T extends Route . recordRaw > ( routes : T ) : T {
63
+ function flatAsyncRoutes < T extends RouteRecordRaw > ( routes : T ) : T {
63
64
if ( routes . children ) {
64
65
routes . children = flatAsyncRoutesRecursive ( routes . children , [ {
65
66
path : routes . path ,
66
- title : routes . meta . title ,
67
- hide : ! routes . meta . breadcrumb && routes . meta . breadcrumb === false ,
67
+ title : routes . meta ? .title ,
68
+ hide : ! routes . meta ? .breadcrumb && routes . meta ? .breadcrumb === false ,
68
69
} ] , routes . path )
69
70
}
70
71
return routes
71
72
}
72
- function flatAsyncRoutesRecursive ( routes : Route . recordRaw [ ] , breadcrumb : Route . breadcrumb [ ] = [ ] , baseUrl = '' ) : Route . recordRaw [ ] {
73
- const res : Route . recordRaw [ ] = [ ]
73
+ function flatAsyncRoutesRecursive ( routes : RouteRecordRaw [ ] , breadcrumb : Route . breadcrumb [ ] = [ ] , baseUrl = '' ) : RouteRecordRaw [ ] {
74
+ const res : RouteRecordRaw [ ] = [ ]
74
75
routes . forEach ( ( route ) => {
75
76
if ( route . children ) {
76
77
const childrenBaseUrl = resolveRoutePath ( baseUrl , route . path )
77
78
const tmpBreadcrumb = cloneDeep ( breadcrumb )
78
- if ( route . meta . breadcrumb !== false ) {
79
+ if ( route . meta ? .breadcrumb !== false ) {
79
80
tmpBreadcrumb . push ( {
80
81
path : childrenBaseUrl ,
81
- title : route . meta . title ,
82
- hide : ! route . meta . breadcrumb && route . meta . breadcrumb === false ,
82
+ title : route . meta ? .title ,
83
+ hide : ! route . meta ? .breadcrumb ,
83
84
} )
84
85
}
85
86
const tmpRoute = cloneDeep ( route )
86
87
tmpRoute . path = childrenBaseUrl
88
+ if ( ! tmpRoute . meta ) {
89
+ tmpRoute . meta = { }
90
+ }
87
91
tmpRoute . meta . breadcrumbNeste = tmpBreadcrumb
88
92
delete tmpRoute . children
89
93
res . push ( tmpRoute )
@@ -109,9 +113,12 @@ function flatAsyncRoutesRecursive(routes: Route.recordRaw[], breadcrumb: Route.b
109
113
const tmpBreadcrumb = cloneDeep ( breadcrumb )
110
114
tmpBreadcrumb . push ( {
111
115
path : tmpRoute . path ,
112
- title : tmpRoute . meta . title ,
113
- hide : ! tmpRoute . meta . breadcrumb && tmpRoute . meta . breadcrumb === false ,
116
+ title : tmpRoute . meta ? .title ,
117
+ hide : ! tmpRoute . meta ? .breadcrumb && tmpRoute . meta ? .breadcrumb === false ,
114
118
} )
119
+ if ( ! tmpRoute . meta ) {
120
+ tmpRoute . meta = { }
121
+ }
115
122
tmpRoute . meta . breadcrumbNeste = tmpBreadcrumb
116
123
res . push ( tmpRoute )
117
124
}
@@ -126,14 +133,14 @@ const useRouteStore = defineStore(
126
133
state : ( ) => ( {
127
134
isGenerate : false ,
128
135
routes : [ ] as Route . recordMainRaw [ ] ,
129
- fileSystemRoutes : [ ] as Route . recordRaw [ ] ,
136
+ fileSystemRoutes : [ ] as RouteRecordRaw [ ] ,
130
137
currentRemoveRoutes : [ ] as Function [ ] ,
131
138
} ) ,
132
139
getters : {
133
140
// 扁平化路由(将三级及以上路由数据拍平成二级)
134
141
flatRoutes : ( state ) => {
135
142
const settingsStore = useSettingsStore ( )
136
- const routes : Route . recordRaw [ ] = [ ]
143
+ const routes : RouteRecordRaw [ ] = [ ]
137
144
if ( state . routes ) {
138
145
if ( settingsStore . app . routeBaseOn !== 'filesystem' ) {
139
146
state . routes . forEach ( ( item ) => {
@@ -171,7 +178,7 @@ const useRouteStore = defineStore(
171
178
}
172
179
// 设置 routes 数据
173
180
this . isGenerate = true
174
- this . routes = accessedRoutes . filter ( item => item . children ?. length !== 0 )
181
+ this . routes = accessedRoutes . filter ( item => item . children ?. length !== 0 ) as any
175
182
resolve ( )
176
183
} )
177
184
} ,
@@ -195,13 +202,13 @@ const useRouteStore = defineStore(
195
202
}
196
203
// 设置 routes 数据
197
204
this . isGenerate = true
198
- this . routes = accessedRoutes . filter ( item => item . children . length !== 0 )
205
+ this . routes = accessedRoutes . filter ( item => item . children . length !== 0 ) as any
199
206
resolve ( )
200
207
} )
201
208
} )
202
209
} ,
203
210
// 根据权限动态生成路由(文件系统生成)
204
- generateRoutesAtFilesystem ( asyncRoutes : Route . recordRaw [ ] ) {
211
+ generateRoutesAtFilesystem ( asyncRoutes : RouteRecordRaw [ ] ) {
205
212
// eslint-disable-next-line no-async-promise-executor
206
213
return new Promise < void > ( async ( resolve ) => {
207
214
const settingsStore = useSettingsStore ( )
@@ -217,7 +224,7 @@ const useRouteStore = defineStore(
217
224
}
218
225
// 设置 routes 数据
219
226
this . isGenerate = true
220
- this . fileSystemRoutes = accessedRoutes . filter ( item => item . children ?. length !== 0 )
227
+ this . fileSystemRoutes = accessedRoutes . filter ( item => item . children ?. length !== 0 ) as any
221
228
resolve ( )
222
229
} )
223
230
} ,
0 commit comments