import { useState, useEffect } from 'react'; import { Table, Card, Button, Space, Tag, Modal, Form, Input, Select, message, Typography, Row, Col, Statistic } from 'antd'; import type { ColumnsType } from 'antd/es/table'; import { PlusOutlined, EditOutlined, DeleteOutlined, SearchOutlined, UserOutlined, TeamOutlined, CrownOutlined } from '@ant-design/icons'; import { useLoading } from '@/store'; import { formatDate } from '@/utils'; const { Title } = Typography; const { Option } = Select; interface User { id: string; name: string; email: string; phone: string; role: 'admin' | 'translator' | 'user'; status: 'active' | 'inactive' | 'suspended'; registeredAt: Date; lastLoginAt: Date; } const UserList = () => { const [users, setUsers] = useState([]); const [loading, setLoading] = useState(false); const [modalVisible, setModalVisible] = useState(false); const [editingUser, setEditingUser] = useState(null); const [searchText, setSearchText] = useState(''); const [form] = Form.useForm(); const { setLoading: setGlobalLoading } = useLoading(); // 模拟数据 const mockUsers: User[] = [ { id: '1', name: '张三', email: 'zhangsan@example.com', phone: '13800138001', role: 'admin', status: 'active', registeredAt: new Date('2023-01-15'), lastLoginAt: new Date('2024-01-15'), }, { id: '2', name: '李四', email: 'lisi@example.com', phone: '13800138002', role: 'translator', status: 'active', registeredAt: new Date('2023-02-20'), lastLoginAt: new Date('2024-01-14'), }, { id: '3', name: '王五', email: 'wangwu@example.com', phone: '13800138003', role: 'user', status: 'inactive', registeredAt: new Date('2023-03-10'), lastLoginAt: new Date('2024-01-10'), }, ]; const columns: ColumnsType = [ { title: '姓名', dataIndex: 'name', key: 'name', filteredValue: searchText ? [searchText] : null, onFilter: (value, record) => record.name.toLowerCase().includes(String(value).toLowerCase()) || record.email.toLowerCase().includes(String(value).toLowerCase()), }, { title: '邮箱', dataIndex: 'email', key: 'email', }, { title: '电话', dataIndex: 'phone', key: 'phone', }, { title: '角色', dataIndex: 'role', key: 'role', render: (role: string) => { const roleMap = { admin: { color: 'red', text: '管理员', icon: }, translator: { color: 'blue', text: '译员', icon: }, user: { color: 'green', text: '用户', icon: }, }; const roleInfo = roleMap[role as keyof typeof roleMap]; return ( {roleInfo.text} ); }, }, { title: '状态', dataIndex: 'status', key: 'status', render: (status: string) => { const statusMap = { active: { color: 'green', text: '活跃' }, inactive: { color: 'orange', text: '不活跃' }, suspended: { color: 'red', text: '已暂停' }, }; const statusInfo = statusMap[status as keyof typeof statusMap]; return {statusInfo.text}; }, }, { title: '注册时间', dataIndex: 'registeredAt', key: 'registeredAt', render: (date: Date) => formatDate(date), }, { title: '最后登录', dataIndex: 'lastLoginAt', key: 'lastLoginAt', render: (date: Date) => formatDate(date), }, { title: '操作', key: 'action', render: (_, record) => ( ), }, ]; const handleEdit = (user: User) => { setEditingUser(user); form.setFieldsValue(user); setModalVisible(true); }; const handleDelete = (userId: string) => { Modal.confirm({ title: '确认删除', content: '确定要删除这个用户吗?', onOk: () => { setUsers(users.filter(user => user.id !== userId)); message.success('删除成功'); }, }); }; const handleSubmit = async (values: any) => { try { setLoading(true); if (editingUser) { // 编辑用户 setUsers(users.map(user => user.id === editingUser.id ? { ...user, ...values } : user )); message.success('更新成功'); } else { // 新增用户 const newUser: User = { id: Date.now().toString(), ...values, registeredAt: new Date(), lastLoginAt: new Date(), }; setUsers([...users, newUser]); message.success('添加成功'); } setModalVisible(false); form.resetFields(); setEditingUser(null); } catch (error) { message.error('操作失败'); } finally { setLoading(false); } }; const handleCancel = () => { setModalVisible(false); form.resetFields(); setEditingUser(null); }; useEffect(() => { setGlobalLoading(true); // 模拟数据加载 setTimeout(() => { setUsers(mockUsers); setGlobalLoading(false); }, 1000); }, [setGlobalLoading]); const stats = { total: users.length, active: users.filter(u => u.status === 'active').length, translators: users.filter(u => u.role === 'translator').length, admins: users.filter(u => u.role === 'admin').length, }; return (
用户管理 {/* 统计卡片 */} } /> } valueStyle={{ color: '#3f8600' }} /> } valueStyle={{ color: '#1890ff' }} /> } valueStyle={{ color: '#cf1322' }} />
} value={searchText} onChange={(e: React.ChangeEvent) => setSearchText(e.target.value)} style={{ width: 300 }} />
`共 ${total} 条记录`, }} /> form.submit()} onCancel={handleCancel} confirmLoading={loading} >
); }; export default UserList;