Skip to content

Commit fa87c12

Browse files
committed
feat: 对接动态路由
1 parent 8033103 commit fa87c12

File tree

4 files changed

+17
-7
lines changed

4 files changed

+17
-7
lines changed

src/features/router/initRouter.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import type { RouteObject } from 'react-router-dom';
22

33
import { authRoutes } from '@/router';
4+
import { fetchGetUserRoutes } from '@/service/api';
45
import { store } from '@/store';
56

67
import { isStaticSuper, selectUserInfo } from '../auth/authStore';
78

9+
import { setHomePath } from './routeStore';
810
import { filterAuthRoutesByDynamic, filterAuthRoutesByRoles, mergeValuesByParent } from './shared';
911

10-
const hasRoutes = ['/manage', '/manage/user', '/manage/user/:id', '/home', '/about'];
11-
12-
export function initAuthRoutes(addRoutes: (parent: string | null, route: RouteObject[]) => void) {
12+
export async function initAuthRoutes(addRoutes: (parent: string | null, route: RouteObject[]) => void) {
1313
const authRouteMode = import.meta.env.VITE_AUTH_ROUTE_MODE;
1414

1515
const reactAuthRoutes = mergeValuesByParent(authRoutes);
@@ -35,7 +35,14 @@ export function initAuthRoutes(addRoutes: (parent: string | null, route: RouteOb
3535
}
3636
} else {
3737
// 动态模式
38-
const filteredRoutes = filterAuthRoutesByDynamic(reactAuthRoutes, hasRoutes);
38+
const { data, error } = await fetchGetUserRoutes();
39+
if (error) {
40+
console.error(error);
41+
return;
42+
}
43+
store.dispatch(setHomePath(data.home));
44+
45+
const filteredRoutes = filterAuthRoutesByDynamic(reactAuthRoutes, data.routes);
3946

4047
filteredRoutes.forEach(({ parent, route }) => {
4148
addRoutes(parent, route);

src/features/router/routeStore.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ export const routeSlice = createAppSlice({
2828
setCacheRoutes: create.reducer((state, { payload }: PayloadAction<string[]>) => {
2929
state.cacheRoutes = payload;
3030
}),
31+
setHomePath: create.reducer((state, { payload }: PayloadAction<string>) => {
32+
state.routeHomePath = payload;
33+
}),
3134
setRemoveCacheKey: create.reducer((state, { payload }: PayloadAction<string | null>) => {
3235
state.removeCacheKey = payload;
3336
})
@@ -39,6 +42,6 @@ export const routeSlice = createAppSlice({
3942
}
4043
});
4144

42-
export const { addCacheRoutes, resetRouteStore, setCacheRoutes, setRemoveCacheKey } = routeSlice.actions;
45+
export const { addCacheRoutes, resetRouteStore, setCacheRoutes, setHomePath, setRemoveCacheKey } = routeSlice.actions;
4346

4447
export const { selectCacheRoutes, selectRemoveCacheKey, selectRouteHomePath } = routeSlice.selectors;

src/service/api/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export function fetchGetConstantRoutes() {
77

88
/** get user routes */
99
export function fetchGetUserRoutes() {
10-
return request<Api.Route.UserRoute>({ url: '/route/getUserRoutes' });
10+
return request<Api.Route.UserRoute>({ url: '/route/getReactUserRoutes' });
1111
}
1212

1313
/**

src/types/api.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ declare namespace Api {
8585

8686
interface UserRoute {
8787
home: import('@soybean-react/vite-plugin-react-router').LastLevelRouteKey;
88-
routes: MenuRoute[];
88+
routes: string[];
8989
}
9090
}
9191

0 commit comments

Comments
 (0)