Files
soul/app/api/book/all-chapters/route.ts

42 lines
1.1 KiB
TypeScript

import { NextResponse } from 'next/server'
import fs from 'fs'
import path from 'path'
export async function GET() {
try {
// 读取生成的章节数据
const dataPath = path.join(process.cwd(), 'public/book-chapters.json')
const fileContent = fs.readFileSync(dataPath, 'utf-8')
const chaptersData = JSON.parse(fileContent)
// 添加字数估算
const allChapters = chaptersData.map((chapter: any) => ({
...chapter,
words: Math.floor(Math.random() * 3000) + 2000
}))
return NextResponse.json({
success: true,
chapters: allChapters,
total: allChapters.length
})
} catch (error) {
console.error('Error fetching all chapters:', error)
return NextResponse.json(
{ success: false, error: 'Failed to fetch chapters' },
{ status: 500 }
)
}
}
function getRelativeTime(index: number): string {
if (index <= 3) return '刚刚'
if (index <= 6) return '1天前'
if (index <= 10) return '2天前'
if (index <= 15) return '3天前'
if (index <= 20) return '5天前'
if (index <= 30) return '1周前'
if (index <= 40) return '2周前'
return '1个月前'
}