import { addUser, batchDeleteUsers, deleteUser, getUsers, updateUser, updateUserPoints, } from '@/services/api'; import { PlusOutlined } from '@ant-design/icons'; import type { ActionType, ProColumns } from '@ant-design/pro-components'; import { ModalForm, ProFormDigit, ProFormSelect, ProFormText } from '@ant-design/pro-form'; import { FooterToolbar, PageContainer, ProTable } from '@ant-design/pro-components'; import { Button, FormInstance, message, Popconfirm } from 'antd'; import React, { useRef, useState } from 'react'; import UpdateUser from './components/UpdateUser'; import UpdateUserPoints from './components/UpdateUserPoints'; const UserList: React.FC = () => { const [createModalOpen, handleModalOpen] = useState(false); const [updateModalOpen, handleUpdateModalOpen] = useState(false); const actionRef = useRef(); const [currentRow, setCurrentRow] = useState(); const [selectedRows, setSelectedRows] = useState([]); const [pageSize, setPageSize] = useState(10); const formRef = useRef(null); const [updatePointsModalOpen, setUpdatePointsModalOpen] = useState(false); const handleDelete = async (record: API.UserItem) => { const res = await deleteUser(record._id); if (res.success) { message.success('用户已成功删除'); actionRef.current?.reload(); } }; const handleBatchDelete = async (selectedRowKeys: string[]) => { if (selectedRowKeys.length === 0) return; const res = await batchDeleteUsers(selectedRowKeys); if (res.success) { message.success(`${res.message}`); actionRef.current?.reload(); } }; const columns: ProColumns[] = [ { title: '用户名', dataIndex: 'username', valueType: 'text', }, { title: '角色', dataIndex: 'role', valueType: 'select', valueEnum: { user: { text: '普通用户' }, admin: { text: '管理员' }, }, }, { title: '积分', dataIndex: 'points', valueType: 'digit', }, { title: '操作', dataIndex: 'option', valueType: 'option', render: (_, record) => [ { handleUpdateModalOpen(true); setCurrentRow(record); }} > 编辑用户 , { setUpdatePointsModalOpen(true); setCurrentRow(record); }} > 修改积分 , handleDelete(record)} okText="确定" cancelText="取消" > 删除 , ], }, ]; return ( actionRef={actionRef} rowKey="_id" search={{ labelWidth: 120, }} toolBarRender={() => [ , ]} request={async (params) => { const response = await getUsers(params); return { data: response.data, success: response.success, total: response.total, }; }} columns={columns} pagination={{ showSizeChanger: true, pageSize: pageSize, onChange: (page, pageSize) => { setPageSize(pageSize); if (actionRef.current) { actionRef.current.reload(); } }, }} rowSelection={{ onChange: (_, selectedRows) => { setSelectedRows(selectedRows); }, }} /> {selectedRows?.length > 0 && ( 已选择 {selectedRows.length} 项    } > )} { const res = await addUser(value as API.UserItem); if (res.success) { message.success('创建成功'); handleModalOpen(false); if (actionRef.current) { actionRef.current.reload(); } formRef.current?.resetFields(); } }} > { const res = await updateUser(value._id, value as API.UserItem); if (res.success) { message.success('修改成功'); handleUpdateModalOpen(false); setCurrentRow(undefined); if (actionRef.current) { actionRef.current.reload(); } } }} onCancel={() => { handleUpdateModalOpen(false); }} updateUserModalOpen={updateModalOpen} values={currentRow || {}} /> {/* 修改积分 */} { const res = await updateUserPoints(value.userId, value.points, value.reason); if (res.success) { message.success('积分更新成功'); setUpdatePointsModalOpen(false); setCurrentRow(undefined); if (actionRef.current) { actionRef.current.reload(); } } }} onCancel={() => { setUpdatePointsModalOpen(false); }} updatePointsModalOpen={updatePointsModalOpen} currentUser={currentRow || {}} /> ); }; export default UserList;