2026-04-28 15:16:50 +08:00
|
|
|
|
import React, { useState, useEffect } from 'react';
|
2026-04-24 15:28:16 +08:00
|
|
|
|
import {
|
|
|
|
|
|
Card, Row, Col, Button, Typography, Space, Modal, Form, Tree, message,
|
2026-04-29 10:00:24 +08:00
|
|
|
|
Input, Popconfirm, Divider, InputNumber, Switch, Pagination
|
2026-04-17 17:20:41 +08:00
|
|
|
|
} from 'antd';
|
2026-04-24 15:28:16 +08:00
|
|
|
|
import {
|
2026-04-29 12:49:47 +08:00
|
|
|
|
PlusOutlined, EditOutlined, DeleteOutlined, CloseOutlined,
|
|
|
|
|
|
UserOutlined
|
2026-04-17 17:20:41 +08:00
|
|
|
|
} from '@ant-design/icons';
|
2026-04-28 15:16:50 +08:00
|
|
|
|
import { getMenuList } from '../api/mune';
|
|
|
|
|
|
import { buildPermTree, buildTreeFromFlat } from '../lib/utils';
|
2026-04-28 16:54:36 +08:00
|
|
|
|
import { addRole, deleteRole, editRoleApi, getRoleList } from '../api/role';
|
2026-04-28 16:36:22 +08:00
|
|
|
|
import EmptyData from '../components/Empty';
|
|
|
|
|
|
|
2026-04-28 14:45:07 +08:00
|
|
|
|
const { Text } = Typography;
|
|
|
|
|
|
const AntdCard = Card as any;
|
2026-04-17 17:20:41 +08:00
|
|
|
|
|
|
|
|
|
|
export default function Roles() {
|
2026-04-28 16:36:22 +08:00
|
|
|
|
const [roles, setRoles] = useState([]);
|
|
|
|
|
|
const [menuTree, setMenuTree] = useState([]);
|
2026-04-28 16:54:36 +08:00
|
|
|
|
const [checkedKeys, setCheckedKeys] = useState<number[]>([]);
|
2026-04-24 15:28:16 +08:00
|
|
|
|
const [roleModalVisible, setRoleModalVisible] = useState(false);
|
|
|
|
|
|
const [roleForm] = Form.useForm();
|
|
|
|
|
|
const [editRole, setEditRole] = useState(null);
|
2026-04-28 15:16:50 +08:00
|
|
|
|
const [messageApi, contextHolder] = message.useMessage();
|
|
|
|
|
|
|
2026-04-29 10:00:24 +08:00
|
|
|
|
// 统一分页
|
|
|
|
|
|
const [pagination, setPagination] = useState({
|
|
|
|
|
|
pageNum: 1,
|
|
|
|
|
|
pageSize: 10,
|
|
|
|
|
|
total: 0
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// 获取角色列表(带分页)
|
2026-04-28 16:36:22 +08:00
|
|
|
|
const getRole = () => {
|
2026-04-29 10:00:24 +08:00
|
|
|
|
const params = {
|
|
|
|
|
|
pageNum: pagination.pageNum,
|
|
|
|
|
|
pageSize: pagination.pageSize
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
getRoleList(params).then(res => {
|
2026-04-28 16:36:22 +08:00
|
|
|
|
if (res.code == 200) {
|
|
|
|
|
|
setRoles(res.data || []);
|
2026-04-29 10:00:24 +08:00
|
|
|
|
setPagination({
|
|
|
|
|
|
...pagination,
|
|
|
|
|
|
total: res.total || res.data?.length || 0
|
|
|
|
|
|
});
|
2026-04-28 16:36:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
})
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 获取菜单树
|
|
|
|
|
|
const getMenu = () => {
|
2026-04-29 10:00:24 +08:00
|
|
|
|
const params = {
|
|
|
|
|
|
pageNum: 1,
|
|
|
|
|
|
pageSize: 1000
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
getMenuList(params).then(res => {
|
2026-04-28 15:16:50 +08:00
|
|
|
|
if (res.code === 200) {
|
|
|
|
|
|
const data = res.data || [];
|
|
|
|
|
|
const treeData = buildTreeFromFlat(data);
|
|
|
|
|
|
const permTree = buildPermTree(treeData);
|
|
|
|
|
|
setMenuTree(permTree);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2026-04-28 16:36:22 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-04-29 10:00:24 +08:00
|
|
|
|
// 分页切换(正确触发)
|
|
|
|
|
|
const handlePageChange = (pageNum, pageSize) => {
|
|
|
|
|
|
setPagination({
|
|
|
|
|
|
...pagination,
|
|
|
|
|
|
pageNum: pageNum,
|
|
|
|
|
|
pageSize: pageSize
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-04-28 16:36:22 +08:00
|
|
|
|
useEffect(() => {
|
|
|
|
|
|
getRole();
|
2026-04-29 10:00:24 +08:00
|
|
|
|
getMenu();
|
|
|
|
|
|
}, [pagination.pageNum, pagination.pageSize]);
|
2026-04-24 15:28:16 +08:00
|
|
|
|
|
2026-04-28 16:36:22 +08:00
|
|
|
|
// 新增角色
|
2026-04-24 15:28:16 +08:00
|
|
|
|
const handleAddRole = () => {
|
|
|
|
|
|
roleForm.resetFields();
|
|
|
|
|
|
setCheckedKeys([]);
|
|
|
|
|
|
setEditRole(null);
|
2026-04-28 16:36:22 +08:00
|
|
|
|
roleForm.setFieldsValue({
|
|
|
|
|
|
status: true,
|
|
|
|
|
|
roleSort: 1,
|
|
|
|
|
|
});
|
2026-04-24 15:28:16 +08:00
|
|
|
|
setRoleModalVisible(true);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2026-04-28 16:36:22 +08:00
|
|
|
|
// 编辑角色
|
2026-04-24 15:28:16 +08:00
|
|
|
|
const handleEditRole = (record) => {
|
|
|
|
|
|
setEditRole(record);
|
2026-04-28 16:36:22 +08:00
|
|
|
|
roleForm.setFieldsValue({
|
|
|
|
|
|
...record,
|
|
|
|
|
|
status: record.status == 1,
|
|
|
|
|
|
});
|
2026-04-28 16:54:36 +08:00
|
|
|
|
setCheckedKeys((record.menuIds || []).map(Number));
|
2026-04-24 15:28:16 +08:00
|
|
|
|
setRoleModalVisible(true);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 删除角色
|
2026-04-28 16:36:22 +08:00
|
|
|
|
const handleDeleteRole = (roleId) => {
|
|
|
|
|
|
deleteRole([roleId]).then(res => {
|
|
|
|
|
|
if (res.code == 200) {
|
|
|
|
|
|
messageApi.success('删除成功');
|
|
|
|
|
|
getRole();
|
|
|
|
|
|
} else {
|
|
|
|
|
|
messageApi.error(res.msg || '删除失败');
|
|
|
|
|
|
}
|
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
|
messageApi.error('删除失败');
|
|
|
|
|
|
})
|
2026-04-24 15:28:16 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 提交保存角色
|
|
|
|
|
|
const handleSaveRole = () => {
|
|
|
|
|
|
roleForm.validateFields().then(values => {
|
|
|
|
|
|
if (editRole) {
|
2026-04-28 16:54:36 +08:00
|
|
|
|
const params = {
|
|
|
|
|
|
...values,
|
|
|
|
|
|
menuIds: checkedKeys,
|
|
|
|
|
|
status: values.status ? 1 : 0,
|
|
|
|
|
|
roleSort: values.roleSort || 1,
|
|
|
|
|
|
roleId: editRole.roleId
|
|
|
|
|
|
};
|
|
|
|
|
|
editRoleApi(params).then(res => {
|
|
|
|
|
|
if (res.code == 200) {
|
|
|
|
|
|
messageApi.success('编辑角色成功');
|
|
|
|
|
|
getRole();
|
|
|
|
|
|
setRoleModalVisible(false);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
messageApi.error(res.msg || "编辑失败");
|
|
|
|
|
|
}
|
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
|
messageApi.error("编辑失败");
|
|
|
|
|
|
});
|
2026-04-24 15:28:16 +08:00
|
|
|
|
} else {
|
2026-04-28 16:54:36 +08:00
|
|
|
|
const params = {
|
|
|
|
|
|
...values,
|
|
|
|
|
|
menuIds: checkedKeys,
|
|
|
|
|
|
status: values.status ? 1 : 0,
|
|
|
|
|
|
roleSort: values.roleSort || 1,
|
|
|
|
|
|
};
|
2026-04-28 16:36:22 +08:00
|
|
|
|
addRole(params).then(res => {
|
|
|
|
|
|
if (res.code == 200) {
|
|
|
|
|
|
messageApi.success('新增角色成功');
|
|
|
|
|
|
getRole();
|
|
|
|
|
|
setRoleModalVisible(false);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
messageApi.error(res.msg || "新增失败");
|
|
|
|
|
|
}
|
|
|
|
|
|
}).catch(() => {
|
|
|
|
|
|
messageApi.error("新增失败");
|
|
|
|
|
|
});
|
2026-04-24 15:28:16 +08:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
return (
|
2026-04-29 09:47:01 +08:00
|
|
|
|
<div style={{ padding: '20px' }}>
|
2026-04-28 15:16:50 +08:00
|
|
|
|
{contextHolder}
|
|
|
|
|
|
|
2026-04-29 09:47:01 +08:00
|
|
|
|
{/* 统一卡片容器(和菜单管理一致) */}
|
|
|
|
|
|
<AntdCard title="角色管理" style={{ borderRadius: 8 }}>
|
|
|
|
|
|
{/* 顶部操作栏(和菜单管理统一) */}
|
|
|
|
|
|
<div style={{ marginBottom: 16, display: 'flex', justifyContent: 'space-between' }}>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="primary"
|
|
|
|
|
|
icon={<PlusOutlined />}
|
|
|
|
|
|
onClick={handleAddRole}
|
|
|
|
|
|
>
|
|
|
|
|
|
新增角色
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
{/* 角色卡片列表 */}
|
2026-04-29 10:00:24 +08:00
|
|
|
|
<Row gutter={[16, 16]} style={{ marginBottom: 16 }}>
|
2026-04-29 09:47:01 +08:00
|
|
|
|
{roles && roles.length > 0 ? (
|
|
|
|
|
|
roles.map(role => (
|
|
|
|
|
|
<Col xs={24} sm={12} md={8} lg={6} key={role.roleId}>
|
|
|
|
|
|
<AntdCard
|
|
|
|
|
|
size="small"
|
|
|
|
|
|
hoverable
|
|
|
|
|
|
style={{ borderRadius: 8 }}
|
|
|
|
|
|
actions={[
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
icon={<EditOutlined />}
|
|
|
|
|
|
onClick={() => handleEditRole(role)}
|
2026-04-29 12:49:47 +08:00
|
|
|
|
style={{ color: '#1677ff' }}
|
2026-04-29 09:47:01 +08:00
|
|
|
|
>
|
|
|
|
|
|
编辑
|
|
|
|
|
|
</Button>,
|
2026-04-29 12:49:47 +08:00
|
|
|
|
<Button
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
icon={<UserOutlined />}
|
|
|
|
|
|
onClick={() => console.log('管理成员', role.roleId)}
|
|
|
|
|
|
style={{ color: '#00b96b' }}
|
|
|
|
|
|
>
|
|
|
|
|
|
管理成员
|
|
|
|
|
|
</Button>,
|
2026-04-29 09:47:01 +08:00
|
|
|
|
<Popconfirm
|
|
|
|
|
|
title="确定删除该角色?"
|
|
|
|
|
|
onConfirm={() => handleDeleteRole(role.roleId)}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Button
|
|
|
|
|
|
type="text"
|
|
|
|
|
|
danger
|
|
|
|
|
|
icon={<DeleteOutlined />}
|
|
|
|
|
|
>
|
|
|
|
|
|
删除
|
|
|
|
|
|
</Button>
|
|
|
|
|
|
</Popconfirm>
|
|
|
|
|
|
]}
|
|
|
|
|
|
>
|
|
|
|
|
|
<Card.Meta
|
|
|
|
|
|
title={<span style={{ fontWeight: 600 }}>{role.roleName}</span>}
|
|
|
|
|
|
description={
|
2026-04-29 12:49:47 +08:00
|
|
|
|
<Space direction="vertical" size={2} style={{ width: '100%' }}>
|
2026-04-29 09:47:01 +08:00
|
|
|
|
<Text type="secondary">权限标识:{role.roleKey}</Text>
|
2026-04-29 12:49:47 +08:00
|
|
|
|
<Text type="secondary">
|
|
|
|
|
|
状态:{role.status == 1 ? '正常' : '停用'}
|
|
|
|
|
|
</Text>
|
2026-04-29 09:47:01 +08:00
|
|
|
|
<Text type="secondary">
|
|
|
|
|
|
{role.admin && '【超级管理员】'}
|
|
|
|
|
|
{role.manager && '【机构管理员】'}
|
|
|
|
|
|
</Text>
|
|
|
|
|
|
</Space>
|
|
|
|
|
|
}
|
|
|
|
|
|
/>
|
|
|
|
|
|
</AntdCard>
|
|
|
|
|
|
</Col>
|
|
|
|
|
|
))
|
|
|
|
|
|
) : (
|
|
|
|
|
|
<Col span={24}>
|
2026-04-29 12:49:47 +08:00
|
|
|
|
<EmptyData height={260} description="暂无数据" />
|
2026-04-28 16:36:22 +08:00
|
|
|
|
</Col>
|
2026-04-29 09:47:01 +08:00
|
|
|
|
)}
|
|
|
|
|
|
</Row>
|
2026-04-29 10:00:24 +08:00
|
|
|
|
|
|
|
|
|
|
{/* 分页组件(正确触发) */}
|
|
|
|
|
|
<Pagination
|
|
|
|
|
|
current={pagination.pageNum}
|
|
|
|
|
|
pageSize={pagination.pageSize}
|
|
|
|
|
|
total={pagination.total}
|
|
|
|
|
|
showSizeChanger
|
|
|
|
|
|
showTotal={(total) => `共 ${total} 条`}
|
|
|
|
|
|
onChange={handlePageChange}
|
|
|
|
|
|
style={{ textAlign: 'right' }}
|
|
|
|
|
|
/>
|
2026-04-29 09:47:01 +08:00
|
|
|
|
</AntdCard>
|
2026-04-24 15:28:16 +08:00
|
|
|
|
|
2026-04-28 16:36:22 +08:00
|
|
|
|
{/* 新增/编辑弹窗 */}
|
2026-04-24 15:28:16 +08:00
|
|
|
|
<Modal
|
|
|
|
|
|
title={editRole ? '编辑角色' : '新增角色'}
|
|
|
|
|
|
open={roleModalVisible}
|
|
|
|
|
|
onCancel={() => setRoleModalVisible(false)}
|
|
|
|
|
|
onOk={handleSaveRole}
|
|
|
|
|
|
width={550}
|
2026-04-28 16:36:22 +08:00
|
|
|
|
maskClosable={false}
|
2026-04-24 15:28:16 +08:00
|
|
|
|
>
|
|
|
|
|
|
<Form form={roleForm} layout="vertical">
|
2026-04-28 16:36:22 +08:00
|
|
|
|
<Form.Item name="roleName" label="角色名称" rules={[{ required: true }]}>
|
|
|
|
|
|
<Input placeholder="请输入角色名称" />
|
2026-04-24 15:28:16 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
2026-04-28 16:36:22 +08:00
|
|
|
|
<Form.Item name="roleKey" label="角色权限" rules={[{ required: true }]}>
|
|
|
|
|
|
<Input placeholder="例如:admin" />
|
2026-04-24 15:28:16 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
2026-04-28 16:36:22 +08:00
|
|
|
|
<Form.Item name="roleSort" label="角色排序">
|
|
|
|
|
|
<InputNumber style={{ width: '100%' }} min={1} />
|
2026-04-24 15:28:16 +08:00
|
|
|
|
</Form.Item>
|
|
|
|
|
|
|
2026-04-29 09:47:01 +08:00
|
|
|
|
{/* 状态 Switch */}
|
2026-04-28 16:36:22 +08:00
|
|
|
|
<Row gutter={16}>
|
|
|
|
|
|
<Col span={8}>
|
|
|
|
|
|
<Form.Item name="status" label="状态" valuePropName="checked">
|
|
|
|
|
|
<Switch checkedChildren="正常" unCheckedChildren="停用" />
|
|
|
|
|
|
</Form.Item>
|
|
|
|
|
|
</Col>
|
|
|
|
|
|
</Row>
|
|
|
|
|
|
|
|
|
|
|
|
<Divider>菜单权限配置</Divider>
|
2026-04-24 15:28:16 +08:00
|
|
|
|
<Tree
|
|
|
|
|
|
checkable
|
|
|
|
|
|
treeData={menuTree}
|
|
|
|
|
|
checkedKeys={checkedKeys}
|
|
|
|
|
|
onCheck={setCheckedKeys}
|
|
|
|
|
|
defaultExpandAll
|
|
|
|
|
|
/>
|
|
|
|
|
|
</Form>
|
|
|
|
|
|
</Modal>
|
|
|
|
|
|
</div>
|
|
|
|
|
|
);
|
2026-04-17 17:20:41 +08:00
|
|
|
|
}
|