头铁
This commit is contained in:
@@ -166,6 +166,17 @@ export async function POST(request: Request) {
|
||||
} catch (e) {
|
||||
console.warn('[MiniPay] 查询推荐人失败,继续创建订单:', e)
|
||||
}
|
||||
|
||||
// 下单时使用的邀请码:优先用请求体,否则用推荐人当前邀请码(便于订单记录对账)
|
||||
let orderReferralCode: string | null = body.referralCode ? String(body.referralCode).trim() || null : null
|
||||
if (!orderReferralCode && referrerId) {
|
||||
try {
|
||||
const refRows = (await query(`SELECT referral_code FROM users WHERE id = ? LIMIT 1`, [referrerId]) as any[])
|
||||
if (refRows.length > 0 && refRows[0].referral_code) {
|
||||
orderReferralCode = refRows[0].referral_code
|
||||
}
|
||||
} catch (_) { /* 忽略 */ }
|
||||
}
|
||||
|
||||
try {
|
||||
// 检查是否已有相同产品的已支付订单
|
||||
@@ -186,34 +197,55 @@ export async function POST(request: Request) {
|
||||
})
|
||||
}
|
||||
|
||||
// 插入订单(含 referrer_id,便于分销归属与统计)
|
||||
// 插入订单(含 referrer_id、referral_code,便于分销归属与对账)
|
||||
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())
|
||||
status, transaction_id, referrer_id, referral_code, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
|
||||
`, [
|
||||
orderSn, orderSn, userId, openId,
|
||||
productType, productId || 'fullbook', amount, goodsBody,
|
||||
'created', null, referrerId
|
||||
'created', null, referrerId, orderReferralCode
|
||||
])
|
||||
} 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)')
|
||||
// 兼容:若表尚无 referrer_id 或 referral_code 列
|
||||
const msg = (insertErr as any)?.message || ''
|
||||
const code = (insertErr as any)?.code || ''
|
||||
if (msg.includes('referrer_id') || msg.includes('referral_code') || 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, referrer_id, created_at, updated_at
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), NOW())
|
||||
`, [
|
||||
orderSn, orderSn, userId, openId,
|
||||
productType, productId || 'fullbook', amount, goodsBody,
|
||||
'created', null, referrerId
|
||||
])
|
||||
console.log('[MiniPay] 订单已插入(未含 referral_code,请执行 scripts/add_orders_referral_code.py)')
|
||||
} catch (e2: any) {
|
||||
if (e2?.message?.includes('referrer_id') || e2?.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/referral_code,请执行迁移脚本)')
|
||||
} else {
|
||||
throw e2
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw insertErr
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user