优化小程序支付流程,新增订单插入逻辑,确保支付成功后更新订单状态并处理佣金分配。同时,重构阅读页面,增强权限管理和阅读追踪功能,提升用户体验。

This commit is contained in:
乘风
2026-02-04 21:36:26 +08:00
parent 25fd3190b2
commit 67ef87095f
48 changed files with 9619 additions and 1218 deletions

View File

@@ -14,6 +14,7 @@ import {
getNotifyUrl,
getReturnUrl,
} from "@/lib/payment"
import { query } from "@/lib/db"
// 确保网关已注册
import "@/lib/payment/alipay"
@@ -52,6 +53,50 @@ export async function POST(request: NextRequest) {
expireAt: new Date(Date.now() + 30 * 60 * 1000).toISOString(), // 30分钟
}
// === 💾 插入订单到数据库 ===
try {
// 获取用户 openId如果有
let openId = null
try {
const userRows = await query('SELECT open_id FROM users WHERE id = ?', [userId]) as any[]
if (userRows.length > 0) {
openId = userRows[0].open_id
}
} catch (e) {
console.warn('[Payment] 获取 openId 失败:', e)
}
const productType = type === 'section' ? 'section' : 'fullbook'
const productId = type === 'section' ? sectionId : 'fullbook'
const description = type === 'section'
? `购买章节: ${sectionTitle}`
: '购买整本书'
await query(`
INSERT INTO orders (
id, order_sn, user_id, open_id,
product_type, product_id, amount, description,
status, transaction_id, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
`, [
orderSn, // id
orderSn, // order_sn
userId, // user_id
openId, // open_id
productType, // product_type
productId, // product_id
amount, // amount
description, // description
'created', // status
tradeSn // transaction_id支付流水号
])
console.log('[Payment] ✅ 订单已插入数据库:', { orderSn, userId, amount })
} catch (dbError) {
console.error('[Payment] ❌ 插入订单失败:', dbError)
// 不中断流程,继续返回支付信息
}
// 获取客户端IP
const clientIp = request.headers.get("x-forwarded-for")
|| request.headers.get("x-real-ip")