'use client' import { useState, useEffect } from 'react' import Link from 'next/link' import { Users, Package, ShoppingCart, DollarSign, TrendingUp, Eye, BarChart3, Settings, Plus } from 'lucide-react' import { BarChart, Bar, XAxis, YAxis, CartesianGrid, Tooltip, ResponsiveContainer, PieChart, Pie, Cell } from 'recharts' interface StatsData { overview: { totalUsers: number totalOrders: number totalComponents: number totalRevenue: number } topUsers: Array<{ id: string username: string name: string email: string totalSpending: number orderCount: number }> topComponents: Array<{ id: string name: string brand: string price: number typeName: string totalSales: number totalRevenue: number }> recentOrders: Array<{ id: string orderNumber: string totalAmount: number status: string createdAt: string user: { username: string name?: string } orderItems: Array<{ component: { name: string } }> }> } const COLORS = ['#3B82F6', '#EF4444', '#10B981', '#F59E0B', '#8B5CF6', '#EC4899'] export default function AdminDashboard() { const [stats, setStats] = useState(null) const [isLoading, setIsLoading] = useState(true) const [activeTab, setActiveTab] = useState('overview') useEffect(() => { checkAdminAccess() loadStats() }, []) const checkAdminAccess = () => { const user = JSON.parse(localStorage.getItem('user') || 'null') if (!user || !user.isAdmin) { alert('权限不足,需要管理员权限') window.location.href = '/' return } } const loadStats = async () => { try { const token = localStorage.getItem('token') const response = await fetch('/api/admin/stats', { headers: { 'Authorization': `Bearer ${token}` } }) if (response.ok) { const data = await response.json() setStats(data) } else if (response.status === 403) { alert('权限不足') window.location.href = '/' } } catch (error) { console.error('加载统计数据失败:', error) } finally { setIsLoading(false) } } if (isLoading) { return (

加载中...

) } return (
{/* Header */}

管理后台

系统管理与数据统计

添加商品 查看订单
{/* Overview Cards */}

总用户数

{stats?.overview.totalUsers || 0}

总订单数

{stats?.overview.totalOrders || 0}

商品总数

{stats?.overview.totalComponents || 0}

总营收

¥{stats?.overview.totalRevenue?.toFixed(2) || '0.00'}

{/* Tab Navigation */}
{/* Tab Content */} {activeTab === 'overview' && (
{/* Top Users Chart */}

用户消费金额排行

[`¥${value}`, '消费金额']} />
{/* Top Components Chart */}

商品销量排行

[`${value}件`, '销量']} />
)} {activeTab === 'users' && (

用户消费排行榜

{stats?.topUsers.map((user, index) => ( ))}
排名 用户 邮箱 订单数 消费金额
#{index + 1} {user.name} {user.email} {user.orderCount} ¥{user.totalSpending.toFixed(2)}
)} {activeTab === 'products' && (

商品销量排行榜

{stats?.topComponents.map((component, index) => ( ))}
排名 商品 品牌 类型 销量 营收
#{index + 1} {component.name} {component.brand} {component.typeName} {component.totalSales} ¥{component.totalRevenue.toFixed(2)}
)} {activeTab === 'orders' && (

最近订单

{stats?.recentOrders.map((order) => ( ))}
订单号 用户 商品数量 订单金额 状态 创建时间
{order.orderNumber} {order.user.name || order.user.username} {order.orderItems.length} 件 ¥{order.totalAmount} {order.status} {new Date(order.createdAt).toLocaleDateString('zh-CN')}
)}
) }