feat: 我的页整合扫一扫/设置与提现、all-chapters去重、内容上传API、文档与后台登录
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -17,23 +17,30 @@ export async function GET() {
|
||||
`) as any[]
|
||||
|
||||
if (dbChapters && dbChapters.length > 0) {
|
||||
console.log('[All Chapters API] 从数据库读取成功,共', dbChapters.length, '章')
|
||||
console.log('[All Chapters API] 从数据库读取成功,共', dbChapters.length, '条')
|
||||
|
||||
// 格式化数据
|
||||
const allChapters = dbChapters.map((chapter: any) => ({
|
||||
id: chapter.id,
|
||||
sectionId: chapter.section_id,
|
||||
title: chapter.title,
|
||||
sectionTitle: chapter.section_title,
|
||||
content: chapter.content,
|
||||
isFree: !!chapter.is_free,
|
||||
price: chapter.price || 0,
|
||||
words: chapter.words || Math.floor(Math.random() * 3000) + 2000,
|
||||
sectionOrder: chapter.section_order,
|
||||
chapterOrder: chapter.chapter_order,
|
||||
createdAt: chapter.created_at,
|
||||
updatedAt: chapter.updated_at
|
||||
}))
|
||||
// 格式化并按 id 去重(保留首次出现)
|
||||
const seen = new Set<string>()
|
||||
const allChapters = dbChapters
|
||||
.map((chapter: any) => ({
|
||||
id: chapter.id,
|
||||
sectionId: chapter.section_id ?? chapter.id,
|
||||
title: chapter.title ?? chapter.section_title,
|
||||
sectionTitle: chapter.section_title ?? chapter.title,
|
||||
content: chapter.content,
|
||||
isFree: !!chapter.is_free,
|
||||
price: chapter.price || 0,
|
||||
words: chapter.words || Math.floor(Math.random() * 3000) + 2000,
|
||||
sectionOrder: chapter.section_order,
|
||||
chapterOrder: chapter.chapter_order,
|
||||
createdAt: chapter.created_at,
|
||||
updatedAt: chapter.updated_at
|
||||
}))
|
||||
.filter((row: { id: string }) => {
|
||||
if (seen.has(row.id)) return false
|
||||
seen.add(row.id)
|
||||
return true
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
@@ -44,7 +51,51 @@ export async function GET() {
|
||||
})
|
||||
}
|
||||
} catch (dbError) {
|
||||
console.log('[All Chapters API] 数据库读取失败,尝试文件读取:', (dbError as Error).message)
|
||||
console.log('[All Chapters API] sections 表读取失败,尝试 chapters 表:', (dbError as Error).message)
|
||||
}
|
||||
|
||||
// 方案1b: 从 chapters 表读取(与 lib/db 表结构一致)
|
||||
try {
|
||||
const dbChapters = await query(`
|
||||
SELECT id, part_id, part_title, chapter_id, chapter_title, section_title, content,
|
||||
is_free, price, word_count, sort_order, created_at, updated_at
|
||||
FROM chapters
|
||||
ORDER BY sort_order ASC, id ASC
|
||||
`) as any[]
|
||||
|
||||
if (dbChapters && dbChapters.length > 0) {
|
||||
console.log('[All Chapters API] 从 chapters 表读取成功,共', dbChapters.length, '条')
|
||||
const seen = new Set<string>()
|
||||
const allChapters = dbChapters
|
||||
.map((row: any) => ({
|
||||
id: row.id,
|
||||
sectionId: row.id,
|
||||
title: row.section_title,
|
||||
sectionTitle: row.section_title,
|
||||
content: row.content,
|
||||
isFree: !!row.is_free,
|
||||
price: row.price || 0,
|
||||
words: row.word_count || 0,
|
||||
sectionOrder: row.sort_order ?? 0,
|
||||
chapterOrder: 0,
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at
|
||||
}))
|
||||
.filter((row: { id: string }) => {
|
||||
if (seen.has(row.id)) return false
|
||||
seen.add(row.id)
|
||||
return true
|
||||
})
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: allChapters,
|
||||
chapters: allChapters,
|
||||
total: allChapters.length,
|
||||
source: 'database'
|
||||
})
|
||||
}
|
||||
} catch (e2) {
|
||||
console.log('[All Chapters API] chapters 表读取失败,尝试文件:', (e2 as Error).message)
|
||||
}
|
||||
|
||||
// 方案2: 从JSON文件读取
|
||||
@@ -72,11 +123,20 @@ export async function GET() {
|
||||
}
|
||||
|
||||
if (chaptersData.length > 0) {
|
||||
// 添加字数估算
|
||||
const allChapters = chaptersData.map((chapter: any) => ({
|
||||
...chapter,
|
||||
words: chapter.words || Math.floor(Math.random() * 3000) + 2000
|
||||
}))
|
||||
// 添加字数估算并按 id 去重
|
||||
const seen = new Set<string>()
|
||||
const allChapters = chaptersData
|
||||
.map((chapter: any) => ({
|
||||
...chapter,
|
||||
id: chapter.id ?? chapter.sectionId,
|
||||
words: chapter.words || Math.floor(Math.random() * 3000) + 2000
|
||||
}))
|
||||
.filter((row: any) => {
|
||||
const id = row.id || row.sectionId
|
||||
if (!id || seen.has(String(id))) return false
|
||||
seen.add(String(id))
|
||||
return true
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
|
||||
Reference in New Issue
Block a user