Skip to content

Commit 72de51f

Browse files
committed
feat: 完成动态路由的添加
1 parent d33c961 commit 72de51f

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

src/features/auth/initAuth.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
11
import { useLoading } from '@sa/hooks';
22

3-
import { useInitAuthRoutes, useRouter } from '@/features/router';
3+
import { useRouter } from '@/features/router';
44

55
import { login } from './authStore';
66

7-
const { VITE_AUTH_ROUTE_MODE, VITE_STATIC_SUPER_ROLE } = import.meta.env;
8-
97
export function useInitAuth() {
108
const { endLoading, loading, startLoading } = useLoading();
119

1210
const [searchParams] = useSearchParams();
1311

1412
const { t } = useTranslation();
1513

16-
const initAuthRoutes = useInitAuthRoutes();
17-
1814
const dispatch = useAppDispatch();
1915

20-
const { addRoutes, navigate } = useRouter();
16+
const { navigate } = useRouter();
2117

2218
const redirectUrl = searchParams.get('redirect');
2319

@@ -28,10 +24,6 @@ export function useInitAuth() {
2824
const info = res.payload as Api.Auth.Info;
2925

3026
if (info.token) {
31-
const isStaticSuper = VITE_AUTH_ROUTE_MODE === 'static' && info.userInfo.roles.includes(VITE_STATIC_SUPER_ROLE);
32-
33-
initAuthRoutes(isStaticSuper, info.userInfo.roles, addRoutes);
34-
3527
if (redirectUrl && redirect) {
3628
await navigate(redirectUrl);
3729
} else {

src/features/router/router.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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';
32
import { createBrowserRouter, matchRoutes } from 'react-router-dom';
43

5-
import { authRoutes, routes } from '@/router';
4+
import { routes } from '@/router';
5+
import { store } from '@/store';
66

7-
import { filterRoutesToMenus } from '../menu/MenuUtil';
7+
import { getIsLogin } from '../auth/authStore';
88

9-
import { mergeValuesByParent } from './shared';
9+
import { initAuthRoutes } from './routerHooks';
1010

11-
export function navigator() {
11+
function initRouter() {
1212
const authRouteMode = import.meta.env.VITE_AUTH_ROUTE_MODE;
1313

1414
const isStaticSuper = true;
@@ -23,35 +23,39 @@ export function navigator() {
2323
if (!matchRoute) return true;
2424

2525
if (matchRoute) {
26-
return matchRoute[0].route.path === '*';
26+
return matchRoute[1].route.path === '*';
2727
}
28+
2829
return false;
2930
}
3031

31-
const reactAuthRoutes = mergeValuesByParent(authRoutes).reverse();
32-
3332
const reactRouter = createBrowserRouter(routes, {
3433
basename: import.meta.env.VITE_BASE_URL,
3534
patchRoutesOnNavigation: ({ patch, path }) => {
3635
if (getIsNeedPatch(path)) {
3736
if (authRouteMode === 'static') {
3837
// 超级管理员
3938
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);
4740
}
4841
}
49-
5042
isAlreadyPatch = true;
5143
}
5244
}
5345
});
5446

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+
5559
async function navigate(path: To | null, options?: RouterNavigateOptions) {
5660
reactRouter.navigate(path, options);
5761
}
@@ -84,12 +88,7 @@ export function navigator() {
8488
reactRouter.navigate('/');
8589
}
8690

87-
function addRoutes(newRoutes: RouteObject[], parent: string | null = null) {
88-
reactRouter.patchRoutes(parent, newRoutes);
89-
}
90-
9191
return {
92-
addRoutes,
9392
back,
9493
forward,
9594
go,

0 commit comments

Comments
 (0)