菜单模块增删改查

This commit is contained in:
mmc
2026-04-28 14:45:07 +08:00
parent c08278190a
commit 2a2d40d772
6 changed files with 532 additions and 528 deletions

View File

@@ -1,13 +1,14 @@
import React, { useState, useEffect } from 'react';
import React, { useState } from 'react';
import {
Card, Row, Col, Button, Typography, Space, Modal, Form, Tree, message,
Input, Popconfirm, Divider
} from 'antd';
import {
PlusOutlined, LockOutlined, SettingOutlined, EditOutlined, DeleteOutlined
PlusOutlined, SettingOutlined, EditOutlined, DeleteOutlined, CloseOutlined
} from '@ant-design/icons';
const { Text, Title } = Typography;
const { Text } = Typography;
const AntdCard = Card as any;
// 默认角色
const defaultRoles = [
@@ -46,7 +47,7 @@ export default function Roles() {
const [editRole, setEditRole] = useState(null);
// 打开权限配置
const handleOpenPerm = async (role) => {
const handleOpenPerm = (role) => {
setCurrentRole(role);
setCheckedKeys([]);
setPermVisible(true);
@@ -54,7 +55,7 @@ export default function Roles() {
// 保存权限
const handleSavePerm = () => {
message.success(`【${currentRole.name}】菜单权限配置成功!`);
message.success(`【${currentRole?.name}】菜单权限配置成功!`);
setPermVisible(false);
};
@@ -85,16 +86,14 @@ export default function Roles() {
roleForm.validateFields().then(values => {
const roleData = {
...values,
menuKeys: checkedKeys, // 保存菜单权限
menuKeys: checkedKeys,
level: editRole ? editRole.level : 'Level Custom'
};
if (editRole) {
// 编辑
setRoles(roles.map(r => r.key === editRole.key ? { ...r, ...roleData } : r));
message.success('编辑成功');
} else {
// 新增
const newRole = {
...roleData,
key: values.key || Date.now().toString(),
@@ -107,52 +106,76 @@ export default function Roles() {
};
return (
<div className="space-y-4">
<div className="flex justify-between items-center">
<Title level={5} style={{ margin: 0 }}>角色管理</Title>
<Button type="primary" icon={<PlusOutlined />} onClick={handleAddRole}>
新增角色
</Button>
</div>
<div style={{ padding: '16px', background: '#f4f7f9', minHeight: '100%' }}>
{/* 标题 */}
<div style={{ fontSize: 16, fontWeight: 600, marginBottom: 16 }}>角色管理</div>
<Row gutter={16}>
{/* 顶部操作栏 */}
<AntdCard size="small" style={{ borderRadius: 8, marginBottom: 16 }}>
<div style={{ display: 'flex', justifyContent: 'flex-end' }}>
<Button
type="primary"
icon={<PlusOutlined />}
onClick={handleAddRole}
style={{ borderRadius: 2 }}
>
新增角色
</Button>
</div>
</AntdCard>
{/* 角色卡片列表 */}
<Row gutter={[16, 16]}>
{roles.map(role => (
<Col span={8} key={role.key}>
<Card
<Col xs={24} sm={12} md={8} lg={6} key={role.key}>
<AntdCard
size="small"
hoverable
style={{ borderRadius: 8, boxShadow: '0 2px 8px rgba(0,0,0,0.06)' }}
actions={[
<Button
type="link"
type="text"
icon={<SettingOutlined />}
onClick={() => handleOpenPerm(role)}
style={{ fontSize: 12 }}
>
菜单权限
</Button>,
<Button
type="link"
type="text"
icon={<EditOutlined />}
onClick={() => handleEditRole(role)}
style={{ fontSize: 12 }}
>
编辑
</Button>,
<Popconfirm
title="确定删除该角色?"
onConfirm={() => handleDeleteRole(role.key)}
okText="确定"
cancelText="取消"
>
<Button type="link" danger icon={<DeleteOutlined />}>删除</Button>
<Button
type="text"
danger
icon={<DeleteOutlined />}
style={{ fontSize: 12 }}
>
删除
</Button>
</Popconfirm>
]}
>
<Card.Meta
title={role.name}
title={<span style={{ fontWeight: 600, fontSize: 14 }}>{role.name}</span>}
description={
<Space direction="vertical" size={2}>
<Text type="secondary" style={{ fontSize: 10 }}>{role.level}</Text>
<Text type="secondary" style={{ fontSize: 12 }}>{role.desc}</Text>
<Space direction="vertical" size={2} style={{ width: '100%' }}>
<Text type="secondary" style={{ fontSize: 11 }}>{role.level}</Text>
<Text type="secondary" style={{ fontSize: 12, lineHeight: 1.4 }}>{role.desc}</Text>
</Space>
}
/>
</Card>
</AntdCard>
</Col>
))}
</Row>
@@ -164,6 +187,7 @@ export default function Roles() {
width={520}
onCancel={() => setPermVisible(false)}
onOk={handleSavePerm}
closeIcon={<CloseOutlined />}
>
<Tree
checkable
@@ -174,7 +198,7 @@ export default function Roles() {
/>
</Modal>
{/* ========== 新增/编辑角色弹窗(带菜单选择) ========== */}
{/* ========== 新增/编辑角色弹窗 ========== */}
<Modal
title={editRole ? '编辑角色' : '新增角色'}
open={roleModalVisible}
@@ -182,6 +206,7 @@ export default function Roles() {
onOk={handleSaveRole}
maskClosable={false}
width={550}
closeIcon={<CloseOutlined />}
>
<Form form={roleForm} layout="vertical">
<Form.Item
@@ -189,7 +214,7 @@ export default function Roles() {
label="角色名称"
rules={[{ required: true, message: '请输入角色名称' }]}
>
<Input placeholder="例如:运维管理员" />
<Input placeholder="例如:运维管理员" style={{ borderRadius: 2 }} />
</Form.Item>
<Form.Item
@@ -197,15 +222,14 @@ export default function Roles() {
label="角色标识"
rules={[{ required: true, message: '请输入角色标识' }]}
>
<Input placeholder="例如:operation" />
<Input placeholder="例如:operation" style={{ borderRadius: 2 }} />
</Form.Item>
<Form.Item name="desc" label="角色描述">
<Input.TextArea rows={3} placeholder="描述该角色的权限范围" />
<Input.TextArea rows={3} placeholder="描述该角色的权限范围" style={{ borderRadius: 2 }} />
</Form.Item>
{/* ========== 新增角色时直接选择菜单权限 ========== */}
<Divider orientation="left">菜单权限配置</Divider>
<Divider orientation="left" style={{ margin: '16px 0' }}>菜单权限配置</Divider>
<Tree
checkable
treeData={menuTree}