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

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

@@ -116,6 +116,27 @@ export async function POST(request: Request) {
}
}
// === ✅ 从 orders 表查询真实购买记录 ===
let purchasedSections: string[] = []
try {
const orderRows = await query(`
SELECT DISTINCT product_id
FROM orders
WHERE user_id = ?
AND status = 'paid'
AND product_type = 'section'
`, [user.id]) as any[]
purchasedSections = orderRows.map((row: any) => row.product_id).filter(Boolean)
console.log('[MiniLogin] 查询到已购章节:', purchasedSections.length, '个')
} catch (e) {
console.warn('[MiniLogin] 查询购买记录失败:', e)
// 降级到 users.purchased_sections 字段
purchasedSections = typeof user.purchased_sections === 'string'
? JSON.parse(user.purchased_sections || '[]')
: (user.purchased_sections || [])
}
// 统一用户数据格式
const responseUser = {
id: user.id,
@@ -126,9 +147,7 @@ export async function POST(request: Request) {
wechatId: user.wechat_id,
referralCode: user.referral_code,
hasFullBook: user.has_full_book || false,
purchasedSections: typeof user.purchased_sections === 'string'
? JSON.parse(user.purchased_sections || '[]')
: (user.purchased_sections || []),
purchasedSections, // ✅ 使用从 orders 表查询的真实数据
earnings: parseFloat(user.earnings) || 0,
pendingEarnings: parseFloat(user.pending_earnings) || 0,
referralCount: user.referral_count || 0,

View File

@@ -125,17 +125,78 @@ export async function POST(request: Request) {
const { productType, productId, userId } = attach
// 1. 更新订单状态为已支付
let orderExists = false
try {
await query(`
UPDATE orders
SET status = 'paid',
transaction_id = ?,
pay_time = CURRENT_TIMESTAMP
WHERE order_sn = ? AND status = 'pending'
`, [transactionId, orderSn])
console.log('[PayNotify] 订单状态已更新:', orderSn)
// 先查询订单是否存在
const orderRows = await query(`
SELECT id, user_id, product_type, product_id, status
FROM orders
WHERE order_sn = ?
`, [orderSn]) as any[]
if (orderRows.length === 0) {
console.warn('[PayNotify] ⚠️ 订单不存在,尝试补记:', orderSn)
// 订单不存在时,补记订单(可能是创建订单时失败了)
try {
await query(`
INSERT INTO orders (
id, order_sn, user_id, open_id,
product_type, product_id, amount, description,
status, transaction_id, pay_time, referrer_id, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'paid', ?, CURRENT_TIMESTAMP, NULL, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
`, [
orderSn, orderSn, userId || openId, openId,
productType || 'unknown', productId || '', totalAmount,
'支付回调补记订单', transactionId
])
console.log('[PayNotify] ✅ 订单补记成功:', orderSn)
orderExists = true
} catch (insertErr: any) {
if (insertErr?.message?.includes('referrer_id') || insertErr?.code === 'ER_BAD_FIELD_ERROR') {
try {
await query(`
INSERT INTO orders (
id, order_sn, user_id, open_id,
product_type, product_id, amount, description,
status, transaction_id, pay_time, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'paid', ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
`, [
orderSn, orderSn, userId || openId, openId,
productType || 'unknown', productId || '', totalAmount,
'支付回调补记订单', transactionId
])
console.log('[PayNotify] ✅ 订单补记成功(无 referrer_id):', orderSn)
orderExists = true
} catch (e2) {
console.error('[PayNotify] ❌ 补记订单失败:', e2)
}
} else {
console.error('[PayNotify] ❌ 补记订单失败:', insertErr)
}
}
} else {
const order = orderRows[0]
orderExists = true
if (order.status === 'paid') {
console.log('[PayNotify] 订单已支付,跳过更新:', orderSn)
} else {
// 更新订单状态
await query(`
UPDATE orders
SET status = 'paid',
transaction_id = ?,
pay_time = CURRENT_TIMESTAMP,
updated_at = CURRENT_TIMESTAMP
WHERE order_sn = ?
`, [transactionId, orderSn])
console.log('[PayNotify] ✅ 订单状态已更新为已支付:', orderSn)
}
}
} catch (e) {
console.error('[PayNotify] 更新订单状态失败:', e)
console.error('[PayNotify] ❌ 处理订单失败:', e)
}
// 2. 获取用户信息
@@ -151,30 +212,83 @@ export async function POST(request: Request) {
}
}
// 3. 更新用户购买记录
if (buyerUserId) {
// 3. 更新用户购买记录(✅ 检查是否已有其他相同产品的已支付订单)
if (buyerUserId && productType) {
try {
if (productType === 'fullbook') {
// 全书购买
// 全书购买:无论如何都解锁
await query('UPDATE users SET has_full_book = TRUE WHERE id = ?', [buyerUserId])
console.log('[PayNotify] 用户已购全书:', buyerUserId)
console.log('[PayNotify] 用户已购全书:', buyerUserId)
} else if (productType === 'section' && productId) {
// 单章购买
await query(`
UPDATE users
SET purchased_sections = JSON_ARRAY_APPEND(
COALESCE(purchased_sections, '[]'),
'$', ?
)
WHERE id = ? AND NOT JSON_CONTAINS(COALESCE(purchased_sections, '[]'), ?)
`, [productId, buyerUserId, JSON.stringify(productId)])
console.log('[PayNotify] 用户已购章节:', buyerUserId, productId)
// 单章购买:检查是否已有该章节的其他已支付订单
const existingPaidOrders = await query(`
SELECT COUNT(*) as count
FROM orders
WHERE user_id = ?
AND product_type = 'section'
AND product_id = ?
AND status = 'paid'
AND order_sn != ?
`, [buyerUserId, productId, orderSn]) as any[]
const hasOtherPaidOrder = existingPaidOrders[0].count > 0
if (hasOtherPaidOrder) {
console.log('[PayNotify] 用户已有该章节的其他已支付订单,无需重复解锁:', {
userId: buyerUserId,
productId
})
} else {
// 第一次支付该章节,解锁权限
await query(`
UPDATE users
SET purchased_sections = JSON_ARRAY_APPEND(
COALESCE(purchased_sections, '[]'),
'$', ?
)
WHERE id = ? AND NOT JSON_CONTAINS(COALESCE(purchased_sections, '[]'), ?)
`, [productId, buyerUserId, JSON.stringify(productId)])
console.log('[PayNotify] ✅ 用户首次购买章节,已解锁:', buyerUserId, productId)
}
}
} catch (e) {
console.error('[PayNotify] 更新用户购买记录失败:', e)
console.error('[PayNotify] 更新用户购买记录失败:', e)
}
// 4. 处理分销佣金90%给推广者
// 4. 清理相同产品的无效订单(未支付的订单
if (productType && (productType === 'fullbook' || productId)) {
try {
const deleteResult = await query(`
DELETE FROM orders
WHERE user_id = ?
AND product_type = ?
AND product_id = ?
AND status = 'created'
AND order_sn != ?
`, [
buyerUserId,
productType,
productId || 'fullbook',
orderSn // 保留当前已支付的订单
])
const deletedCount = (deleteResult as any).affectedRows || 0
if (deletedCount > 0) {
console.log('[PayNotify] ✅ 已清理无效订单:', {
userId: buyerUserId,
productType,
productId: productId || 'fullbook',
deletedCount
})
}
} catch (deleteErr) {
console.error('[PayNotify] ❌ 清理无效订单失败:', deleteErr)
// 清理失败不影响主流程
}
}
// 5. 处理分销佣金90%给推广者)
await processReferralCommission(buyerUserId, totalAmount, orderSn)
}
@@ -267,3 +381,58 @@ async function processReferralCommission(buyerUserId: string, amount: number, or
// 分佣失败不影响主流程
}
}
/**
* 清理无效订单
* 当一个订单支付成功后,删除该用户相同产品的其他未支付订单
*/
async function cleanupUnpaidOrders(
userId: string,
productType: string | undefined,
productId: string | undefined,
paidOrderSn: string
) {
try {
if (!userId || !productType) {
return
}
// 查询相同产品的其他未支付订单
const unpaidOrders = await query(`
SELECT id, order_sn, status, created_at
FROM orders
WHERE user_id = ?
AND product_type = ?
AND product_id = ?
AND status IN ('created', 'pending')
AND order_sn != ?
`, [userId, productType, productId || 'fullbook', paidOrderSn]) as any[]
if (unpaidOrders.length === 0) {
console.log('[PayNotify] 没有需要清理的无效订单')
return
}
// 删除这些无效订单
await query(`
DELETE FROM orders
WHERE user_id = ?
AND product_type = ?
AND product_id = ?
AND status IN ('created', 'pending')
AND order_sn != ?
`, [userId, productType, productId || 'fullbook', paidOrderSn])
console.log('[PayNotify] ✅ 已清理无效订单:', {
userId,
productType,
productId,
deletedCount: unpaidOrders.length,
deletedOrders: unpaidOrders.map(o => o.order_sn)
})
} catch (error) {
console.error('[PayNotify] ❌ 清理无效订单失败:', error)
// 清理失败不影响主流程
}
}

View File

@@ -10,6 +10,7 @@
import { NextResponse } from 'next/server'
import crypto from 'crypto'
import { query } from '@/lib/db'
// 微信支付配置 - 2026-01-25 更新
// 小程序支付绑定状态: 审核中申请单ID: 201554696918
@@ -134,6 +135,104 @@ export async function POST(request: Request) {
productId,
})
// === ✅ 1. 先插入订单到数据库(无论支付是否成功,都要有订单记录) ===
const userId = body.userId || openId // 优先使用 userId否则用 openId
let orderCreated = false
// 查询当前用户的有效推荐人(用于订单归属与分销)
let referrerId: string | null = null
try {
const bindings = await query(`
SELECT referrer_id
FROM referral_bindings
WHERE referee_id = ? AND status = 'active' AND expiry_date > NOW()
ORDER BY binding_date DESC
LIMIT 1
`, [userId]) as any[]
if (bindings.length > 0) {
referrerId = bindings[0].referrer_id || null
console.log('[MiniPay] 订单归属推荐人(绑定):', referrerId)
}
// 若绑定未查到且前端传了邀请码,按邀请码解析推荐人
if (!referrerId && body.referralCode) {
const refUsers = await query(`
SELECT id FROM users WHERE referral_code = ? LIMIT 1
`, [String(body.referralCode).trim()]) as any[]
if (refUsers.length > 0) {
referrerId = refUsers[0].id
console.log('[MiniPay] 订单归属推荐人(邀请码):', referrerId)
}
}
} catch (e) {
console.warn('[MiniPay] 查询推荐人失败,继续创建订单:', e)
}
try {
// 检查是否已有相同产品的已支付订单
const existingOrders = await query(`
SELECT id FROM orders
WHERE user_id = ?
AND product_type = ?
AND product_id = ?
AND status = 'paid'
LIMIT 1
`, [userId, productType, productId || 'fullbook']) as any[]
if (existingOrders.length > 0) {
console.log('[MiniPay] ⚠️ 用户已购买该产品,但仍创建订单:', {
userId,
productType,
productId
})
}
// 插入订单(含 referrer_id便于分销归属与统计
try {
await query(`
INSERT INTO orders (
id, order_sn, user_id, open_id,
product_type, product_id, amount, description,
status, transaction_id, referrer_id, created_at, updated_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
`, [
orderSn, orderSn, userId, openId,
productType, productId || 'fullbook', amount, goodsBody,
'created', null, referrerId
])
} catch (insertErr: any) {
// 兼容:若表尚无 referrer_id 列,则用不含该字段的 INSERT
if (insertErr?.message?.includes('referrer_id') || insertErr?.code === 'ER_BAD_FIELD_ERROR') {
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, orderSn, userId, openId,
productType, productId || 'fullbook', amount, goodsBody,
'created', null
])
console.log('[MiniPay] 订单已插入(未含 referrer_id请执行 scripts/add_orders_referrer_id.py)')
} else {
throw insertErr
}
}
orderCreated = true
console.log('[MiniPay] ✅ 订单已插入数据库:', {
orderSn,
userId,
productType,
productId,
amount
})
} catch (dbError) {
console.error('[MiniPay] ❌ 插入订单失败:', dbError)
// 订单创建失败,但不中断支付流程
// 理由:微信支付成功后仍可以通过回调补记订单
}
// 调用微信统一下单接口
const xmlData = dictToXml(params)
const response = await fetch('https://api.mch.weixin.qq.com/pay/unifiedorder', {