"use client" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import { ChevronLeft, Lock, Share2, Sparkles, ChevronRight, X, Copy, Check, QrCode } from "lucide-react" import { type Section, getFullBookPrice, getTotalSectionCount, getNextSection, getPrevSection } from "@/lib/book-data" import { useStore } from "@/lib/store" import { PaymentModal } from "./payment-modal" import { AuthModal } from "./modules/auth/auth-modal" interface ChapterContentProps { section: Section & { filePath: string } partTitle: string chapterTitle: string } export function ChapterContent({ section, partTitle, chapterTitle }: ChapterContentProps) { const router = useRouter() const [content, setContent] = useState("") const [isLoading, setIsLoading] = useState(true) const [isPaymentOpen, setIsPaymentOpen] = useState(false) const [isAuthOpen, setIsAuthOpen] = useState(false) const [paymentType, setPaymentType] = useState<"section" | "fullbook">("section") const [readingProgress, setReadingProgress] = useState(0) const [showPaywall, setShowPaywall] = useState(false) const [showShareModal, setShowShareModal] = useState(false) const [shareCopied, setShareCopied] = useState(false) const { user, isLoggedIn, hasPurchased, settings } = useStore() const fullBookPrice = getFullBookPrice() const totalSections = getTotalSectionCount() const hasFullBook = user?.hasFullBook || false const canAccess = section.isFree || hasFullBook || (isLoggedIn && hasPurchased(section.id)) // 获取下一篇和上一篇 const nextSection = getNextSection(section.id) const prevSection = getPrevSection(section.id) // 生成分享链接(带用户邀请码) const getShareLink = () => { const baseUrl = typeof window !== 'undefined' ? window.location.origin : '' const referralCode = user?.referralCode || '' const shareUrl = `${baseUrl}/read/${section.id}${referralCode ? `?ref=${referralCode}` : ''}` return shareUrl } // 生成小程序路径 const getMiniProgramPath = () => { const referralCode = user?.referralCode || '' return `/pages/read/read?id=${section.id}${referralCode ? `&ref=${referralCode}` : ''}` } // 如果没有访问权限,直接显示付费墙 useEffect(() => { if (!canAccess && !isLoading) { setShowPaywall(true) } }, [canAccess, isLoading]) // 阅读进度追踪 useEffect(() => { const handleScroll = () => { const scrollTop = window.scrollY const docHeight = document.documentElement.scrollHeight - window.innerHeight const progress = docHeight > 0 ? Math.min((scrollTop / docHeight) * 100, 100) : 0 setReadingProgress(progress) } window.addEventListener("scroll", handleScroll) return () => window.removeEventListener("scroll", handleScroll) }, []) // 加载内容 useEffect(() => { async function loadContent() { try { if (section.content) { setContent(section.content) setIsLoading(false) return } const response = await fetch(`/api/content?path=${encodeURIComponent(section.filePath)}`) if (response.ok) { const data = await response.json() setContent(data.content || "") } } catch (error) { console.error("Failed to load content:", error) } finally { setIsLoading(false) } } loadContent() }, [section.filePath, section.content]) const handlePurchaseClick = (type: "section" | "fullbook") => { if (!isLoggedIn) { setIsAuthOpen(true) return } setPaymentType(type) setIsPaymentOpen(true) } const handleShare = () => { setShowShareModal(true) } const handleCopyLink = () => { const shareLink = getShareLink() navigator.clipboard.writeText(shareLink) setShareCopied(true) setTimeout(() => setShareCopied(false), 2000) } const handleShareToWechat = () => { // 生成微信分享文案 const shareText = `📚 推荐阅读《${section.title}》\n\n${content.slice(0, 100)}...\n\n👉 点击阅读:${getShareLink()}` navigator.clipboard.writeText(shareText) alert('分享文案已复制,请粘贴到微信发送给好友') } const contentLines = content.split("\n").filter((line) => line.trim()) const previewLineCount = Math.ceil(contentLines.length * 0.2) // 改为20% const previewContent = contentLines.slice(0, previewLineCount).join("\n") return (
{/* 顶部导航 */}
{partTitle &&

{partTitle}

} {chapterTitle &&

{chapterTitle}

}
{/* 阅读内容 */}
{section.id} {section.isFree && 免费}

{section.title}

{isLoading ? (
{[75, 90, 65, 85, 70, 95, 80, 88].map((width, i) => (
))}
) : canAccess ? ( // 完整内容 <>
{content.split("\n").map( (paragraph, index) => paragraph.trim() && (

{paragraph}

), )}
{/* 底部章节导航 */}
{prevSection ? ( ) : (
)} {nextSection ? ( ) : (

已是最后一篇 🎉

)}
{/* 分享提示 */}

觉得不错?分享给好友

好友购买你获得90%佣金

) : (
{/* 免费预览部分 */}
{previewContent.split("\n").map( (paragraph, index) => paragraph.trim() && (

{paragraph}

), )}
{showPaywall && ( <> {/* 渐变遮罩 */}

解锁完整内容

已阅读20%,{isLoggedIn ? "购买后继续阅读" : "登录并购买后继续阅读"}

{/* 购买选项 */}
{/* 只有购买超过3章才显示全书购买选项 */} {(user?.purchasedSections?.length || 0) >= 3 && ( )}

分享给好友购买,你可获得90%佣金

)}
)}
{/* 分享弹窗 */} {showShareModal && (
setShowShareModal(false)} />

分享文章

{/* 分享链接预览 */}

你的专属分享链接

{getShareLink()}

{user?.referralCode && (

邀请码: {user.referralCode} · 好友购买你获得90%佣金

)}
{/* 分享按钮 */}
{/* 小程序路径(开发者调试用) */} {user?.isAdmin && (

小程序路径

{getMiniProgramPath()}

)}
)} {/* 登录弹窗 */} setIsAuthOpen(false)} /> {/* 支付弹窗 */} setIsPaymentOpen(false)} type={paymentType} sectionId={section.id} sectionTitle={section.title} amount={paymentType === "section" ? section.price : fullBookPrice} onSuccess={() => { setIsPaymentOpen(false) // 刷新当前页面以显示解锁内容 window.location.reload() }} />
) }