Skip to content

Commit c2577dc

Browse files
committed
menus
1 parent bd1bad3 commit c2577dc

File tree

12 files changed

+138
-40
lines changed

12 files changed

+138
-40
lines changed

antd-pro/src/access.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
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');
7+
const authentications = localStorage.getItem('authentications');
88
return {
99
isAdmin: isAdmin === undefined ? false : isAdmin,
1010
hasAuthentication: (element: any) => {
11-
console.log('path:',element.path);
12-
console.log('authentications:',authentications);
13-
if (element.path === '/') {
11+
if (authentications === null || authentications === undefined) {
1412
return true;
1513
}
16-
17-
if (authentications === null || authentications === undefined) {
18-
return false;
14+
if (element.path === '/') {
15+
return true;
1916
}
2017
if (authentications.indexOf(element.path) > -1) {
2118
return true;

antd-pro/src/app.tsx

+10-5
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,16 @@ export const layout: RunTimeLayoutConfig = ({ initialState, setInitialState }) =
6363
},
6464
},
6565
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);
66+
request: async (params,defaultMenuData) => {
67+
try{
68+
const response = await menus();
69+
const authentications = await loadLoayoutMenuAuthentications(response);
70+
localStorage.setItem('authentications', JSON.stringify(authentications));
71+
return await loadLayoutMenus(response);
72+
}catch(error){
73+
localStorage.removeItem('authentications');
74+
return defaultMenuData
75+
}
7176
}
7277
},
7378
waterMarkProps: {

antd-pro/src/components/Menu/index.tsx

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
import React from 'react';
2+
import Icon from '@ant-design/icons'
3+
import * as icons from '@ant-design/icons'
4+
25

36
interface MenuProps {
47
icon: string
58
}
69

710
const Menus: React.FC<MenuProps> = (props) => {
8-
return (
9-
<img src={props.icon} style={{
10-
width: '20px'
11-
}} />
12-
)
11+
if (props.icon === '-') {
12+
return <></>
13+
}
14+
15+
if (props.icon) {
16+
return <Icon component={icons[props.icon]} />
17+
} else {
18+
return <></>
19+
}
1320
}
1421

15-
export async function loadLayoutMenus(response:any) {
22+
export async function loadLayoutMenus(response: any) {
1623
if (response.success) {
1724
let childrens = response.data.children;
1825
if (childrens === null) {
@@ -26,14 +33,14 @@ export async function loadLayoutMenus(response:any) {
2633
});
2734
return data;
2835
}
29-
childrens = childrens.map((item:any) => fetchMenu(item));
36+
childrens = childrens.map((item: any) => fetchMenu(item));
3037
return childrens;
3138
} else {
3239
return [];
3340
}
3441
}
3542

36-
export async function loadLoayoutMenuAuthentications(response:any) {
43+
export async function loadLoayoutMenuAuthentications(response: any) {
3744
if (response.success) {
3845
let childrens = response.data.children;
3946
if (childrens === null) {
@@ -48,7 +55,7 @@ export async function loadLoayoutMenuAuthentications(response:any) {
4855
});
4956
}
5057
}
51-
childrens.forEach((element:any) => {
58+
childrens.forEach((element: any) => {
5259
feathAuthorities(element);
5360
});
5461
return authorities;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import React from 'react'
2+
import Icon from '@ant-design/icons'
3+
import * as icons from '@ant-design/icons'
4+
import { ProFormSelect, ProFormSelectProps } from '@ant-design/pro-components'
5+
6+
export interface IconSelectProps extends ProFormSelectProps {
7+
placeholder?: string
8+
}
9+
const IconSelect: React.FC<IconSelectProps> = (props) => {
10+
const iconList = Object.keys(icons).filter((item) => typeof icons[item] === 'object')
11+
const options = iconList.map(item => {
12+
return {
13+
label: (
14+
<>
15+
<Icon component={icons[item]} style={{ marginRight: '8px' }} /> {item}
16+
</>
17+
),
18+
value: item
19+
}
20+
});
21+
22+
return (
23+
<ProFormSelect
24+
{
25+
...props
26+
}
27+
showSearch
28+
allowClear
29+
options={options}
30+
>
31+
</ProFormSelect>
32+
)
33+
}
34+
35+
export default IconSelect

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

+18-10
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import { list, save, del, tree } from '@/services/api/menu';
22
import { PlusOutlined } from '@ant-design/icons';
33
import type { ActionType, ProColumns } from '@ant-design/pro-components';
44
import { ModalForm, PageContainer, ProCard, ProFormDigit, ProFormText, ProFormTreeSelect } from '@ant-design/pro-components';
5-
import { Button, Form, message, Popconfirm } from 'antd';
5+
import { Button, Form, message, Modal, Popconfirm } from 'antd';
66
import { MyTable } from 'coding-components';
77
import React, { useRef, useState, useEffect } from 'react';
88
import MenuTree from './tree';
9+
import IconSelect from './icons';
10+
import Icon from '@ant-design/icons';
11+
import * as icons from '@ant-design/icons'
912

1013
const MenuPage: React.FC = () => {
1114

@@ -66,6 +69,16 @@ const MenuPage: React.FC = () => {
6669
title: "图标",
6770
dataIndex: 'icon',
6871
search: false,
72+
render: (_, record) => {
73+
if (record.icon === '-') {
74+
return '';
75+
}
76+
if (record.icon) {
77+
return <Icon component={icons[record.icon]} />
78+
} else {
79+
return '';
80+
}
81+
}
6982
},
7083
{
7184
title: "地址",
@@ -254,22 +267,15 @@ const MenuPage: React.FC = () => {
254267
label="菜单地址"
255268
rules={[
256269
{
257-
required: true,
258270
message: "请输入菜单地址",
259271
},
260272
]}
261273
name="path"
262274
/>
263-
<ProFormText
264-
placeholder="请输入菜单图标"
275+
<IconSelect
265276
label="菜单图标"
266-
rules={[
267-
{
268-
required: true,
269-
message: "请输入菜单图标",
270-
},
271-
]}
272277
name="icon"
278+
placeholder="请输入菜单图标"
273279
/>
274280
<ProFormDigit
275281
min={0}
@@ -290,6 +296,8 @@ const MenuPage: React.FC = () => {
290296
/>
291297
</ModalForm>
292298

299+
300+
293301
</PageContainer>
294302
);
295303
};

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { Tree } from 'antd';
22
import React, { useEffect, useImperativeHandle, useState, forwardRef } from 'react';
3-
import { DownOutlined, HomeOutlined, TeamOutlined } from '@ant-design/icons';
3+
import { DownOutlined } from '@ant-design/icons';
44
import { DataNode } from 'antd/es/tree';
55
import { tree } from '@/services/api/menu';
66

7+
const { DirectoryTree } = Tree;
78
interface MenuPageProps {
89
onSelect?: (selectedKeys: React.Key[], info?: any) => void;
910
}
@@ -20,7 +21,7 @@ const MenuTree = forwardRef((props: MenuPageProps, ref) => {
2021
const search = (data: any) => {
2122
data.title = data.name;
2223
data.key = data.id;
23-
data.icon = data.type === 'COMPANY' ? <HomeOutlined /> : <TeamOutlined />;
24+
delete data.icon;
2425
data.children = data.children || [];
2526
data.children.forEach((item: any) => {
2627
search(item);
@@ -43,16 +44,18 @@ const MenuTree = forwardRef((props: MenuPageProps, ref) => {
4344
}));
4445

4546
const onSelect = (selectedKeys: React.Key[]) => {
46-
setSelectedKeys(selectedKeys);
47-
if (props.onSelect) {
48-
props.onSelect(selectedKeys);
47+
if (selectedKeys.length > 0) {
48+
setSelectedKeys(selectedKeys);
49+
if (props.onSelect) {
50+
props.onSelect(selectedKeys);
51+
}
4952
}
5053
};
5154

5255
return (
5356
<>
5457
{treeData && treeData.length > 0 && (
55-
<Tree
58+
<DirectoryTree
5659
showIcon
5760
blockNode
5861
defaultExpandAll

antd-pro/src/services/api/account.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,5 @@ export async function login(body: Account.LoginRequest) {
3939
* @returns 菜单列表
4040
*/
4141
export async function menus(){
42-
return get('/api/menu/tree');
42+
return get('/api/menus');
4343
}

components-server/components-server/pom.xml

-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
<version>${project.version}</version>
4040
</dependency>
4141

42-
4342
<dependency>
4443
<groupId>com.codingapi.components</groupId>
4544
<artifactId>menu-restapi</artifactId>

components-server/components/menu/menu-domain/src/main/java/com/codingapi/components/menu/config/MenuDomainConfiguration.java

+13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.codingapi.components.menu.context.MenuContextRegister;
44
import com.codingapi.components.menu.domain.Menu;
5+
import com.codingapi.components.menu.gateway.MenuGateway;
56
import com.codingapi.components.menu.repository.MenuRepository;
67
import com.codingapi.components.menu.service.MenuService;
78
import com.codingapi.springboot.framework.dto.request.PageRequest;
@@ -61,4 +62,16 @@ public MenuService menuService(MenuRepository menuRepository) {
6162
public MenuContextRegister menuContextRegister(MenuRepository menuRepository){
6263
return new MenuContextRegister(menuRepository);
6364
}
65+
66+
67+
@Bean
68+
@ConditionalOnMissingBean
69+
public MenuGateway menuGateway(MenuRepository menuRepository){
70+
return new MenuGateway() {
71+
@Override
72+
public Menu loadMenus() {
73+
return menuRepository.tree();
74+
}
75+
};
76+
}
6477
}

components-server/components/menu/menu-domain/src/main/java/com/codingapi/components/menu/domain/Menu.java

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class Menu {
2121

2222
private String name;
2323

24-
@Column(unique = true)
2524
private String path;
2625

2726
private String icon;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.codingapi.components.menu.gateway;
2+
3+
import com.codingapi.components.menu.domain.Menu;
4+
5+
public interface MenuGateway {
6+
7+
Menu loadMenus();
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.codingapi.components.menu.controller;
2+
3+
import com.codingapi.components.menu.domain.Menu;
4+
import com.codingapi.components.menu.gateway.MenuGateway;
5+
import com.codingapi.springboot.framework.dto.response.SingleResponse;
6+
import lombok.AllArgsConstructor;
7+
import org.springframework.web.bind.annotation.GetMapping;
8+
import org.springframework.web.bind.annotation.RequestMapping;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
12+
@RestController
13+
@AllArgsConstructor
14+
@RequestMapping("/api/menus")
15+
public class MenusController {
16+
17+
private final MenuGateway menuGateway;
18+
19+
@GetMapping
20+
public SingleResponse<Menu> index() {
21+
return SingleResponse.of(menuGateway.loadMenus());
22+
}
23+
24+
}

0 commit comments

Comments
 (0)