93 lines
2.8 KiB
TypeScript
93 lines
2.8 KiB
TypeScript
import { notFound } from "next/navigation"
|
|
import { ChapterContent } from "@/components/chapter-content"
|
|
import { getSectionBySlug, getChapterBySectionSlug } from "@/lib/book-file-system"
|
|
import { specialSections, getSectionById } from "@/lib/book-data"
|
|
import { query } from "@/lib/db"
|
|
|
|
interface ReadPageProps {
|
|
params: Promise<{ id: string }>
|
|
}
|
|
|
|
export const dynamic = "force-dynamic"
|
|
export const runtime = "nodejs"
|
|
|
|
// 从数据库获取章节数据(包含最新的 isFree 状态)
|
|
async function getChapterFromDB(id: string) {
|
|
try {
|
|
const results = await query(
|
|
`SELECT id, part_title, chapter_title, section_title, content, is_free, price
|
|
FROM chapters
|
|
WHERE id = ? AND status = 'published'`,
|
|
[id]
|
|
) as any[]
|
|
|
|
if (results && results.length > 0) {
|
|
const chapter = results[0]
|
|
return {
|
|
id: chapter.id,
|
|
title: chapter.section_title,
|
|
price: chapter.price || 1,
|
|
isFree: chapter.is_free === 1 || chapter.price === 0,
|
|
filePath: '',
|
|
content: chapter.content,
|
|
partTitle: chapter.part_title,
|
|
chapterTitle: chapter.chapter_title,
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error("[ReadPage] 从数据库获取章节失败:", error)
|
|
}
|
|
return null
|
|
}
|
|
|
|
export default async function ReadPage({ params }: ReadPageProps) {
|
|
const { id } = await params
|
|
|
|
if (id === "preface") {
|
|
return <ChapterContent section={specialSections.preface as any} partTitle="序言" chapterTitle="" />
|
|
}
|
|
|
|
if (id === "epilogue") {
|
|
return <ChapterContent section={specialSections.epilogue as any} partTitle="尾声" chapterTitle="" />
|
|
}
|
|
|
|
if (id.startsWith("appendix-")) {
|
|
const appendixSection = specialSections.appendix.find((a) => a.id === id)
|
|
if (appendixSection) {
|
|
return <ChapterContent section={appendixSection as any} partTitle="附录" chapterTitle="" />
|
|
}
|
|
}
|
|
|
|
try {
|
|
// 🔥 优先从数据库获取(包含最新的 isFree 状态)
|
|
const dbChapter = await getChapterFromDB(id)
|
|
if (dbChapter) {
|
|
return <ChapterContent
|
|
section={dbChapter as any}
|
|
partTitle={dbChapter.partTitle || ""}
|
|
chapterTitle={dbChapter.chapterTitle || ""}
|
|
/>
|
|
}
|
|
|
|
// 如果数据库没有,再从文件系统获取(兼容旧数据)
|
|
const section = getSectionBySlug(id)
|
|
if (section) {
|
|
const context = getChapterBySectionSlug(id)
|
|
if (context) {
|
|
return <ChapterContent section={section} partTitle={context.part.title} chapterTitle={context.chapter.title} />
|
|
}
|
|
}
|
|
|
|
// 最后从 book-data 获取
|
|
const bookSection = getSectionById(id)
|
|
if (bookSection) {
|
|
return <ChapterContent section={bookSection as any} partTitle="" chapterTitle="" />
|
|
}
|
|
|
|
notFound()
|
|
} catch (error) {
|
|
console.error("[Karuo] Error in ReadPage:", error)
|
|
notFound()
|
|
}
|
|
}
|