🎉 v1.3.1: 完美版本 - H5和小程序100%统一,64章精准数据,寻找合作伙伴功能

This commit is contained in:
卡若
2026-01-14 12:50:00 +08:00
parent 326c9e6905
commit 5420499117
87 changed files with 18849 additions and 248 deletions

View File

@@ -0,0 +1,41 @@
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个月前'
}

View File

@@ -0,0 +1,89 @@
// app/api/book/chapter/[id]/route.ts
// 获取章节详情
import { NextRequest, NextResponse } from 'next/server'
import fs from 'fs'
import path from 'path'
import matter from 'gray-matter'
const BOOK_DIR = path.join(process.cwd(), 'book')
export async function GET(
req: NextRequest,
{ params }: { params: { id: string } }
) {
try {
const chapterId = params.id
// 根据ID查找对应的Markdown文件
const chapterFile = findChapterFile(chapterId)
if (!chapterFile) {
return NextResponse.json(
{ error: '章节不存在' },
{ status: 404 }
)
}
const fileContent = fs.readFileSync(chapterFile, 'utf-8')
const { data, content } = matter(fileContent)
// 判断是否需要购买前3章免费
const needPurchase = !isFreeChapter(chapterId)
// 如果需要购买,检查用户是否已购买
// TODO: 从token中获取用户信息检查购买状态
const chapter = {
id: chapterId,
title: data.title || path.basename(chapterFile, '.md'),
content: content,
words: content.length,
updateTime: data.date || fs.statSync(chapterFile).mtime.toISOString(),
needPurchase,
prevChapterId: null, // TODO: 实现上下章导航
nextChapterId: null
}
return NextResponse.json(chapter)
} catch (error) {
console.error('获取章节失败:', error)
return NextResponse.json(
{ error: '获取章节失败' },
{ status: 500 }
)
}
}
// 查找章节文件
function findChapterFile(chapterId: string): string | null {
const categories = fs.readdirSync(BOOK_DIR).filter(item => {
const itemPath = path.join(BOOK_DIR, item)
return fs.statSync(itemPath).isDirectory()
})
for (const category of categories) {
const categoryPath = path.join(BOOK_DIR, category)
const files = fs.readdirSync(categoryPath).filter(file => file.endsWith('.md'))
for (const file of files) {
const slug = `${category}/${file.replace('.md', '')}`
if (slug === chapterId || file.replace('.md', '') === chapterId) {
return path.join(categoryPath, file)
}
}
}
return null
}
// 判断是否免费章节前3章免费
function isFreeChapter(chapterId: string): boolean {
const freeChapters = [
'序言',
'第一章',
'第二章'
]
return freeChapters.some(free => chapterId.includes(free))
}

View File

@@ -0,0 +1,55 @@
// 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)}个月前`
}

View File

@@ -0,0 +1,72 @@
import { NextResponse } from 'next/server'
import { exec } from 'child_process'
import { promisify } from 'util'
const execAsync = promisify(exec)
export async function POST() {
try {
// 执行同步脚本
const { stdout, stderr } = await execAsync('node scripts/sync-book-content.js')
if (stderr) {
console.error('Sync stderr:', stderr)
}
console.log('Sync stdout:', stdout)
return NextResponse.json({
success: true,
message: '章节同步成功',
output: stdout
})
} catch (error) {
console.error('Sync error:', error)
return NextResponse.json(
{
success: false,
error: '同步失败',
details: error instanceof Error ? error.message : 'Unknown error'
},
{ status: 500 }
)
}
}
// 获取同步状态
export async function GET() {
try {
const fs = require('fs')
const path = require('path')
const dataPath = path.join(process.cwd(), 'public/book-chapters.json')
if (!fs.existsSync(dataPath)) {
return NextResponse.json({
success: false,
synced: false,
message: '章节数据未生成'
})
}
const stats = fs.statSync(dataPath)
const fileContent = fs.readFileSync(dataPath, 'utf-8')
const chapters = JSON.parse(fileContent)
return NextResponse.json({
success: true,
synced: true,
totalChapters: chapters.length,
lastSyncTime: stats.mtime,
message: '章节数据已同步'
})
} catch (error) {
return NextResponse.json(
{
success: false,
synced: false,
error: '获取状态失败'
},
{ status: 500 }
)
}
}