import { addUser, batchDeleteUsers, deleteUser, getUsers, oneClickClear, updateUser, updateUserPoints, } from '@/services/api'; import { DeleteOutlined, 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, Modal, 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 handleClear = async () => { try { const result = await oneClickClear(); if (result.success) { message.success('清空成功'); actionRef.current?.reload(); } else { message.error(result.error); } } catch (error) { message.error('操作失败'); } }; const columns: ProColumns[] = [ { title: '用户名', dataIndex: 'username', valueType: 'text', }, { title: '角色', dataIndex: 'role', valueType: 'select', valueEnum: { user: { text: '普通用户' }, admin: { text: '管理员' }, }, }, { title: '积分', dataIndex: 'points', valueType: 'digit', sorter: true, }, { title: '密保问题', dataIndex: 'securityQuestion', valueType: 'text', }, { title: '密保答案', dataIndex: 'securityAnswer', valueType: 'text', }, { title: '创建时间', dataIndex: 'createdAt', valueType: 'dateTime', sorter: true, }, { title: '最后登录时间', dataIndex: 'lastLoginAt', valueType: 'dateTime', sorter: true, }, { title: '登录IP', dataIndex: 'loginIp', valueType: 'text', }, { title: '最后登录地址', dataIndex: 'lastLoginAddress', valueType: 'text', }, { 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, sort, filter) => { return getUsers(params, sort) }} 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;