'use client'; import { useState, useEffect } from 'react'; import { X, Users, Search, Shield, Key, Crown, CheckCircle2, Calendar, Trash2, Receipt } from 'lucide-react'; import { api, AdminUserItem, AdminOrderItem } from '@/lib/api'; import { useAuthStore } from '@/store/authStore'; interface AdminPanelProps { isOpen: boolean; onClose: () => void; } export default function AdminPanel({ isOpen, onClose }: AdminPanelProps) { const { token } = useAuthStore(); const [users, setUsers] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [searchQuery, setSearchQuery] = useState(''); const [activeTab, setActiveTab] = useState<'all' | 'vip' | 'normal'>('all'); // 操作状态 const [actionLoading, setActionLoading] = useState(null); // userId const [successMsg, setSuccessMsg] = useState(null); // 订单查看状态 const [viewingOrdersUser, setViewingOrdersUser] = useState<{ id: number; name: string } | null>(null); const [userOrders, setUserOrders] = useState([]); const [ordersLoading, setOrdersLoading] = useState(false); const fetchUsers = async () => { if (!token) return; setLoading(true); try { const data = await api.adminGetUsers(token); setUsers(data); setError(null); } catch (err: any) { setError(err.message || '获取用户列表失败'); } finally { setLoading(false); } }; useEffect(() => { if (isOpen) { fetchUsers(); } }, [isOpen, token]); // 修改密码 const handleUpdatePassword = async (userId: number, username: string) => { const newPassword = window.prompt(`请输入用户 [${username}] 的新密码:`); if (!newPassword || !token) return; setActionLoading(userId); try { await api.adminUpdatePassword(userId, newPassword, token); setSuccessMsg(`用户 ${username} 密码修改成功`); setTimeout(() => setSuccessMsg(null), 3000); } catch (err: any) { alert('修改失败: ' + err.message); } finally { setActionLoading(null); } }; // 充值会员 const handleUpdateVip = async (userId: number, username: string) => { const monthsStr = window.prompt(`请输入为用户 [${username}] 充值的月数:`, '1'); if (!monthsStr || !token) return; const months = parseInt(monthsStr); if (isNaN(months) || months <= 0) return alert('请输入有效数字'); setActionLoading(userId); try { await api.adminUpdateVip(userId, months, token); setSuccessMsg(`用户 ${username} 充值成功 (+${months}个月)`); fetchUsers(); setTimeout(() => setSuccessMsg(null), 3000); } catch (err: any) { alert('充值失败: ' + err.message); } finally { setActionLoading(null); } }; // 删除用户 const handleDeleteUser = async (userId: number, username: string) => { if (!window.confirm(`⚠️ 警告:确定要彻底删除用户 [${username}] 吗?\n\n此操作将清除该用户的所有数据(VIP、会话、使用记录),且无法撤销。`)) { return; } if (!token) return; setActionLoading(userId); try { await api.adminDeleteUser(userId, token); setSuccessMsg(`用户 ${username} 已被彻底删除`); fetchUsers(); setTimeout(() => setSuccessMsg(null), 3000); } catch (err: any) { alert('删除失败: ' + err.message); } finally { setActionLoading(null); } }; // 获取并查看订单 const handleViewOrders = async (userId: number, username: string) => { setViewingOrdersUser({ id: userId, name: username }); setOrdersLoading(true); try { const data = await api.adminUserOrders(userId, token!); setUserOrders(data); } catch (err: any) { alert('获取订单失败: ' + err.message); setViewingOrdersUser(null); } finally { setOrdersLoading(false); } }; const handleToggleOrderStatus = async (orderId: string, currentStatus: string) => { if (!token) return; const newStatus = currentStatus === 'paid' ? 'pending' : 'paid'; try { await api.adminUpdateOrderStatus(orderId, newStatus, token); // 本地状态更新 setUserOrders(prev => prev.map(o => o.order_id === orderId ? { ...o, status: newStatus, paid_at: newStatus === 'paid' ? new Date().toLocaleString() : null } : o )); } catch (err: any) { alert('切换状态失败: ' + err.message); } }; if (!isOpen) return null; const filteredUsers = users.filter(u => { const matchesSearch = u.username.toLowerCase().includes(searchQuery.toLowerCase()); const matchesTab = activeTab === 'all' || (activeTab === 'vip' && u.is_vip) || (activeTab === 'normal' && !u.is_vip); return matchesSearch && matchesTab; }); return (
{/* Header */}

管理后台

系统用户管理与权限控制

{successMsg && (
{successMsg}
)}
{/* Toolbar */}
{(['all', 'vip', 'normal'] as const).map((tab) => ( ))}
setSearchQuery(e.target.value)} />
{/* Content */}
{loading ? (

数据同步中...

) : (
{filteredUsers.length === 0 ? ( ) : ( filteredUsers.map((user) => ( handleViewOrders(user.user_id, user.username)} > )).reverse() )}
用户详情 会员状态 支付记录 最近登录 注册日期 管理操作

无记录

{user.username[0].toUpperCase()}
{user.username} UID: {user.user_id}
{user.is_vip ? (
会员
{user.vip_expire_at?.split(' ')[0].replace(/-/g, '/') || ''} {user.vip_expire_at?.split(' ')[1] || ''}
) : ( 普通 )}
{user.has_payment ? ( ) : ( )} {user.last_login ? (
{user.last_login.split(' ')[0].replace(/-/g, '/') || ''} {user.last_login.split(' ')[1] || ''}
) : ( 从未登录 )}
{user.created_at.split(' ')[0].replace(/-/g, '/') || ''} {user.created_at.split(' ')[1] || ''}
e.stopPropagation()}>
)}
{/* Footer */}
用户: {users.length}
会员: {users.filter(u => u.is_vip).length}
管理控制台
{/* 订单详情弹窗 */} {viewingOrdersUser && (

支付记录

用户: {viewingOrdersUser.name} (UID: {viewingOrdersUser.id})

{ordersLoading ? (

正在读取记录...

) : userOrders.length === 0 ? (

暂无支付记录

) : (
{userOrders.map((order) => (
订单号 {order.order_id}
金额 / 时长
¥{order.amount} ({order.months}个月)
方式 {order.pay_type === 1 ? '支付宝' : '微信'}
最后更新 {order.paid_at || order.created_at}
))}
)}
)}
); }