1
- import { emitter } from '@sa/hooks' ;
2
- import type { RouteObject , RouterNavigateOptions , To } from 'react-router-dom' ;
1
+ import type { RouterNavigateOptions , To } from 'react-router-dom' ;
3
2
import { createBrowserRouter , matchRoutes } from 'react-router-dom' ;
4
3
5
- import { authRoutes , routes } from '@/router' ;
4
+ import { routes } from '@/router' ;
5
+ import { store } from '@/store' ;
6
6
7
- import { filterRoutesToMenus } from '../menu/MenuUtil ' ;
7
+ import { getIsLogin } from '../auth/authStore ' ;
8
8
9
- import { mergeValuesByParent } from './shared ' ;
9
+ import { initAuthRoutes } from './routerHooks ' ;
10
10
11
- export function navigator ( ) {
11
+ function initRouter ( ) {
12
12
const authRouteMode = import . meta. env . VITE_AUTH_ROUTE_MODE ;
13
13
14
14
const isStaticSuper = true ;
@@ -23,35 +23,39 @@ export function navigator() {
23
23
if ( ! matchRoute ) return true ;
24
24
25
25
if ( matchRoute ) {
26
- return matchRoute [ 0 ] . route . path === '*' ;
26
+ return matchRoute [ 1 ] . route . path === '*' ;
27
27
}
28
+
28
29
return false ;
29
30
}
30
31
31
- const reactAuthRoutes = mergeValuesByParent ( authRoutes ) . reverse ( ) ;
32
-
33
32
const reactRouter = createBrowserRouter ( routes , {
34
33
basename : import . meta. env . VITE_BASE_URL ,
35
34
patchRoutesOnNavigation : ( { patch, path } ) => {
36
35
if ( getIsNeedPatch ( path ) ) {
37
36
if ( authRouteMode === 'static' ) {
38
37
// 超级管理员
39
38
if ( isStaticSuper ) {
40
- reactAuthRoutes . forEach ( route => {
41
- if ( route . parent ?. includes ( 'base' ) ) {
42
- emitter . emit ( 'ADD_MENUS' , filterRoutesToMenus ( route . route ) ) ;
43
- }
44
-
45
- patch ( route . parent , route . route ) ;
46
- } ) ;
39
+ initAuthRoutes ( patch ) ;
47
40
}
48
41
}
49
-
50
42
isAlreadyPatch = true ;
51
43
}
52
44
}
53
45
} ) ;
54
46
47
+ if ( getIsLogin ( store . getState ( ) ) ) {
48
+ initAuthRoutes ( reactRouter . patchRoutes ) ;
49
+
50
+ isAlreadyPatch = true ;
51
+ }
52
+
53
+ return reactRouter ;
54
+ }
55
+
56
+ export function navigator ( ) {
57
+ const reactRouter = initRouter ( ) ;
58
+
55
59
async function navigate ( path : To | null , options ?: RouterNavigateOptions ) {
56
60
reactRouter . navigate ( path , options ) ;
57
61
}
@@ -84,12 +88,7 @@ export function navigator() {
84
88
reactRouter . navigate ( '/' ) ;
85
89
}
86
90
87
- function addRoutes ( newRoutes : RouteObject [ ] , parent : string | null = null ) {
88
- reactRouter . patchRoutes ( parent , newRoutes ) ;
89
- }
90
-
91
91
return {
92
- addRoutes,
93
92
back,
94
93
forward,
95
94
go,
0 commit comments