- 后端: users表新增VIP字段, 4个VIP API (purchase/status/profile/members) - 后端: hot接口改按user_tracks阅读量排序 - 后端: orders表支持vip产品类型, migrate新增vip_fields迁移 - 小程序「我的」: 推广中心改为我的收益, 头像VIP标识, VIP入口卡片 - 小程序「我的」: 最近阅读显示真实章节名称 - 小程序首页: 去掉内容概览, 新增创业老板排行(4列网格) - 小程序首页: 精选推荐从hot接口获取, goToRead增加track记录 - 新增页面: VIP详情页, 会员详情页 - 开发文档精简为10个标准目录, 创建SKILL.md, 需求日志规范化 Co-authored-by: Cursor <cursoragent@cursor.com>
87 lines
4.0 KiB
TypeScript
87 lines
4.0 KiB
TypeScript
/**
|
||
* 热门章节API
|
||
* 按阅读量(user_tracks view_chapter)排序
|
||
*/
|
||
import { NextResponse } from 'next/server'
|
||
import { query } from '@/lib/db'
|
||
|
||
const DEFAULT_CHAPTERS = [
|
||
{ id: '1.1', title: '荷包:电动车出租的被动收入模式', tag: '免费', tagClass: 'tag-free', part: '真实的人', views: 0 },
|
||
{ id: '9.12', title: '美业整合:一个人的公司如何月入十万', tag: '热门', tagClass: 'tag-pink', part: '真实的赚钱', views: 0 },
|
||
{ id: '3.1', title: '3000万流水如何跑出来', tag: '热门', tagClass: 'tag-pink', part: '真实的行业', views: 0 },
|
||
{ id: '8.1', title: '流量杠杆:抖音、Soul、飞书', tag: '推荐', tagClass: 'tag-purple', part: '真实的赚钱', views: 0 },
|
||
{ id: '9.13', title: 'AI工具推广:一个隐藏的高利润赛道', tag: '最新', tagClass: 'tag-green', part: '真实的赚钱', views: 0 },
|
||
]
|
||
|
||
const SECTION_INFO: Record<string, any> = {
|
||
'1.1': { title: '荷包:电动车出租的被动收入模式', part: '真实的人', tag: '免费', tagClass: 'tag-free' },
|
||
'1.2': { title: '老墨:资源整合高手的社交方法', part: '真实的人', tag: '推荐', tagClass: 'tag-purple' },
|
||
'2.1': { title: '电商的底层逻辑', part: '真实的行业', tag: '推荐', tagClass: 'tag-purple' },
|
||
'3.1': { title: '3000万流水如何跑出来', part: '真实的行业', tag: '热门', tagClass: 'tag-pink' },
|
||
'4.1': { title: '我的第一次创业失败', part: '真实的错误', tag: '热门', tagClass: 'tag-pink' },
|
||
'5.1': { title: '未来职业的三个方向', part: '真实的社会', tag: '推荐', tagClass: 'tag-purple' },
|
||
'8.1': { title: '流量杠杆:抖音、Soul、飞书', part: '真实的赚钱', tag: '推荐', tagClass: 'tag-purple' },
|
||
'9.12': { title: '美业整合:一个人的公司如何月入十万', part: '真实的赚钱', tag: '热门', tagClass: 'tag-pink' },
|
||
'9.13': { title: 'AI工具推广:一个隐藏的高利润赛道', part: '真实的赚钱', tag: '最新', tagClass: 'tag-green' },
|
||
'9.14': { title: '大健康私域:一个月150万的70后', part: '真实的赚钱', tag: '热门', tagClass: 'tag-pink' },
|
||
'9.15': { title: '本地同城运营拿150万投资', part: '真实的赚钱', tag: '热门', tagClass: 'tag-pink' },
|
||
}
|
||
|
||
export async function GET() {
|
||
try {
|
||
let hotChapters: any[] = []
|
||
|
||
try {
|
||
// 按 user_tracks 的 view_chapter 阅读量排序
|
||
const rows = await query(`
|
||
SELECT chapter_id as id, COUNT(*) as view_count
|
||
FROM user_tracks
|
||
WHERE action = 'view_chapter' AND chapter_id IS NOT NULL AND chapter_id != ''
|
||
GROUP BY chapter_id
|
||
ORDER BY view_count DESC
|
||
LIMIT 10
|
||
`) as any[]
|
||
|
||
if (rows?.length) {
|
||
hotChapters = rows.map((row: any) => {
|
||
const info = SECTION_INFO[row.id] || {}
|
||
return {
|
||
id: row.id,
|
||
title: info.title || `章节 ${row.id}`,
|
||
part: info.part || '',
|
||
tag: info.tag || '热门',
|
||
tagClass: info.tagClass || 'tag-pink',
|
||
views: row.view_count
|
||
}
|
||
})
|
||
}
|
||
} catch (e) {
|
||
console.log('[Hot] user_tracks查询失败,尝试订单统计')
|
||
// 降级:从订单表统计
|
||
try {
|
||
const rows = await query(`
|
||
SELECT product_id as id, COUNT(*) as purchase_count
|
||
FROM orders WHERE status = 'paid' AND product_id IS NOT NULL
|
||
GROUP BY product_id ORDER BY purchase_count DESC LIMIT 10
|
||
`) as any[]
|
||
if (rows?.length) {
|
||
hotChapters = rows.map((row: any) => ({
|
||
id: row.id,
|
||
...(SECTION_INFO[row.id] || { title: `章节 ${row.id}`, part: '', tag: '热门', tagClass: 'tag-pink' }),
|
||
views: row.purchase_count
|
||
}))
|
||
}
|
||
} catch { /* 使用默认 */ }
|
||
}
|
||
|
||
if (!hotChapters.length) {
|
||
hotChapters = DEFAULT_CHAPTERS
|
||
}
|
||
|
||
return NextResponse.json({ success: true, chapters: hotChapters })
|
||
} catch (error) {
|
||
console.error('[Hot] Error:', error)
|
||
return NextResponse.json({ success: true, chapters: DEFAULT_CHAPTERS })
|
||
}
|
||
}
|