"use client" import { MainNav } from "@/components/main-nav" import { SearchBar } from "@/components/search-bar" import { ThemeToggle } from "@/components/theme-toggle" import { ShoppingCart } from "@/components/shopping-cart" import { UserNav } from "@/components/user-nav" import { Button } from "@/components/ui/button" import { Card, CardContent } from "@/components/ui/card" import { Separator } from "@/components/ui/separator" import { useToast } from "@/components/ui/use-toast" import { useCart } from "@/context/cart-context" import { BookOpen, Calendar, DollarSign, MinusCircle, PlusCircle, ShoppingCartIcon as CartIcon, Heart, } from "lucide-react" import { useEffect, useState, use } from "react" import { fetchWithAuth } from "@/lib/api" import { motion } from "framer-motion" import { notify } from "@/lib/event-bus" import { Badge } from "@/components/ui/badge" interface BookDetail { bookId: number title: string isbn: string price: number stock: number publishDate: string publisherName: string description: string coverImage: string author: string[] } export default function BookDetailPage({ params }: { params: Promise<{ id: string }> | { id: string } }) { // 使用 React.use() 解包 params const resolvedParams = "then" in params ? use(params) : params const bookId = resolvedParams.id const [book, setBook] = useState(null) const [loading, setLoading] = useState(true) const [quantity, setQuantity] = useState(1) const [isFavorite, setIsFavorite] = useState(false) const { toast } = useToast() const { addToCart } = useCart() useEffect(() => { const fetchBook = async () => { try { const response = await fetchWithAuth(`book/${bookId}`) const result = await response.json() if (result.code === 0) { setBook(result.data) } else { toast({ variant: "destructive", title: "获取图书失败", description: result.msg || "无法获取图书信息", }) } } catch (error) { toast({ variant: "destructive", title: "获取图书失败", description: "服务器连接错误,请稍后再试", }) } finally { setLoading(false) } } fetchBook() }, [bookId, toast]) const handleAddToCart = () => { if (book) { addToCart({ id: book.bookId, title: book.title, price: book.price, quantity, coverImage: book.coverImage || "/placeholder.svg?height=100&width=80", }) notify({ title: "已添加到购物车", message: `${book.title} x ${quantity}`, type: "success", duration: 3000, }) } } const increaseQuantity = () => { if (book && quantity < book.stock) { setQuantity(quantity + 1) } } const decreaseQuantity = () => { if (quantity > 1) { setQuantity(quantity - 1) } } const toggleFavorite = () => { setIsFavorite(!isFavorite) notify({ title: isFavorite ? "已移除收藏" : "已添加收藏", message: book?.title || "", type: "info", duration: 2000, }) } if (loading) { return (
) } if (!book) { return (

图书不存在

该图书可能已被删除或移动

) } return (
{book.title}

{book.title}

作者: {book.author.join(", ")}

¥{book.price.toFixed(2)}
ISBN: {book.isbn}
出版日期: {new Date(book.publishDate).toLocaleDateString()}
出版社: {book.publisherName}
0 ? "text-green-600 dark:text-green-400" : "text-red-600 dark:text-red-400"} > {book.stock > 0 ? `库存: ${book.stock}` : "缺货"}
{quantity}

图书简介

{book.description || "暂无简介"}

) }