Skip to content

Commit bd1bad3

Browse files
committed
add menus
1 parent 31a38e3 commit bd1bad3

File tree

5 files changed

+71
-19
lines changed

5 files changed

+71
-19
lines changed

antd-pro/config/routes.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@ export default [
2929
name: 'welcome',
3030
icon: 'smile',
3131
component: './Welcome',
32+
access: 'hasAuthentication',
3233
},
3334
{
3435
path: '/admin',
3536
name: 'admin',
3637
icon: 'crown',
37-
access: 'isAdmin',
38+
access: 'hasAuthentication',
3839
routes: [
3940
{
4041
path: '/admin',
@@ -52,36 +53,43 @@ export default [
5253
icon: 'table',
5354
path: '/list',
5455
component: './table',
56+
access: 'hasAuthentication',
5557
},
5658
{
5759
name: 'components',
5860
icon: 'appstore',
5961
path: '/components',
62+
access: 'hasAuthentication',
6063
routes: [
6164
{
6265
name: 'parameter',
6366
path: '/components/parameter',
6467
component: './components/parameter',
68+
access: 'hasAuthentication',
6569
},
6670
{
6771
name: 'menu',
6872
path: '/components/menu',
6973
component: './components/menu',
74+
access: 'hasAuthentication',
7075
},
7176
{
7277
path: '/components/api',
7378
name: 'api',
7479
component: './components/api',
80+
access: 'hasAuthentication',
7581
},
7682
{
7783
path: '/components/oss',
7884
name: 'oss',
7985
component: './components/oss',
86+
access: 'hasAuthentication',
8087
},
8188
{
8289
path: '/components/oss_test',
8390
name: 'oss.test',
8491
component: './components/oss/test',
92+
access: 'hasAuthentication',
8593
},
8694
],
8795
},

antd-pro/src/access.ts

+16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,23 @@
44
export default function access(initialState: { currentUser?: API.CurrentUser } | undefined) {
55
const { currentUser } = initialState ?? {};
66
const isAdmin = currentUser && currentUser.authorities?.includes('ROLE_ADMIN');
7+
const authentications = localStorage.getItem('authentications');
78
return {
89
isAdmin: isAdmin === undefined ? false : isAdmin,
10+
hasAuthentication: (element: any) => {
11+
console.log('path:',element.path);
12+
console.log('authentications:',authentications);
13+
if (element.path === '/') {
14+
return true;
15+
}
16+
17+
if (authentications === null || authentications === undefined) {
18+
return false;
19+
}
20+
if (authentications.indexOf(element.path) > -1) {
21+
return true;
22+
}
23+
return false;
24+
},
925
};
1026
}

antd-pro/src/app.tsx

+12-7
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ import Footer from '@/components/Footer';
22
import { Question, SelectLang } from '@/components/RightContent';
33
import type { Settings as LayoutSettings } from '@ant-design/pro-components';
44
import { SettingDrawer } from '@ant-design/pro-components';
5-
import type { RunTimeLayoutConfig } from '@umijs/max';
5+
import type { RunTimeLayoutConfig } from '@umijs/max';
66
import { history } from '@umijs/max';
77
import defaultSettings from '../config/defaultSettings';
88
import { errorConfig } from './requestErrorConfig';
99
import React from 'react';
1010
import { AvatarDropdown, AvatarName } from './components/RightContent/AvatarDropdown';
11-
import { loadLayoutMenus } from './components/Menu';
11+
import { loadLayoutMenus, loadLoayoutMenuAuthentications } from './components/Menu';
12+
import { flushSync } from 'react-dom';
13+
import { menus } from '@/services/api/account'
1214
const loginPath = '/user/login';
1315

1416
/**
@@ -25,7 +27,7 @@ export async function getInitialState(): Promise<{
2527
const localUser = {
2628
avatar: localStorage.getItem('avatar'),
2729
username: localStorage.getItem('username'),
28-
authorities:localStorage.getItem('authorities'),
30+
authorities: localStorage.getItem('authorities'),
2931
};
3032
return localUser;
3133
} catch (error) {
@@ -60,10 +62,13 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
6062
return <AvatarDropdown>{avatarChildren}</AvatarDropdown>;
6163
},
6264
},
63-
menu:{
64-
request: async(params, defaultMenuData) =>{
65-
return await loadLayoutMenus();
66-
}
65+
menu: {
66+
request: async () => {
67+
const response = await menus();
68+
const authentications = await loadLoayoutMenuAuthentications(response);
69+
localStorage.setItem('authentications', JSON.stringify(authentications));
70+
return await loadLayoutMenus(response);
71+
}
6772
},
6873
waterMarkProps: {
6974
content: initialState?.currentUser?.username,
+27-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import { menus } from '@/services/api/account';
32

43
interface MenuProps {
54
icon: string
@@ -13,24 +12,46 @@ const Menus: React.FC<MenuProps> = (props) => {
1312
)
1413
}
1514

16-
export async function loadLayoutMenus() {
17-
const response = await menus();
15+
export async function loadLayoutMenus(response:any) {
1816
if (response.success) {
1917
let childrens = response.data.children;
2018
if (childrens === null) {
2119
return [];
2220
}
23-
const search = (data: any) => {
21+
const fetchMenu = (data: any) => {
2422
data.icon = <Menus icon={data.icon} />;
2523
data.children = data.children || [];
2624
data.children.forEach((item: any) => {
27-
search(item);
25+
fetchMenu(item);
2826
});
2927
return data;
3028
}
31-
childrens = childrens.map(item => search(item));
29+
childrens = childrens.map((item:any) => fetchMenu(item));
3230
return childrens;
3331
} else {
3432
return [];
3533
}
3634
}
35+
36+
export async function loadLoayoutMenuAuthentications(response:any) {
37+
if (response.success) {
38+
let childrens = response.data.children;
39+
if (childrens === null) {
40+
return [];
41+
}
42+
const authorities: string[] = [];
43+
const feathAuthorities = (data: any) => {
44+
authorities.push(data.path);
45+
if (data.children) {
46+
data.children.forEach((item: any) => {
47+
feathAuthorities(item);
48+
});
49+
}
50+
}
51+
childrens.forEach((element:any) => {
52+
feathAuthorities(element);
53+
});
54+
return authorities;
55+
}
56+
return [];
57+
}

antd-pro/src/pages/components/menu/index.tsx

+7-5
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ const MenuPage: React.FC = () => {
3030
}
3131
};
3232

33+
const reloadTree = () => {
34+
tree().then(res => {
35+
setTreeData([res.data]);
36+
})
37+
}
38+
3339

3440
const handleDel = async (id: string) => {
3541
const hide = message.loading('正在删除');
@@ -107,11 +113,7 @@ const MenuPage: React.FC = () => {
107113
},
108114
];
109115

110-
const reloadTree = () => {
111-
tree().then(res => {
112-
setTreeData([res.data]);
113-
})
114-
}
116+
115117

116118
useEffect(() => {
117119
reloadTree();

0 commit comments

Comments
 (0)