"use client" import { useState, useEffect, Suspense } from "react" import { Card, CardContent } from "@/components/ui/card" import { Input } from "@/components/ui/input" import { Button } from "@/components/ui/button" import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table" import { Badge } from "@/components/ui/badge" import { Search, RefreshCw, Download, Filter, TrendingUp } from "lucide-react" import { useStore } from "@/lib/store" interface Purchase { id: string userId: string type: "section" | "fullbook" | "match" sectionId?: string sectionTitle?: string amount: number status: "pending" | "completed" | "failed" paymentMethod?: string referrerEarnings?: number createdAt: string } function OrdersContent() { const { getAllPurchases, getAllUsers } = useStore() const [purchases, setPurchases] = useState([]) // 改为 any[] 以支持新字段 const [users, setUsers] = useState([]) const [searchTerm, setSearchTerm] = useState("") const [statusFilter, setStatusFilter] = useState("all") const [isLoading, setIsLoading] = useState(true) // 从API获取订单(包含用户昵称) async function loadOrders() { setIsLoading(true) try { const ordersRes = await fetch('/api/orders') const ordersData = await ordersRes.json() if (ordersData.success && ordersData.orders) { setPurchases(ordersData.orders) } const usersRes = await fetch('/api/db/users') const usersData = await usersRes.json() if (usersData.success && usersData.users) { setUsers(usersData.users) } } catch (e) { console.error('加载订单失败', e) } finally { setIsLoading(false) } } useEffect(() => { loadOrders() }, []) // 获取用户昵称(优先使用 order.userNickname) const getUserNickname = (order: any) => { return order.userNickname || users.find((u: any) => u.id === order.userId)?.nickname || "匿名用户" } // 获取用户手机号 const getUserPhone = (userId: string) => { const user = users.find(u => u.id === userId) return user?.phone || "-" } // 格式化商品信息 const formatProduct = (order: any) => { const type = order.productType || "" const desc = order.description || "" if (desc) { if (type === "section" && desc.includes("章节")) { if (desc.includes("-")) { const parts = desc.split("-") if (parts.length >= 3) { return { name: `第${parts[1]}章 第${parts[2]}节`, type: "《一场Soul的创业实验》" } } } return { name: desc, type: "章节购买" } } if (type === "fullbook" || desc.includes("全书")) { return { name: "《一场Soul的创业实验》", type: "全书购买" } } if (type === "match" || desc.includes("伙伴")) { return { name: "找伙伴匹配", type: "功能服务" } } return { name: desc, type: "其他" } } if (type === "section") return { name: `章节 ${order.productId || ""}`, type: "单章" } if (type === "fullbook") return { name: "《一场Soul的创业实验》", type: "全书" } if (type === "match") return { name: "找伙伴匹配", type: "功能" } return { name: "未知商品", type: type || "其他" } } // 过滤订单 const filteredPurchases = purchases.filter((p) => { const product = formatProduct(p) const matchSearch = getUserNickname(p).includes(searchTerm) || getUserPhone(p.userId).includes(searchTerm) || product.name.includes(searchTerm) || (p.orderSn && p.orderSn.includes(searchTerm)) || (p.id && p.id.includes(searchTerm)) const matchStatus = statusFilter === "all" || p.status === statusFilter || (statusFilter === "completed" && p.status === "paid") return matchSearch && matchStatus }) // 统计数据(status 可能是 'paid' 或 'completed') const totalRevenue = purchases.filter(p => p.status === "paid" || p.status === "completed").reduce((sum, p) => sum + Number(p.amount || 0), 0) const todayRevenue = purchases .filter(p => { const today = new Date().toDateString() return (p.status === "paid" || p.status === "completed") && new Date(p.createdAt).toDateString() === today }) .reduce((sum, p) => sum + Number(p.amount || 0), 0) return (

订单管理

共 {purchases.length} 笔订单

总收入: ¥{totalRevenue.toFixed(2)} | 今日: ¥{todayRevenue.toFixed(2)}
setSearchTerm(e.target.value)} />
{isLoading ? (
加载中...
) : ( 订单号 用户 商品 金额 支付方式 状态 分销佣金 下单时间 {filteredPurchases.map((purchase) => { const product = formatProduct(purchase) return ( {(purchase.orderSn || purchase.id || "").slice(0, 12)}...

{getUserNickname(purchase)}

{getUserPhone(purchase.userId)}

{product.name}

{product.type}

¥{Number(purchase.amount || 0).toFixed(2)} {purchase.paymentMethod === "wechat" ? "微信支付" : purchase.paymentMethod === "alipay" ? "支付宝" : purchase.paymentMethod || "微信支付"} {purchase.status === "paid" || purchase.status === "completed" ? ( 已完成 ) : purchase.status === "pending" || purchase.status === "created" ? ( 待支付 ) : ( 已失败 )} {purchase.referrerEarnings ? `¥${Number(purchase.referrerEarnings).toFixed(2)}` : "-"} {new Date(purchase.createdAt).toLocaleString('zh-CN')}
) })} {filteredPurchases.length === 0 && ( 暂无订单数据 )}
)}
) } export default function OrdersPage() { return ( ) }