11import type { Route } from '#/global'
2- import type { RouteRecordRaw } from 'vue-router'
2+ import type { RouteRecordRaw , RouterMatcher } from 'vue-router'
33import apiApp from '@/api/modules/app'
4- import { systemRoutes } from '@/router/routes'
5- import { resolveRoutePath } from '@/utils'
4+ import { systemRoutes as systemRoutesRaw } from '@/router/routes'
65import { cloneDeep } from 'es-toolkit'
6+ import { createRouterMatcher } from 'vue-router'
77import useSettingsStore from './settings'
88
99const useRouteStore = defineStore (
@@ -13,79 +13,15 @@ const useRouteStore = defineStore(
1313 const settingsStore = useSettingsStore ( )
1414
1515 const isGenerate = ref ( false )
16+ // 原始路由
1617 const routesRaw = ref < Route . recordMainRaw [ ] > ( [ ] )
18+ // 文件系统原始路由
1719 const filesystemRoutesRaw = ref < RouteRecordRaw [ ] > ( [ ] )
20+ // 已注册的路由,用于登出时删除路由
1821 const currentRemoveRoutes = ref < ( ( ) => void ) [ ] > ( [ ] )
1922
20- // 将多层嵌套路由处理成两层,保留顶层和最子层路由,中间层级将被拍平
21- function flatAsyncRoutes < T extends RouteRecordRaw > ( route : T ) : T {
22- if ( route . children ) {
23- route . children = flatAsyncRoutesRecursive ( route . children , [ {
24- path : route . path ,
25- title : route . meta ?. title ,
26- icon : route . meta ?. icon ,
27- hide : ! route . meta ?. breadcrumb && route . meta ?. breadcrumb === false ,
28- } ] , route . path )
29- }
30- return route
31- }
32- function flatAsyncRoutesRecursive ( routes : RouteRecordRaw [ ] , breadcrumb : Route . breadcrumb [ ] = [ ] , baseUrl = '' ) : RouteRecordRaw [ ] {
33- const res : RouteRecordRaw [ ] = [ ]
34- routes . forEach ( ( route ) => {
35- if ( route . children ) {
36- const childrenBaseUrl = resolveRoutePath ( baseUrl , route . path )
37- const tmpBreadcrumb = cloneDeep ( breadcrumb )
38- tmpBreadcrumb . push ( {
39- path : childrenBaseUrl ,
40- title : route . meta ?. title ,
41- icon : route . meta ?. icon ,
42- hide : ! route . meta ?. breadcrumb && route . meta ?. breadcrumb === false ,
43- } )
44- const tmpRoute = cloneDeep ( route )
45- tmpRoute . path = childrenBaseUrl
46- if ( ! tmpRoute . meta ) {
47- tmpRoute . meta = { }
48- }
49- tmpRoute . meta . breadcrumbNeste = tmpBreadcrumb
50- delete tmpRoute . children
51- res . push ( tmpRoute )
52- const childrenRoutes = flatAsyncRoutesRecursive ( route . children , tmpBreadcrumb , childrenBaseUrl )
53- childrenRoutes . forEach ( ( item ) => {
54- // 如果 path 一样则覆盖,因为子路由的 path 可能设置为空,导致和父路由一样,直接注册会提示路由重复
55- if ( res . some ( v => v . path === item . path ) ) {
56- res . forEach ( ( v , i ) => {
57- if ( v . path === item . path ) {
58- res [ i ] = item
59- }
60- } )
61- }
62- else {
63- res . push ( item )
64- }
65- } )
66- }
67- else {
68- const tmpRoute = cloneDeep ( route )
69- tmpRoute . path = resolveRoutePath ( baseUrl , tmpRoute . path )
70- // 处理面包屑导航
71- const tmpBreadcrumb = cloneDeep ( breadcrumb )
72- tmpBreadcrumb . push ( {
73- path : tmpRoute . path ,
74- title : tmpRoute . meta ?. title ,
75- icon : tmpRoute . meta ?. icon ,
76- hide : ! tmpRoute . meta ?. breadcrumb && tmpRoute . meta ?. breadcrumb === false ,
77- } )
78- if ( ! tmpRoute . meta ) {
79- tmpRoute . meta = { }
80- }
81- tmpRoute . meta . breadcrumbNeste = tmpBreadcrumb
82- res . push ( tmpRoute )
83- }
84- } )
85- return res
86- }
87- // 扁平化路由(将三级及以上路由数据拍平成二级)
88- const flatRoutes = computed ( ( ) => {
23+ // 实际路由
24+ const routes = computed ( ( ) => {
8925 const returnRoutes : RouteRecordRaw [ ] = [ ]
9026 if ( settingsStore . settings . app . routeBaseOn !== 'filesystem' ) {
9127 if ( routesRaw . value ) {
@@ -100,24 +36,61 @@ const useRouteStore = defineStore(
10036 } )
10137 returnRoutes . push ( ...tmpRoutes )
10238 } )
103- returnRoutes . forEach ( item => flatAsyncRoutes ( item ) )
39+ returnRoutes . forEach ( ( item ) => {
40+ if ( item . children ) {
41+ item . children = deleteMiddleRouteComponent ( item . children )
42+ }
43+ return item
44+ } )
10445 }
10546 }
10647 else {
10748 returnRoutes . push ( ...cloneDeep ( filesystemRoutesRaw . value ) as RouteRecordRaw [ ] )
10849 }
10950 return returnRoutes
11051 } )
111- const flatSystemRoutes = computed ( ( ) => {
112- const routes = [ ...systemRoutes ]
113- routes . forEach ( item => flatAsyncRoutes ( item ) )
52+ // 系统路由
53+ const systemRoutes = computed ( ( ) => {
54+ const routes = [ ...systemRoutesRaw ]
55+ routes . forEach ( ( item ) => {
56+ if ( item . children ) {
57+ item . children = deleteMiddleRouteComponent ( item . children )
58+ }
59+ } )
11460 return routes
11561 } )
62+ // 删除路由中间层级对应的组件
63+ function deleteMiddleRouteComponent ( routes : RouteRecordRaw [ ] ) {
64+ const res : RouteRecordRaw [ ] = [ ]
65+ routes . forEach ( ( route ) => {
66+ if ( route . children ) {
67+ delete route . component
68+ route . children = deleteMiddleRouteComponent ( route . children )
69+ }
70+ res . push ( route )
71+ } )
72+ return res
73+ }
74+
75+ // 路由匹配器
76+ const routesMatcher = ref < RouterMatcher > ( )
77+ // 根据路径获取匹配的路由
78+ function getRouteMatchedByPath ( path : string ) {
79+ return routesMatcher . value ?. resolve ( { path } , undefined ! ) ?. matched ?? [ ]
80+ }
11681
11782 // 生成路由(前端生成)
11883 function generateRoutesAtFront ( asyncRoutes : Route . recordMainRaw [ ] ) {
11984 // 设置 routes 数据
12085 routesRaw . value = cloneDeep ( asyncRoutes ) as any
86+ // 创建路由匹配器
87+ const routes : RouteRecordRaw [ ] = [ ]
88+ routesRaw . value . forEach ( ( route ) => {
89+ if ( route . children ) {
90+ routes . push ( ...route . children )
91+ }
92+ } )
93+ routesMatcher . value = createRouterMatcher ( routes , { } )
12194 isGenerate . value = true
12295 }
12396 // 格式化后端路由数据
@@ -146,6 +119,14 @@ const useRouteStore = defineStore(
146119 await apiApp . routeList ( ) . then ( ( res ) => {
147120 // 设置 routes 数据
148121 routesRaw . value = formatBackRoutes ( res . data ) as any
122+ // 创建路由匹配器
123+ const routes : RouteRecordRaw [ ] = [ ]
124+ routesRaw . value . forEach ( ( route ) => {
125+ if ( route . children ) {
126+ routes . push ( ...route . children )
127+ }
128+ } )
129+ routesMatcher . value = createRouterMatcher ( routes , { } )
149130 isGenerate . value = true
150131 } ) . catch ( ( ) => { } )
151132 }
@@ -174,8 +155,9 @@ const useRouteStore = defineStore(
174155 isGenerate,
175156 routesRaw,
176157 currentRemoveRoutes,
177- flatRoutes,
178- flatSystemRoutes,
158+ routes,
159+ systemRoutes,
160+ getRouteMatchedByPath,
179161 generateRoutesAtFront,
180162 generateRoutesAtBack,
181163 generateRoutesAtFilesystem,
0 commit comments