Files
soul-yongping/app/api/vip/purchase/route.ts
卡若 afc2376e96 v1.19 全面改版:VIP会员系统、我的收益、创业老板排行、阅读量排序
- 后端: 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>
2026-02-23 14:07:41 +08:00

58 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* VIP会员购买 - 创建VIP订单
*/
import { NextRequest, NextResponse } from 'next/server'
import { query, getConfig } from '@/lib/db'
export async function POST(request: NextRequest) {
try {
const { userId } = await request.json()
if (!userId) {
return NextResponse.json({ success: false, error: '缺少userId' }, { status: 400 })
}
const users = await query(
'SELECT id, open_id, is_vip, vip_expire_date FROM users WHERE id = ?',
[userId]
) as any[]
if (!users.length) {
return NextResponse.json({ success: false, error: '用户不存在' }, { status: 404 })
}
const user = users[0]
// 如果已经是VIP且未过期
if (user.is_vip && user.vip_expire_date && new Date(user.vip_expire_date) > new Date()) {
return NextResponse.json({ success: false, error: '当前已是VIP会员' }, { status: 400 })
}
let vipPrice = 1980
try {
const config = await getConfig('vip_price')
if (config) vipPrice = Number(config) || 1980
} catch { /* 默认 */ }
const orderId = 'vip_' + Date.now().toString(36) + Math.random().toString(36).substr(2, 6)
const orderSn = 'VIP' + Date.now() + Math.floor(Math.random() * 1000)
await query(
`INSERT INTO orders (id, order_sn, user_id, open_id, product_type, amount, description, status)
VALUES (?, ?, ?, ?, 'vip', ?, 'VIP年度会员', 'created')`,
[orderId, orderSn, userId, user.open_id || '', vipPrice]
)
return NextResponse.json({
success: true,
data: {
orderId,
orderSn,
amount: vipPrice,
productType: 'vip',
description: 'VIP年度会员365天'
}
})
} catch (error) {
console.error('[VIP Purchase]', error)
return NextResponse.json({ success: false, error: '创建订单失败' }, { status: 500 })
}
}