56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
// app/api/book/latest-chapters/route.ts
|
|
// 获取最新章节列表
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getBookStructure } from '@/lib/book-file-system'
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const bookStructure = getBookStructure()
|
|
|
|
// 获取所有章节并按时间排序
|
|
const allChapters: any[] = []
|
|
|
|
bookStructure.forEach((part: any) => {
|
|
part.chapters.forEach((chapter: any) => {
|
|
allChapters.push({
|
|
id: chapter.slug,
|
|
title: chapter.title,
|
|
part: part.title,
|
|
words: Math.floor(Math.random() * 3000) + 1500, // 模拟字数
|
|
updateTime: getRelativeTime(new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000)),
|
|
readTime: Math.ceil((Math.random() * 3000 + 1500) / 300)
|
|
})
|
|
})
|
|
})
|
|
|
|
// 取最新的3章
|
|
const latestChapters = allChapters.slice(0, 3)
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
chapters: latestChapters,
|
|
total: allChapters.length
|
|
})
|
|
} catch (error) {
|
|
console.error('获取章节失败:', error)
|
|
return NextResponse.json(
|
|
{ error: '获取章节失败' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// 获取相对时间
|
|
function getRelativeTime(date: Date): string {
|
|
const now = new Date()
|
|
const diff = now.getTime() - date.getTime()
|
|
const days = Math.floor(diff / (1000 * 60 * 60 * 24))
|
|
|
|
if (days === 0) return '今天'
|
|
if (days === 1) return '昨天'
|
|
if (days < 7) return `${days}天前`
|
|
if (days < 30) return `${Math.floor(days / 7)}周前`
|
|
return `${Math.floor(days / 30)}个月前`
|
|
}
|