Skip to content

Commit befbb35

Browse files
committed
feat: 添加菜单进行排序
1 parent 0a91d7f commit befbb35

File tree

11 files changed

+68
-53
lines changed

11 files changed

+68
-53
lines changed

src/features/menu/MenuUtil.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ import { $t } from '@/locales';
99
* @param routes Auth routes
1010
*/
1111
export function filterRoutesToMenus(routes: RouteObject[]) {
12+
// 先根据 handle?.order 对路由做排序
13+
const sortedRoutes = sortRoutesByOrder(routes);
14+
1215
const menus: App.Global.Menu[] = [];
1316

14-
for (const route of routes) {
17+
for (const route of sortedRoutes) {
1518
// 如果节点存在 path(注意:这里假设空字符串或 undefined 均视为无 path)
1619

1720
if (route.path && !route.handle?.hideInMenu) {
@@ -36,6 +39,32 @@ export function filterRoutesToMenus(routes: RouteObject[]) {
3639
return menus;
3740
}
3841

42+
/**
43+
* sort routes by order
44+
*
45+
* @param routes routes
46+
*/
47+
export function sortRoutesByOrder(routes: RouteObject[]) {
48+
routes.sort((next, prev) => (Number(next.handle?.order) || 0) - (Number(prev.handle?.order) || 0));
49+
routes.forEach(sortRouteByOrder);
50+
51+
return routes;
52+
}
53+
54+
/**
55+
* sort route by order
56+
*
57+
* @param route route
58+
*/
59+
function sortRouteByOrder(route: RouteObject) {
60+
if (route.children?.length) {
61+
route.children.sort((next, prev) => (Number(next.handle?.order) || 0) - (Number(prev.handle?.order) || 0));
62+
route.children.forEach(sortRouteByOrder);
63+
}
64+
65+
return route;
66+
}
67+
3968
/**
4069
* Get global menu by route
4170
*

src/features/router/routerHooks.ts

Lines changed: 27 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,43 @@
1-
import { emitter } from '@sa/hooks';
1+
import type { RouteObject } from 'react-router-dom';
22

33
import { authRoutes } from '@/router';
4+
import { store } from '@/store';
45

5-
import { filterRoutesToMenus } from '../menu/MenuUtil';
6+
import { isStaticSuper, selectUserInfo } from '../auth/authStore';
67

7-
import type { RouterContextType } from './router';
88
import { filterAuthRoutesByRoles, mergeValuesByParent } from './shared';
99

10-
export function useInitAuthRoutes() {
10+
export function initAuthRoutes(addRoutes: (parent: string | null, route: RouteObject[]) => void) {
1111
const authRouteMode = import.meta.env.VITE_AUTH_ROUTE_MODE;
1212

1313
const reactAuthRoutes = mergeValuesByParent(authRoutes).reverse();
1414

15-
function initAuthRoutes(isStaticSuper: boolean, roles: string[], addRoutes: RouterContextType['addRoutes']) {
16-
// 静态模式
17-
if (authRouteMode === 'static') {
18-
// 超级管理员
19-
if (isStaticSuper) {
20-
reactAuthRoutes.forEach(route => {
21-
if (route.parent?.includes('base')) {
22-
emitter.emit('ADD_MENUS', filterRoutesToMenus(route.route));
23-
24-
console.log('route.route', route.route);
25-
}
26-
27-
addRoutes(route.route, route.parent);
28-
});
29-
} else {
30-
// 非超级管理员
31-
const filteredRoutes = filterAuthRoutesByRoles(reactAuthRoutes, roles);
32-
filteredRoutes.forEach((route, index) => {
33-
addRoutes(route, reactAuthRoutes[index].parent);
34-
});
35-
}
15+
const isSuper = isStaticSuper(store.getState());
16+
17+
const { roles } = selectUserInfo(store.getState());
18+
19+
// 静态模式
20+
if (authRouteMode === 'static') {
21+
// 超级管理员
22+
if (isSuper) {
23+
reactAuthRoutes.forEach(route => {
24+
addRoutes(route.parent, route.route);
25+
});
3626
} else {
37-
// 动态模式
38-
// await dispatch(initDynamicAuthRoute());
27+
// 非超级管理员
28+
const filteredRoutes = filterAuthRoutesByRoles(reactAuthRoutes, roles);
29+
filteredRoutes.forEach((route, index) => {
30+
addRoutes(reactAuthRoutes[index].parent, route);
31+
});
3932
}
33+
} else {
34+
// 动态模式
35+
// await dispatch(initDynamicAuthRoute());
36+
}
4037

41-
// const routeHomeName = getRouteHome(getState());
42-
43-
// const homeRoute = router.getRouteByName(routeHomeName);
38+
// const routeHomeName = getRouteHome(getState());
4439

45-
// if (homeRoute) dispatch(initHomeTab({ homeRouteName: routeHomeName as LastLevelRouteKey, route: homeRoute }));
46-
}
40+
// const homeRoute = router.getRouteByName(routeHomeName);
4741

48-
return initAuthRoutes;
42+
// if (homeRoute) dispatch(initHomeTab({ homeRouteName: routeHomeName as LastLevelRouteKey, route: homeRoute }));
4943
}

src/features/router/shared.ts

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

3-
import type { AuthRoute } from '@/router/routes';
4-
53
export function filterCacheRoutes(routes: RouteObject[]) {
64
const cacheRoutes: string[] = [];
75

@@ -32,8 +30,8 @@ export function filterCacheRoutes(routes: RouteObject[]) {
3230
* @param data Auth routes
3331
* @returns Merged routes
3432
*/
35-
export function mergeValuesByParent(data: AuthRoute[]) {
36-
const merged: Record<string, { parent: string | null; route: RouteObject[] }> = {};
33+
export function mergeValuesByParent(data: Router.SingleAuthRoute[]) {
34+
const merged: Record<string, Router.AuthRoute> = {};
3735
data.forEach(item => {
3836
// 使用一个变量作为 key,若 parent 为 null,则转换为字符串 "null"
3937
const key = item.parent === null ? 'null' : item.parent;

src/features/tab/GlobalTab.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useTheme } from '@/features/theme';
66
import { getFullContent, toggleFullContent } from '@/layouts/appStore';
77
import { isPC } from '@/utils/agent';
88

9-
import { setRemoveCacheKey } from '../router';
9+
import { setRemoveCacheKey } from '../router/routeStore';
1010

1111
import ContextMenu from './TabContextMenu';
1212
import TabReloadButton from './TabReloadButton';

src/layouts/modules/GlobalContent.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import clsx from 'clsx';
22
import KeepAlive, { useKeepAliveRef } from 'keepalive-for-react';
33

4-
import { selectCacheRoutes, selectRemoveCacheKey } from '@/features/router';
4+
import { selectCacheRoutes, selectRemoveCacheKey } from '@/features/router/routeStore';
55
import { getReloadFlag } from '@/layouts/appStore';
66
import './transition.css';
77

src/pages/(base)/home/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const Home = () => {
1313
size={[16, 16]}
1414
>
1515
<HeaderBanner />
16+
1617
<CardData />
1718

1819
<ARow gutter={[16, 16]}>
@@ -29,7 +30,6 @@ const Home = () => {
2930
<PieChart />
3031
</ACol>
3132
</ARow>
32-
3333
<ARow gutter={[16, 16]}>
3434
<ACol
3535
lg={14}

src/pages/(base)/manage/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const Manage = () => {
77
export const handle = {
88
constant: true,
99
i18nKey: 'route.manage',
10-
order: 2,
10+
order: 3,
1111
title: 'manage'
1212
};
1313

src/pages/(base)/multi-menu/index.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { redirect } from 'react-router-dom';
33
export const handle = {
44
constant: true,
55
i18nKey: 'route.multi-menu',
6+
order: 5,
67
title: 'multi-menu'
78
};
89

src/router/routes/builtin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export const BaseChildrenRoutes = [
5555
handle: {
5656
i18nKey: 'route.exception',
5757
icon: 'ant-design:exception-outlined',
58-
order: 7,
58+
order: 4,
5959
title: 'exception'
6060
},
6161
id: '(base)_exception',

src/router/routes/index.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ import { transformElegantRoutesToReactRoutes } from '../elegant/transform';
66

77
import { BaseChildrenRoutes } from './builtin';
88

9-
export type AuthRoute = {
10-
parent: string | null;
11-
route: RouteObject;
12-
};
13-
149
/**
1510
* Get auth react routes
1611
*
@@ -32,7 +27,7 @@ function getReactRoutes(route: ElegantConstRoute[]) {
3227
function filterRoutes(
3328
routes: RouteObject[],
3429
parent: string | null = null,
35-
authRoutes: AuthRoute[] = [],
30+
authRoutes: Router.SingleAuthRoute[] = [],
3631
cacheRoutes: string[] = []
3732
) {
3833
return routes.reduce((acc, route) => {
@@ -109,7 +104,7 @@ function initRoutes() {
109104
// 添加自定义复用路由至基础路由
110105
baseRoute?.children?.push(...BaseChildrenRoutes);
111106

112-
const authRoutes: AuthRoute[] = [];
107+
const authRoutes: Router.SingleAuthRoute[] = [];
113108

114109
const cacheRoutes: string[] = [];
115110

@@ -119,5 +114,3 @@ function initRoutes() {
119114
}
120115

121116
export const { authRoutes, cacheRoutes, constantRoutes } = initRoutes();
122-
123-
console.log('authRoutes, cacheRoutes, constantRoutes', authRoutes, cacheRoutes, constantRoutes);

src/store/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { combineSlices, configureStore } from '@reduxjs/toolkit';
22
import type { Action, ThunkAction } from '@reduxjs/toolkit';
33

44
import { authSlice } from '../features/auth/authStore';
5-
import { routeSlice } from '../features/router';
5+
import { routeSlice } from '../features/router/routeStore';
66
import { tabSlice } from '../features/tab/tabStore';
77
import { themeSlice } from '../features/theme';
88
import { appSlice } from '../layouts/appStore';

0 commit comments

Comments
 (0)