import { useState, useEffect } from 'react'; import AdminLayout from '@/components/admin/AdminLayout'; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from '@/components/ui/table'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { Badge } from '@/components/ui/badge'; import { Avatar, AvatarFallback, AvatarImage } from '@/components/ui/avatar'; import { Search, Edit, Trash2, MoreHorizontal, Loader2 } from 'lucide-react'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; interface User { _id: string; 用户名: string; 邮箱: string; 角色: string; 是否被封禁: boolean; 头像: string; createdAt: string; } export default function UserManagement() { const [users, setUsers] = useState([]); const [loading, setLoading] = useState(true); const [search, setSearch] = useState(''); const [page, setPage] = useState(1); const [totalPages, setTotalPages] = useState(1); const [totalUsers, setTotalUsers] = useState(0); // 编辑状态 const [editingUser, setEditingUser] = useState(null); const [isEditDialogOpen, setIsEditDialogOpen] = useState(false); const [editForm, setEditForm] = useState({ role: '', isBanned: false }); // 搜索防抖 useEffect(() => { const timer = setTimeout(() => { fetchUsers(); }, 500); return () => clearTimeout(timer); }, [search, page]); const fetchUsers = async () => { setLoading(true); try { const res = await fetch(`/api/admin/users?page=${page}&limit=10&search=${search}`); const data = await res.json(); if (res.ok) { setUsers(data.users); setTotalPages(data.totalPages); setTotalUsers(data.total); } } catch (error) { console.error('Failed to fetch users', error); } finally { setLoading(false); } }; const handleEditClick = (user: User) => { setEditingUser(user); setEditForm({ role: user.角色, isBanned: user.是否被封禁 }); setIsEditDialogOpen(true); }; const handleUpdateUser = async () => { if (!editingUser) return; try { const res = await fetch(`/api/admin/users/${editingUser._id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(editForm), }); if (res.ok) { setIsEditDialogOpen(false); fetchUsers(); // 刷新列表 } else { alert('更新失败'); } } catch (error) { console.error('Update failed', error); } }; const handleDeleteUser = async (id: string) => { if (!confirm('确定要删除该用户吗?此操作不可恢复。')) return; try { const res = await fetch(`/api/admin/users/${id}`, { method: 'DELETE', }); if (res.ok) { fetchUsers(); } else { const data = await res.json(); alert(data.message || '删除失败'); } } catch (error) { console.error('Delete failed', error); } }; return (

用户管理

管理系统用户,查看详情、修改角色或封禁账号。

{/* */}
setSearch(e.target.value)} />
头像 用户名 邮箱 角色 状态 注册时间 操作 {loading ? ( ) : users.length === 0 ? ( 暂无用户 ) : ( users.map((user) => ( {user.用户名.slice(0, 2).toUpperCase()} {user.用户名} {user.邮箱} {user.角色} {user.是否被封禁 ? ( 已封禁 ) : ( 正常 )} {new Date(user.createdAt).toLocaleDateString()} 操作 handleEditClick(user)}> 编辑用户 handleDeleteUser(user._id)} > 删除用户 )) )}
{/* 分页 */}
共 {totalUsers} 条记录,当前第 {page} / {totalPages} 页
{/* 编辑弹窗 */} 编辑用户 修改用户 {editingUser?.用户名} 的信息。
); }