import React, { useState, useEffect } from 'react'; import { Card, Table, Button, Typography, Space, Modal, Form, Input, Select, Popconfirm, message, Tag, Row, Col } from 'antd'; import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons'; import { addUser, deleteUser, editUser, getUser } from '../api/user'; import { getRoleList } from '../api/role'; const { Title } = Typography; const { Option } = Select; export default function UserList() { const [userList, setUserList] = useState([]); const [modalVisible, setModalVisible] = useState(false); const [form] = Form.useForm(); const [currentUser, setCurrentUser] = useState(null); const [messageApi, contextHolder] = message.useMessage(); const [roles, setRoles] = useState([]); // 统一分页 const [pagination, setPagination] = useState({ pageNum: 1, pageSize: 10, total: 0 }); // 获取角色列表 const getRole = () => { const params = { pageNum: 1, pageSize: 1000 }; getRoleList(params).then(res => { if (res.code === 200) { setRoles(res.data || []); } }); }; // 获取用户列表 const getUserList = () => { const params = { pageNum: pagination.pageNum, pageSize: pagination.pageSize }; getUser(params).then(res => { if (res.code === 200) { setUserList(res.rows || []); setPagination({ ...pagination, total: res.total || res.rows?.length || 0 }); } }); }; // 分页切换 const handleTableChange = (newPagination) => { setPagination({ ...pagination, pageNum: newPagination.current, pageSize: newPagination.pageSize }); }; useEffect(() => { getUserList(); //getRole(); }, [pagination.pageNum, pagination.pageSize]); // 新增 const handleAdd = () => { form.resetFields(); setCurrentUser(null); setModalVisible(true); }; // 编辑 const handleEdit = (record) => { setCurrentUser(record); const roleIds = record.roles?.map(item => item.roleId) || []; form.setFieldsValue({ ...record, roleIds: roleIds, // 回填多选角色 }); setModalVisible(true); }; // 删除 const handleDelete = (userId) => { deleteUser(userId).then(res => { if (res.code === 200) { messageApi.success('删除成功'); getUserList(); } else { messageApi.error(res.msg || '删除失败'); } }).catch(() => { messageApi.error('删除失败'); }) }; // 提交保存 const handleSubmit = () => { form.validateFields().then(values => { const params = { ...values, roleIds: values.roleIds || [], // 直接提交数组 }; if (currentUser) { params.userId = currentUser.userId; editUser(params).then(res => { if (res.code === 200) { messageApi.success('编辑成功'); getUserList(); setModalVisible(false); } else { messageApi.error(res.msg || '编辑失败'); } }).catch(() => { messageApi.error('编辑失败'); }); } else { addUser(params).then(res => { if (res.code === 200) { messageApi.success('新增成功'); getUserList(); setModalVisible(false); } else { messageApi.error(res.msg || '新增失败'); } }).catch(() => { messageApi.error('新增失败'); }) } }); }; // 表格列(支持多角色展示) const columns = [ { title: '用户账号', dataIndex: 'userName' }, { title: '用户昵称', dataIndex: 'nickName' }, { title: '手机号', dataIndex: 'phonenumber' }, { title: '邮箱', dataIndex: 'email' }, { title: '角色', render: (_, record) => { const roles = record.roles || []; if (roles.length === 0) return 未分配; return roles.map(item => ( {item.roleName || item.roleKey || '角色'} )); } }, { title: '状态', dataIndex: 'status', render: (s) => s === '0' ? 正常 : 停用 }, { title: '操作', render: (_, r) => ( handleDelete(r.userId)}> ) } ]; return (
{contextHolder}
`共 ${t} 条` }} onChange={handleTableChange} bordered /> {/* 新增/编辑弹窗 */} setModalVisible(false)} onOk={handleSubmit} destroyOnClose >
{/* 多角色选择 */} ); }