feat: 完善后台管理+搜索功能+分销系统
主要更新: - 后台菜单精简(9项→6项) - 新增搜索功能(敏感信息过滤) - 分销绑定和提现系统完善 - 数据库初始化API(自动修复表结构) - 用户管理:显示绑定关系详情 - 小程序:上下章导航优化、匹配页面重构 - 修复hydration和数据类型问题
This commit is contained in:
@@ -1,95 +1,142 @@
|
||||
/**
|
||||
* 推广中心数据API
|
||||
* 获取用户推广数据、绑定关系等
|
||||
* 分销数据API
|
||||
* 获取用户的推广数据、绑定用户列表、收益统计
|
||||
*/
|
||||
|
||||
import { NextResponse } from 'next/server'
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { query } from '@/lib/db'
|
||||
|
||||
/**
|
||||
* GET - 获取用户推广数据
|
||||
* GET - 获取分销数据
|
||||
*/
|
||||
export async function GET(request: Request) {
|
||||
try {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const userId = searchParams.get('userId')
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: '缺少用户ID'
|
||||
}, { status: 400 })
|
||||
}
|
||||
|
||||
console.log('[ReferralData] 获取推广数据, userId:', userId)
|
||||
|
||||
// TODO: 从数据库获取真实数据
|
||||
// 这里应该连接数据库查询用户的推广数据
|
||||
|
||||
// 模拟数据结构
|
||||
const mockData = {
|
||||
earnings: 0,
|
||||
pendingEarnings: 0,
|
||||
referralCount: 0,
|
||||
activeBindings: [],
|
||||
convertedBindings: [],
|
||||
expiredBindings: [],
|
||||
referralCode: `SOUL${userId.slice(-6).toUpperCase()}`
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: mockData
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('[ReferralData] 获取推广数据失败:', error)
|
||||
export async function GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url)
|
||||
const userId = searchParams.get('userId')
|
||||
|
||||
if (!userId) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: '获取推广数据失败'
|
||||
}, { status: 500 })
|
||||
error: '用户ID不能为空'
|
||||
}, { status: 400 })
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* POST - 创建推广绑定关系
|
||||
*/
|
||||
export async function POST(request: Request) {
|
||||
|
||||
try {
|
||||
const body = await request.json()
|
||||
const { referralCode, userId, userInfo } = body
|
||||
|
||||
if (!referralCode || !userId) {
|
||||
// 1. 获取用户基本信息
|
||||
const users = await query(`
|
||||
SELECT id, nickname, referral_code, earnings, pending_earnings,
|
||||
withdrawn_earnings, referral_count
|
||||
FROM users WHERE id = ?
|
||||
`, [userId]) as any[]
|
||||
|
||||
if (users.length === 0) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: '缺少必要参数'
|
||||
}, { status: 400 })
|
||||
error: '用户不存在'
|
||||
}, { status: 404 })
|
||||
}
|
||||
|
||||
const user = users[0]
|
||||
|
||||
// 2. 获取推荐的用户列表
|
||||
const referees = await query(`
|
||||
SELECT id, nickname, avatar, phone, wechat_id,
|
||||
has_full_book, created_at,
|
||||
DATEDIFF(DATE_ADD(created_at, INTERVAL 30 DAY), NOW()) as days_remaining
|
||||
FROM users
|
||||
WHERE referred_by = ?
|
||||
ORDER BY created_at DESC
|
||||
`, [userId]) as any[]
|
||||
|
||||
// 3. 分类绑定用户
|
||||
const now = new Date()
|
||||
const activeBindings: any[] = []
|
||||
const convertedBindings: any[] = []
|
||||
const expiredBindings: any[] = []
|
||||
|
||||
for (const referee of referees) {
|
||||
const binding = {
|
||||
id: referee.id,
|
||||
nickname: referee.nickname || '用户' + referee.id.slice(-4),
|
||||
avatar: referee.avatar || `https://picsum.photos/100/100?random=${referee.id.slice(-2)}`,
|
||||
phone: referee.phone ? referee.phone.slice(0, 3) + '****' + referee.phone.slice(-4) : null,
|
||||
hasFullBook: referee.has_full_book,
|
||||
daysRemaining: Math.max(0, referee.days_remaining || 0),
|
||||
createdAt: referee.created_at
|
||||
}
|
||||
|
||||
if (referee.has_full_book) {
|
||||
// 已转化(已购买)
|
||||
convertedBindings.push(binding)
|
||||
} else if (binding.daysRemaining <= 0) {
|
||||
// 已过期
|
||||
expiredBindings.push(binding)
|
||||
} else {
|
||||
// 活跃中
|
||||
activeBindings.push(binding)
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 获取收益明细(最近的订单)
|
||||
let earningsDetails: any[] = []
|
||||
try {
|
||||
earningsDetails = await query(`
|
||||
SELECT o.id, o.amount, o.product_type, o.created_at,
|
||||
u.nickname as buyer_nickname
|
||||
FROM orders o
|
||||
JOIN users u ON o.user_id = u.id
|
||||
WHERE u.referred_by = ? AND o.status = 'paid'
|
||||
ORDER BY o.created_at DESC
|
||||
LIMIT 20
|
||||
`, [userId]) as any[]
|
||||
} catch (e) {
|
||||
// 订单表可能不存在,忽略
|
||||
}
|
||||
|
||||
// 5. 统计数据
|
||||
const stats = {
|
||||
totalReferrals: referees.length,
|
||||
activeCount: activeBindings.length,
|
||||
convertedCount: convertedBindings.length,
|
||||
expiredCount: expiredBindings.length,
|
||||
expiringCount: activeBindings.filter(b => b.daysRemaining <= 7).length
|
||||
}
|
||||
|
||||
console.log('[ReferralData] 创建绑定关系:', { referralCode, userId })
|
||||
|
||||
// TODO: 数据库操作
|
||||
// 1. 根据referralCode查找推广者
|
||||
// 2. 创建绑定关系记录
|
||||
// 3. 设置30天过期时间
|
||||
|
||||
// 模拟成功响应
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
bindingId: `binding_${Date.now()}`,
|
||||
referrerId: `referrer_${referralCode}`,
|
||||
userId,
|
||||
bindingDate: new Date().toISOString(),
|
||||
expiryDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toISOString(),
|
||||
status: 'active'
|
||||
// 收益数据
|
||||
earnings: parseFloat(user.earnings) || 0,
|
||||
pendingEarnings: parseFloat(user.pending_earnings) || 0,
|
||||
withdrawnEarnings: parseFloat(user.withdrawn_earnings) || 0,
|
||||
|
||||
// 推荐码
|
||||
referralCode: user.referral_code,
|
||||
referralCount: user.referral_count || referees.length,
|
||||
|
||||
// 绑定用户分类
|
||||
activeBindings,
|
||||
convertedBindings,
|
||||
expiredBindings,
|
||||
|
||||
// 统计
|
||||
stats,
|
||||
|
||||
// 收益明细
|
||||
earningsDetails: earningsDetails.map(e => ({
|
||||
id: e.id,
|
||||
amount: parseFloat(e.amount) * 0.9, // 90%佣金
|
||||
productType: e.product_type,
|
||||
buyerNickname: e.buyer_nickname,
|
||||
createdAt: e.created_at
|
||||
}))
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
} catch (error) {
|
||||
console.error('[ReferralData] 创建绑定关系失败:', error)
|
||||
console.error('[ReferralData] 错误:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: '创建绑定关系失败'
|
||||
error: '获取分销数据失败: ' + (error as Error).message
|
||||
}, { status: 500 })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user