优化提现逻辑,修正可提现金额计算方式,确保准确反映累计佣金、已提现金额和待审核金额。同时,更新小程序界面,调整可提现金额的显示和按钮状态判断,提升用户体验。
This commit is contained in:
@@ -84,39 +84,75 @@ export async function POST(request: NextRequest) {
|
||||
})
|
||||
}
|
||||
|
||||
// 查询可提现金额(待结算收益)
|
||||
let totalEarnings = 0
|
||||
// ✅ 修正:从 orders 表查询累计佣金(与前端逻辑一致)
|
||||
let totalCommission = 0
|
||||
try {
|
||||
const earningsResult = await query(`
|
||||
SELECT COALESCE(SUM(commission), 0) as total_commission
|
||||
FROM referral_bindings
|
||||
WHERE referrer_id = ? AND status = 'converted'
|
||||
// 读取分成比例
|
||||
let distributorShare = 0.9 // 默认90%
|
||||
try {
|
||||
const config = await getConfig('referral_config')
|
||||
if (config?.distributorShare) {
|
||||
distributorShare = Number(config.distributorShare)
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[Withdraw] 读取分成比例失败,使用默认值 90%')
|
||||
}
|
||||
|
||||
// 查询订单总金额
|
||||
const ordersResult = await query(`
|
||||
SELECT COALESCE(SUM(amount), 0) as total_amount
|
||||
FROM orders
|
||||
WHERE referrer_id = ? AND status = 'paid'
|
||||
`, [userId]) as any[]
|
||||
totalEarnings = parseFloat(earningsResult[0]?.total_commission || 0)
|
||||
|
||||
const totalAmount = parseFloat(ordersResult[0]?.total_amount || 0)
|
||||
totalCommission = totalAmount * distributorShare
|
||||
|
||||
console.log('[Withdraw] 佣金计算:')
|
||||
console.log('- 订单总金额:', totalAmount)
|
||||
console.log('- 分成比例:', distributorShare * 100 + '%')
|
||||
console.log('- 累计佣金:', totalCommission)
|
||||
} catch (e) {
|
||||
// 如果表不存在,收益为0
|
||||
console.log('[Withdraw] 查询收益失败,可能表不存在')
|
||||
console.log('[Withdraw] 查询收益失败,可能表不存在:', e)
|
||||
}
|
||||
|
||||
// 查询已提现金额
|
||||
let withdrawnAmount = 0
|
||||
let withdrawnEarnings = 0
|
||||
try {
|
||||
const withdrawnResult = await query(`
|
||||
SELECT COALESCE(SUM(amount), 0) as withdrawn
|
||||
FROM withdrawals
|
||||
WHERE user_id = ? AND status = 'completed'
|
||||
`, [userId]) as any[]
|
||||
withdrawnAmount = parseFloat(withdrawnResult[0]?.withdrawn || 0)
|
||||
withdrawnEarnings = parseFloat(user.withdrawn_earnings) || 0
|
||||
} catch (e) {
|
||||
// 如果表不存在,已提现为0
|
||||
console.log('[Withdraw] 读取已提现金额失败:', e)
|
||||
}
|
||||
|
||||
const availableAmount = totalEarnings - withdrawnAmount
|
||||
// 查询待审核提现金额
|
||||
let pendingWithdrawAmount = 0
|
||||
try {
|
||||
const pendingResult = await query(`
|
||||
SELECT COALESCE(SUM(amount), 0) as pending_amount
|
||||
FROM withdrawals
|
||||
WHERE user_id = ? AND status = 'pending'
|
||||
`, [userId]) as any[]
|
||||
pendingWithdrawAmount = parseFloat(pendingResult[0]?.pending_amount || 0)
|
||||
} catch (e) {
|
||||
console.log('[Withdraw] 查询待审核金额失败:', e)
|
||||
}
|
||||
|
||||
// ✅ 修正:可提现金额 = 累计佣金 - 已提现金额 - 待审核金额(三元素完整校验)
|
||||
const availableAmount = totalCommission - withdrawnEarnings - pendingWithdrawAmount
|
||||
|
||||
console.log('[Withdraw] 提现验证(完整版):')
|
||||
console.log('- 累计佣金 (totalCommission):', totalCommission)
|
||||
console.log('- 已提现金额 (withdrawnEarnings):', withdrawnEarnings)
|
||||
console.log('- 待审核金额 (pendingWithdrawAmount):', pendingWithdrawAmount)
|
||||
console.log('- 可提现金额 = 累计 - 已提现 - 待审核 =', totalCommission, '-', withdrawnEarnings, '-', pendingWithdrawAmount, '=', availableAmount)
|
||||
console.log('- 申请提现金额 (amount):', amount)
|
||||
console.log('- 判断:', amount, '>', availableAmount, '=', amount > availableAmount)
|
||||
|
||||
if (amount > availableAmount) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: `可提现金额不足,当前可提现 ¥${availableAmount.toFixed(2)}`
|
||||
message: `可提现金额不足。当前可提现 ¥${availableAmount.toFixed(2)}(累计 ¥${totalCommission.toFixed(2)} - 已提现 ¥${withdrawnEarnings.toFixed(2)} - 待审核 ¥${pendingWithdrawAmount.toFixed(2)})`
|
||||
})
|
||||
}
|
||||
|
||||
@@ -144,12 +180,13 @@ export async function POST(request: NextRequest) {
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '提现成功',
|
||||
message: '提现申请已提交,正在审核中,通过后会自动到账您的微信零钱',
|
||||
data: {
|
||||
withdrawId,
|
||||
amount,
|
||||
account,
|
||||
accountType: accountType === 'alipay' ? '支付宝' : '微信'
|
||||
accountType: accountType === 'alipay' ? '支付宝' : '微信',
|
||||
status: 'pending'
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user