新增订单推荐人和邀请码功能,优化支付流程中的订单插入逻辑,确保订单记录准确。更新小程序支付请求,支持传递邀请码以便于分销归属和对账。同时,调整数据库结构以支持新字段,提升系统的稳定性和用户体验。

This commit is contained in:
2026-02-06 18:34:02 +08:00
parent f8fac00c85
commit 2e65d68e1e
34 changed files with 3288 additions and 1255 deletions

View File

@@ -1,5 +1,5 @@
/**
* 推荐码绑定API - 增强版
* 推荐码绑定API - 使用 Prisma ORM
*
* 核心规则:
* 1. 链接带ID谁发的链接进的人就绑谁
@@ -10,20 +10,16 @@
*/
import { NextRequest, NextResponse } from 'next/server'
import { query, getConfig } from '@/lib/db'
import { prisma } from '@/lib/prisma'
import { getPrismaConfig } from '@/lib/prisma-helpers'
// 绑定有效期(天)- 默认值,优先从配置读取
const DEFAULT_BINDING_DAYS = 30
/**
* POST - 绑定推荐关系(支持抢夺机制)
*/
export async function POST(request: NextRequest) {
try {
const body = await request.json()
const { userId, referralCode, openId, source } = body
// 验证参数
const effectiveUserId = userId || (openId ? `user_${openId.slice(-8)}` : null)
if (!effectiveUserId || !referralCode) {
return NextResponse.json({
@@ -35,29 +31,31 @@ export async function POST(request: NextRequest) {
// 获取绑定天数配置
let bindingDays = DEFAULT_BINDING_DAYS
try {
const config = await getConfig('referral_config')
const config = await getPrismaConfig('referral_config')
if (config?.bindingDays) {
bindingDays = Number(config.bindingDays)
}
} catch (e) {
console.warn('[Referral Bind] 读取配置失败,使用默认', DEFAULT_BINDING_DAYS)
console.warn('[Referral Bind] 使用默认配置', DEFAULT_BINDING_DAYS)
}
// 查找推荐人
const referrers = await query(
'SELECT id, nickname, referral_code FROM users WHERE referral_code = ?',
[referralCode]
) as any[]
// 查找推荐人(使用 Prisma
const referrer = await prisma.users.findUnique({
where: { referral_code: referralCode },
select: {
id: true,
nickname: true,
referral_code: true
}
})
if (referrers.length === 0) {
if (!referrer) {
return NextResponse.json({
success: false,
error: '推荐码无效'
}, { status: 400 })
}
const referrer = referrers[0]
// 不能自己推荐自己
if (referrer.id === effectiveUserId) {
return NextResponse.json({
@@ -67,138 +65,135 @@ export async function POST(request: NextRequest) {
}
// 检查用户是否存在
const users = await query(
'SELECT id FROM users WHERE id = ? OR open_id = ?',
[effectiveUserId, openId || effectiveUserId]
) as any[]
const user = await prisma.users.findFirst({
where: {
OR: [
{ id: effectiveUserId },
{ open_id: openId || effectiveUserId }
]
}
})
if (users.length === 0) {
if (!user) {
return NextResponse.json({
success: false,
error: '用户不存在'
}, { status: 400 })
}
const user = users[0]
const now = new Date()
// 检查现有绑定关系
const existingBindings = await query(`
SELECT id, referrer_id, expiry_date, status
FROM referral_bindings
WHERE referee_id = ? AND status = 'active'
ORDER BY binding_date DESC LIMIT 1
`, [user.id]) as any[]
const existingBinding = await prisma.referral_bindings.findFirst({
where: {
referee_id: user.id,
status: 'active'
},
orderBy: { binding_date: 'desc' }
})
let action = 'new' // new=新绑定, renew=续期, switch=立即切换
let action = 'new'
let oldReferrerId = null
if (existingBindings.length > 0) {
const existing = existingBindings[0]
// 同一个推荐人 - 续期刷新30天
if (existing.referrer_id === referrer.id) {
action = 'renew'
}
// 不同推荐人 - 立即切换(新逻辑:无条件切换)
else {
action = 'switch'
oldReferrerId = existing.referrer_id
// 将旧绑定标记为 cancelled被切换
await query(
"UPDATE referral_bindings SET status = 'cancelled' WHERE id = ?",
[existing.id]
)
console.log(`[Referral Bind] 立即切换: ${user.id}: ${oldReferrerId} -> ${referrer.id}`)
}
}
// 计算新的过期时间(从配置读取天数)
// 计算新的过期时间
const expiryDate = new Date()
expiryDate.setDate(expiryDate.getDate() + bindingDays)
// 创建或更新绑定记录
const bindingId = 'bind_' + Date.now().toString(36) + Math.random().toString(36).substr(2, 6)
if (action === 'renew') {
// 续期:更新过期时间
await query(`
UPDATE referral_bindings
SET expiry_date = ?, binding_date = CURRENT_TIMESTAMP
WHERE referee_id = ? AND referrer_id = ? AND status = 'active'
`, [expiryDate, user.id, referrer.id])
console.log(`[Referral Bind] 续期: ${user.id} -> ${referrer.id},新过期时间: ${expiryDate.toISOString()}`)
if (existingBinding) {
if (existingBinding.referrer_id === referrer.id) {
// 同一个推荐人 - 续期
action = 'renew'
await prisma.referral_bindings.update({
where: { id: existingBinding.id },
data: {
expiry_date: expiryDate,
binding_date: new Date()
}
})
console.log(`[Referral Bind] 续期: ${user.id} -> ${referrer.id}`)
} else {
// 不同推荐人 - 立即切换
action = 'switch'
oldReferrerId = existingBinding.referrer_id
// 使用 Prisma 事务确保原子性
await prisma.$transaction([
// 将旧绑定标记为 cancelled
prisma.referral_bindings.update({
where: { id: existingBinding.id },
data: { status: 'cancelled' }
}),
// 创建新绑定
prisma.referral_bindings.create({
data: {
id: `bind_${Date.now()}_${Math.random().toString(36).substr(2, 6)}`,
referrer_id: referrer.id,
referee_id: user.id,
referral_code: referralCode,
status: 'active',
expiry_date: expiryDate,
binding_date: new Date()
}
})
])
console.log(`[Referral Bind] 立即切换: ${user.id}: ${oldReferrerId} -> ${referrer.id}`)
}
} else {
// 新绑定或切换
await query(`
INSERT INTO referral_bindings (
id, referrer_id, referee_id, referral_code, status, expiry_date, binding_date
) VALUES (?, ?, ?, ?, 'active', ?, CURRENT_TIMESTAMP)
ON DUPLICATE KEY UPDATE
referrer_id = VALUES(referrer_id),
referral_code = VALUES(referral_code),
expiry_date = VALUES(expiry_date),
binding_date = CURRENT_TIMESTAMP,
status = 'active'
`, [bindingId, referrer.id, user.id, referralCode, expiryDate])
// 新绑定
await prisma.referral_bindings.create({
data: {
id: `bind_${Date.now()}_${Math.random().toString(36).substr(2, 6)}`,
referrer_id: referrer.id,
referee_id: user.id,
referral_code: referralCode,
status: 'active',
expiry_date: expiryDate,
binding_date: new Date()
}
})
// 注意:不再更新 users.referred_by已弃用只使用 referral_bindings
// 更新推荐人的推广数量
await prisma.users.update({
where: { id: referrer.id },
data: {
referral_count: { increment: 1 }
}
})
// 更新推荐人的推广数量(仅新绑定时)
if (action === 'new') {
await query(
'UPDATE users SET referral_count = referral_count + 1 WHERE id = ?',
[referrer.id]
)
console.log(`[Referral Bind] 新绑定: ${user.id} -> ${referrer.id}`)
}
// 如果是立即切换,更新双方的推广数量
if (action === 'switch' && oldReferrerId) {
// 减少旧推荐人的数量
await query(
'UPDATE users SET referral_count = GREATEST(referral_count - 1, 0) WHERE id = ?',
[oldReferrerId]
)
// 增加新推荐人的数量
await query(
'UPDATE users SET referral_count = referral_count + 1 WHERE id = ?',
[referrer.id]
)
console.log(`[Referral Bind] 立即切换完成: ${user.id}: ${oldReferrerId} -> ${referrer.id}`)
}
console.log(`[Referral Bind] 新绑定: ${user.id} -> ${referrer.id}`)
}
// 记录访问日志(用于统计「通过链接进的人数」
try {
await query(`
INSERT INTO referral_visits (referrer_id, visitor_id, source, created_at)
VALUES (?, ?, ?, CURRENT_TIMESTAMP)
`, [referrer.id, user.id, source || 'miniprogram'])
} catch (e) {
// 访问日志表可能不存在,忽略错误
}
const messages = {
new: '绑定成功',
renew: '绑定已续期',
switch: '已切换推荐人'
// 记录访问(如果有 referral_visits 表
if (source) {
try {
await prisma.referral_visits.create({
data: {
referrer_id: referrer.id,
visitor_id: user.id,
visitor_openid: openId || null,
source: source || 'miniprogram',
page: null
}
})
} catch (e) {
console.log('[Referral Bind] 记录访问失败(表可能不存在)')
}
}
return NextResponse.json({
success: true,
message: messages[action] || '绑定成功',
action,
expiryDate: expiryDate.toISOString(),
bindingDays,
referrer: {
id: referrer.id,
nickname: referrer.nickname
},
...(oldReferrerId && { oldReferrerId })
message: action === 'renew' ? '绑定已续期' : action === 'switch' ? '推荐人已切换' : '绑定成功',
data: {
action,
referrer: {
id: referrer.id,
nickname: referrer.nickname
},
expiryDate,
bindingDays,
oldReferrerId
}
})
} catch (error) {
@@ -209,113 +204,3 @@ export async function POST(request: NextRequest) {
}, { status: 500 })
}
}
/**
* GET - 查询推荐关系
*/
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url)
const userId = searchParams.get('userId')
const referralCode = searchParams.get('referralCode')
try {
if (referralCode) {
// 查询推荐码对应的用户
const users = await query(
'SELECT id, nickname, avatar FROM users WHERE referral_code = ?',
[referralCode]
) as any[]
if (users.length === 0) {
return NextResponse.json({
success: false,
error: '推荐码无效'
}, { status: 404 })
}
return NextResponse.json({
success: true,
referrer: users[0]
})
}
if (userId) {
// 查询用户是否存在
const users = await query(
'SELECT id FROM users WHERE id = ?',
[userId]
) as any[]
if (users.length === 0) {
return NextResponse.json({
success: false,
error: '用户不存在'
}, { status: 404 })
}
// 从 referral_bindings 查询当前有效的推荐人
let referrer = null
const activeBinding = await query(`
SELECT
rb.referrer_id,
u.nickname,
u.avatar,
rb.expiry_date,
rb.purchase_count
FROM referral_bindings rb
JOIN users u ON rb.referrer_id = u.id
WHERE rb.referee_id = ?
AND rb.status = 'active'
AND rb.expiry_date > NOW()
ORDER BY rb.binding_date DESC
LIMIT 1
`, [userId]) as any[]
if (activeBinding.length > 0) {
referrer = {
id: activeBinding[0].referrer_id,
nickname: activeBinding[0].nickname,
avatar: activeBinding[0].avatar,
expiryDate: activeBinding[0].expiry_date,
purchaseCount: activeBinding[0].purchase_count
}
}
// 获取该用户推荐的人(所有活跃绑定)
const referees = await query(`
SELECT
u.id,
u.nickname,
u.avatar,
rb.binding_date as created_at,
rb.purchase_count,
rb.total_commission
FROM referral_bindings rb
JOIN users u ON rb.referee_id = u.id
WHERE rb.referrer_id = ?
AND rb.status = 'active'
AND rb.expiry_date > NOW()
ORDER BY rb.binding_date DESC
`, [userId]) as any[]
return NextResponse.json({
success: true,
referrer,
referees,
referralCount: referees.length
})
}
return NextResponse.json({
success: false,
error: '请提供userId或referralCode参数'
}, { status: 400 })
} catch (error) {
console.error('[Referral Bind] GET错误:', error)
return NextResponse.json({
success: false,
error: '查询失败: ' + (error as Error).message
}, { status: 500 })
}
}

View File

@@ -1,31 +1,18 @@
/**
* 分销数据API - 性能优化版
* 分销数据API - 使用 Prisma ORM
*
* 优化内容
* - ⚡ 合并统计查询5个独立查询 → 1个聚合查询减少60%响应时间)
* - ⚡ 减少数据量列表从50/30条 → 20条减少55%数据传输)
* - ⚡ 优化查询添加索引提升查询效率30-50%
*
* 可见数据:
* - 绑定用户数(当前有效绑定)
* - 通过链接进的人数(总访问量)
* - 带来的付款人数(已转化购买)
* - 收益统计90%归分发者)
*
* 性能:
* - 数据库查询9个 → 5个减少44%
* - 预计响应时间500-800ms → 200-300ms
* 优
* - ✅ 完全类型安全
* - ✅ 自动防SQL注入
* - ✅ 使用 Prisma 原生聚合查询
*/
import { NextRequest, NextResponse } from 'next/server'
import { query, getConfig } from '@/lib/db'
import { prisma } from '@/lib/prisma'
import { getPrismaConfig } from '@/lib/prisma-helpers'
// 分成比例默认90%给推广者)
const DISTRIBUTOR_SHARE = 0.9
/**
* GET - 获取分销数据
*/
export async function GET(request: NextRequest) {
const { searchParams } = new URL(request.url)
const userId = searchParams.get('userId')
@@ -40,264 +27,288 @@ export async function GET(request: NextRequest) {
try {
// 获取分销配置
let distributorShare = DISTRIBUTOR_SHARE
let minWithdrawAmount = 10 // 默认最低提现金额
let minWithdrawAmount = 10
try {
const config = await getConfig('referral_config')
const config = await getPrismaConfig('referral_config')
if (config?.distributorShare) {
distributorShare = config.distributorShare / 100
}
if (config?.minWithdrawAmount) {
minWithdrawAmount = Number(config.minWithdrawAmount)
}
} catch (e) { /* 使用默认配置 */ }
} catch (e) { }
// ⚡ 优化:合并统计查询 - 添加错误处理
let statsResult: any[]
try {
statsResult = await query(`
SELECT
-- 用户基本信息
u.id, u.nickname, u.referral_code, u.earnings, u.pending_earnings,
u.withdrawn_earnings, u.referral_count,
-- 绑定关系统计
(SELECT COUNT(*) FROM referral_bindings WHERE referrer_id = u.id) as total_bindings,
(SELECT COUNT(*) FROM referral_bindings WHERE referrer_id = u.id AND status = 'active' AND expiry_date > NOW()) as active_bindings,
(SELECT COUNT(*) FROM referral_bindings WHERE referrer_id = u.id AND status = 'active' AND purchase_count > 0) as converted_bindings,
(SELECT COUNT(*) FROM referral_bindings WHERE referrer_id = u.id AND (status IN ('expired', 'cancelled') OR (status = 'active' AND expiry_date <= NOW()))) as expired_bindings,
-- 付款统计直接从orders表查询
(SELECT COUNT(DISTINCT user_id) FROM orders WHERE referrer_id = u.id AND status = 'paid') as paid_count,
(SELECT COALESCE(SUM(amount), 0) FROM orders WHERE referrer_id = u.id AND status = 'paid') as total_referral_amount
FROM users u
WHERE u.id = ?
`, [userId]) as any[]
} catch (err) {
console.error('[ReferralData] 统计查询失败:', err)
return NextResponse.json({
success: false,
error: '查询统计数据失败: ' + (err as Error).message
}, { status: 500 })
}
// 1. 查询用户基本信息
const user = await prisma.users.findUnique({
where: { id: userId },
select: {
id: true,
nickname: true,
referral_code: true,
earnings: true,
pending_earnings: true,
withdrawn_earnings: true,
referral_count: true
}
})
if (statsResult.length === 0) {
if (!user) {
return NextResponse.json({
success: false,
error: '用户不存在'
}, { status: 404 })
}
const stats = statsResult[0]
// 2. 使用 Prisma 聚合查询绑定统计
const [totalBindings, activeBindings, convertedBindings, expiredBindings] = await Promise.all([
prisma.referral_bindings.count({
where: { referrer_id: userId }
}),
prisma.referral_bindings.count({
where: {
referrer_id: userId,
status: 'active',
expiry_date: { gt: new Date() }
}
}),
prisma.referral_bindings.count({
where: {
referrer_id: userId,
status: 'active',
purchase_count: { gt: 0 }
}
}),
prisma.referral_bindings.count({
where: {
referrer_id: userId,
OR: [
{ status: { in: ['expired', 'cancelled'] } },
{ status: 'active', expiry_date: { lte: new Date() } }
]
}
})
])
// 解构统计数据
const user = {
id: stats.id,
nickname: stats.nickname,
referral_code: stats.referral_code,
earnings: stats.earnings,
pending_earnings: stats.pending_earnings,
withdrawn_earnings: stats.withdrawn_earnings,
referral_count: stats.referral_count
}
// 3. 付款统计(使用 Prisma 聚合)
const paidOrders = await prisma.orders.aggregate({
where: {
referrer_id: userId,
status: 'paid'
},
_count: { user_id: true },
_sum: { amount: true }
})
const bindingStats = {
total: parseInt(stats.total_bindings) || 0,
active: parseInt(stats.active_bindings) || 0,
converted: parseInt(stats.converted_bindings) || 0,
expired: parseInt(stats.expired_bindings) || 0
}
const paidCount = await prisma.orders.findMany({
where: {
referrer_id: userId,
status: 'paid'
},
distinct: ['user_id'],
select: { user_id: true }
})
const paymentStats = {
paidCount: parseInt(stats.paid_count) || 0,
totalAmount: parseFloat(stats.total_referral_amount) || 0
}
const totalAmount = Number(paidOrders._sum.amount || 0)
const uniquePaidCount = paidCount.length
// 获取访问统计(独立查询,带错误处理)
let totalVisits = bindingStats.total
// 4. 访问统计
let totalVisits = totalBindings
try {
const visits = await query(`
SELECT COUNT(DISTINCT visitor_id) as count
FROM referral_visits
WHERE referrer_id = ?
`, [userId]) as any[]
totalVisits = parseInt(visits[0]?.count) || bindingStats.total
const visits = await prisma.referral_visits.groupBy({
by: ['visitor_id'],
where: { referrer_id: userId }
})
totalVisits = visits.length
} catch (e) {
// referral_visits 表可能不存在,使用绑定数作为访问数
console.log('[ReferralData] 访问统计表不存在,使用绑定数')
console.log('[ReferralData] 访问统计表不存在')
}
// 获取待审核提现金额(独立查询,带错误处理
let pendingWithdrawAmount = 0
try {
const withdraws = await query(`
SELECT COALESCE(SUM(amount), 0) as pending_amount
FROM withdrawals
WHERE user_id = ? AND status = 'pending'
`, [userId]) as any[]
pendingWithdrawAmount = parseFloat(withdraws[0]?.pending_amount) || 0
} catch (e) {
console.log('[ReferralData] 提现表查询失败:', e)
}
// 5. 待审核 + 已成功提现金额(用提现表汇总,与 user.withdrawn_earnings 可能不一致时以表为准,避免可提现出现负数
const [pendingWithdraw, successWithdraw] = await Promise.all([
prisma.withdrawals.aggregate({
where: { user_id: userId, status: 'pending' },
_sum: { amount: true }
}),
prisma.withdrawals.aggregate({
where: { user_id: userId, status: 'success' },
_sum: { amount: true }
})
])
const pendingWithdrawAmount = Number(pendingWithdraw._sum.amount || 0)
const withdrawnFromTable = Number(successWithdraw._sum.amount || 0)
// ⚡ 优化减少列表数据量50条→20条减少数据传输
// 2. 获取活跃绑定用户列表
const activeBindings = await query(`
SELECT rb.id, rb.referee_id, rb.expiry_date, rb.binding_date,
u.nickname, u.avatar, u.has_full_book,
DATEDIFF(rb.expiry_date, NOW()) as days_remaining
FROM referral_bindings rb
JOIN users u ON rb.referee_id = u.id
WHERE rb.referrer_id = ? AND rb.status = 'active' AND rb.expiry_date > NOW()
ORDER BY rb.binding_date DESC
LIMIT 20
`, [userId]) as any[]
// 6. 获取活跃绑定用户列表
const activeBindingsList = await prisma.referral_bindings.findMany({
where: {
referrer_id: userId,
status: 'active',
expiry_date: { gt: new Date() }
},
take: 20,
orderBy: { binding_date: 'desc' },
include: {
users_referral_bindings_referee_idTousers: {
select: {
id: true,
nickname: true,
avatar: true,
has_full_book: true
}
}
}
})
// 3. 获取已转化用户列表(新逻辑:有购买记录的活跃绑定)
const convertedBindings = await query(`
SELECT rb.id, rb.referee_id, rb.last_purchase_date as conversion_date,
rb.total_commission as commission_amount, rb.purchase_count,
u.nickname, u.avatar,
(SELECT COALESCE(SUM(amount), 0) FROM orders WHERE user_id = rb.referee_id AND status = 'paid') as order_amount
FROM referral_bindings rb
JOIN users u ON rb.referee_id = u.id
WHERE rb.referrer_id = ? AND rb.status = 'active' AND rb.purchase_count > 0
ORDER BY rb.last_purchase_date DESC
LIMIT 20
`, [userId]) as any[]
// 7. 获取已转化用户列表
const convertedBindingsList = await prisma.referral_bindings.findMany({
where: {
referrer_id: userId,
status: 'active',
purchase_count: { gt: 0 }
},
take: 20,
orderBy: { last_purchase_date: 'desc' },
include: {
users_referral_bindings_referee_idTousers: {
select: {
id: true,
nickname: true,
avatar: true
}
}
}
})
// 4. 获取已过期用户列表
const expiredBindings = await query(`
SELECT rb.id, rb.referee_id, rb.expiry_date, rb.binding_date,
u.nickname, u.avatar
FROM referral_bindings rb
JOIN users u ON rb.referee_id = u.id
WHERE rb.referrer_id = ? AND (rb.status = 'expired' OR (rb.status = 'active' AND rb.expiry_date <= NOW()))
ORDER BY rb.expiry_date DESC
LIMIT 20
`, [userId]) as any[]
// 8. 获取已过期用户列表
const expiredBindingsList = await prisma.referral_bindings.findMany({
where: {
referrer_id: userId,
OR: [
{ status: 'expired' },
{ status: 'active', expiry_date: { lte: new Date() } }
]
},
take: 20,
orderBy: { expiry_date: 'desc' },
include: {
users_referral_bindings_referee_idTousers: {
select: {
id: true,
nickname: true,
avatar: true
}
}
}
})
// 5. 获取收益明细(包含买家信息和商品详情)
let earningsDetails: any[] = []
try {
earningsDetails = await query(`
SELECT
o.id,
o.order_sn,
o.amount,
o.product_type,
o.product_id,
o.description,
o.pay_time,
u.nickname as buyer_nickname,
u.avatar as buyer_avatar,
rb.total_commission / rb.purchase_count as commission_per_order
FROM orders o
JOIN users u ON o.user_id = u.id
JOIN referral_bindings rb ON o.user_id = rb.referee_id AND rb.referrer_id = ?
WHERE o.status = 'paid' AND o.referrer_id = ?
ORDER BY o.pay_time DESC
LIMIT 20
`, [userId, userId]) as any[]
} catch (e) {
console.log('[ReferralData] 获取收益明细失败:', e)
}
// 9. 获取收益明细
const earningsDetailsList = await prisma.orders.findMany({
where: {
referrer_id: userId,
status: 'paid'
},
take: 20,
orderBy: { pay_time: 'desc' },
include: {
users: {
select: {
nickname: true,
avatar: true
}
}
}
})
// 6. 计算预估收益
const estimatedEarnings = paymentStats.totalAmount * distributorShare
// 计算预估收益
const estimatedEarnings = totalAmount * distributorShare
const totalCommission = totalAmount * distributorShare
// 计算即将过期用户数7天内
const now = new Date()
const sevenDaysLater = new Date(now.getTime() + 7 * 24 * 60 * 60 * 1000)
const expiringCount = activeBindingsList.filter(b => {
const expiryDate = new Date(b.expiry_date)
return expiryDate > now && expiryDate <= sevenDaysLater
}).length
return NextResponse.json({
success: true,
data: {
// === 核心可见数据 ===
// 绑定用户数(当前有效绑定)
bindingCount: bindingStats.active,
// 通过链接进的人数
// 核心可见数据
bindingCount: activeBindings,
visitCount: totalVisits,
// 带来的付款人数
paidCount: paymentStats.paidCount,
// 已过期用户数
expiredCount: bindingStats.expired,
paidCount: uniquePaidCount,
expiredCount: expiredBindings,
// === 收益数据 ===
// 累计佣金总额(直接从订单表计算:订单金额 × 分成比例)
totalCommission: Math.round((paymentStats.totalAmount * distributorShare) * 100) / 100,
// 可提现金额pending_earnings
availableEarnings: parseFloat(user.pending_earnings) || 0,
// 待审核金额(提现申请中的金额)
// 收益数据
totalCommission: Math.round(totalCommission * 100) / 100,
availableEarnings: Math.max(0, Math.round((totalCommission - withdrawnFromTable - pendingWithdrawAmount) * 100) / 100),
pendingWithdrawAmount: Math.round(pendingWithdrawAmount * 100) / 100,
// 已提现金额
withdrawnEarnings: parseFloat(user.withdrawn_earnings) || 0,
// 已结算收益(保留兼容)
earnings: parseFloat(user.earnings) || 0,
// 待结算收益(保留兼容)
pendingEarnings: parseFloat(user.pending_earnings) || 0,
// 预估总收益
withdrawnEarnings: withdrawnFromTable,
earnings: Number(user.earnings) || 0,
pendingEarnings: Number(user.pending_earnings) || 0,
estimatedEarnings: Math.round(estimatedEarnings * 100) / 100,
// 分成比例
shareRate: Math.round(distributorShare * 100),
// 最低提现金额(新增:给小程序使用)
minWithdrawAmount,
// === 推荐码 ===
// 推荐码
referralCode: user.referral_code,
referralCount: user.referral_count || bindingStats.total,
referralCount: user.referral_count || totalBindings,
// === 详细统计 ===
// 详细统计
stats: {
totalBindings: bindingStats.total,
activeBindings: bindingStats.active,
convertedBindings: bindingStats.converted,
expiredBindings: bindingStats.expired,
// 即将过期7天内
expiringCount: activeBindings.filter((b: any) => b.days_remaining <= 7 && b.days_remaining > 0).length,
// 总支付金额
totalPaymentAmount: paymentStats.totalAmount
totalBindings,
activeBindings,
convertedBindings,
expiredBindings,
expiringCount,
totalPaymentAmount: totalAmount
},
// === 用户列表 ===
activeUsers: activeBindings.map((b: any) => ({
id: b.referee_id,
nickname: b.nickname || '用户' + b.referee_id.slice(-4),
avatar: b.avatar,
daysRemaining: Math.max(0, b.days_remaining),
hasFullBook: b.has_full_book,
bindingDate: b.binding_date,
status: 'active'
})),
// 用户列表
activeUsers: activeBindingsList.map(b => {
const daysRemaining = Math.max(0, Math.floor((new Date(b.expiry_date).getTime() - Date.now()) / (24 * 60 * 60 * 1000)))
return {
id: b.referee_id,
nickname: b.users_referral_bindings_referee_idTousers.nickname || '用户' + b.referee_id.slice(-4),
avatar: b.users_referral_bindings_referee_idTousers.avatar,
daysRemaining,
hasFullBook: b.users_referral_bindings_referee_idTousers.has_full_book,
bindingDate: b.binding_date,
status: 'active'
}
}),
convertedUsers: convertedBindings.map((b: any) => ({
convertedUsers: convertedBindingsList.map(b => ({
id: b.referee_id,
nickname: b.nickname || '用户' + b.referee_id.slice(-4),
avatar: b.avatar,
commission: parseFloat(b.commission_amount) || 0,
orderAmount: parseFloat(b.order_amount) || 0,
purchaseCount: parseInt(b.purchase_count) || 0,
conversionDate: b.conversion_date,
nickname: b.users_referral_bindings_referee_idTousers.nickname || '用户' + b.referee_id.slice(-4),
avatar: b.users_referral_bindings_referee_idTousers.avatar,
commission: Number(b.total_commission) || 0,
orderAmount: Number(b.total_commission) / distributorShare || 0,
purchaseCount: b.purchase_count || 0,
conversionDate: b.last_purchase_date,
status: 'converted'
})),
// 已过期用户列表
expiredUsers: expiredBindings.map((b: any) => ({
expiredUsers: expiredBindingsList.map(b => ({
id: b.referee_id,
nickname: b.nickname || '用户' + b.referee_id.slice(-4),
avatar: b.avatar,
nickname: b.users_referral_bindings_referee_idTousers.nickname || '用户' + b.referee_id.slice(-4),
avatar: b.users_referral_bindings_referee_idTousers.avatar,
bindingDate: b.binding_date,
expiryDate: b.expiry_date,
status: 'expired'
})),
// === 收益明细 ===
earningsDetails: earningsDetails.map((e: any) => ({
// 收益明细
earningsDetails: earningsDetailsList.map(e => ({
id: e.id,
orderSn: e.order_sn,
amount: parseFloat(e.amount),
commission: parseFloat(e.commission_per_order) || parseFloat(e.amount) * distributorShare,
amount: Number(e.amount),
commission: Number(e.amount) * distributorShare,
productType: e.product_type,
productId: e.product_id,
description: e.description,
buyerNickname: e.buyer_nickname || '用户' + e.id?.toString().slice(-4),
buyerAvatar: e.buyer_avatar,
buyerNickname: e.users.nickname || '用户' + e.id.slice(-4),
buyerAvatar: e.users.avatar,
payTime: e.pay_time
}))
}