优化章节读取逻辑,优先从数据库获取章节数据并处理最新的 isFree 状态;改进用户行为轨迹和绑定关系加载,增加错误处理和用户提示。

This commit is contained in:
乘风
2026-01-31 11:42:49 +08:00
parent e21837bf47
commit 77a1c87678
10 changed files with 3384 additions and 15 deletions

View File

@@ -2,6 +2,7 @@ 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 }>
@@ -10,6 +11,35 @@ interface ReadPageProps {
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
@@ -29,7 +59,17 @@ export default async function ReadPage({ params }: ReadPageProps) {
}
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)
@@ -38,7 +78,7 @@ export default async function ReadPage({ params }: ReadPageProps) {
}
}
// 再从book-data获取
// 最后从 book-data 获取
const bookSection = getSectionById(id)
if (bookSection) {
return <ChapterContent section={bookSection as any} partTitle="" chapterTitle="" />