feat: 完善后台管理+搜索功能+分销系统
主要更新: - 后台菜单精简(9项→6项) - 新增搜索功能(敏感信息过滤) - 分销绑定和提现系统完善 - 数据库初始化API(自动修复表结构) - 用户管理:显示绑定关系详情 - 小程序:上下章导航优化、匹配页面重构 - 修复hydration和数据类型问题
This commit is contained in:
201
app/api/referral/bind/route.ts
Normal file
201
app/api/referral/bind/route.ts
Normal file
@@ -0,0 +1,201 @@
|
||||
/**
|
||||
* 推荐码绑定API
|
||||
* 用于处理分享带来的推荐关系绑定
|
||||
*/
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { query } from '@/lib/db'
|
||||
|
||||
/**
|
||||
* POST - 绑定推荐关系
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json()
|
||||
const { userId, referralCode, openId } = body
|
||||
|
||||
// 验证参数
|
||||
const effectiveUserId = userId || (openId ? `user_${openId.slice(-8)}` : null)
|
||||
if (!effectiveUserId || !referralCode) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: '用户ID和推荐码不能为空'
|
||||
}, { status: 400 })
|
||||
}
|
||||
|
||||
// 查找推荐人
|
||||
const referrers = await query(
|
||||
'SELECT id, nickname, referral_code FROM users WHERE referral_code = ?',
|
||||
[referralCode]
|
||||
) as any[]
|
||||
|
||||
if (referrers.length === 0) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: '推荐码无效'
|
||||
}, { status: 400 })
|
||||
}
|
||||
|
||||
const referrer = referrers[0]
|
||||
|
||||
// 不能自己推荐自己
|
||||
if (referrer.id === effectiveUserId) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: '不能使用自己的推荐码'
|
||||
}, { status: 400 })
|
||||
}
|
||||
|
||||
// 检查用户是否已有推荐人
|
||||
const users = await query(
|
||||
'SELECT id, referred_by FROM users WHERE id = ? OR open_id = ?',
|
||||
[effectiveUserId, openId || effectiveUserId]
|
||||
) as any[]
|
||||
|
||||
if (users.length === 0) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: '用户不存在'
|
||||
}, { status: 400 })
|
||||
}
|
||||
|
||||
const user = users[0]
|
||||
|
||||
if (user.referred_by) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: '已绑定其他推荐人'
|
||||
}, { status: 400 })
|
||||
}
|
||||
|
||||
// 绑定推荐关系
|
||||
await query(
|
||||
'UPDATE users SET referred_by = ? WHERE id = ?',
|
||||
[referrer.id, user.id]
|
||||
)
|
||||
|
||||
// 更新推荐人的推广数量
|
||||
await query(
|
||||
'UPDATE users SET referral_count = referral_count + 1 WHERE id = ?',
|
||||
[referrer.id]
|
||||
)
|
||||
|
||||
// 创建推荐绑定记录
|
||||
const bindingId = 'bind_' + Date.now().toString(36) + Math.random().toString(36).substr(2, 6)
|
||||
const expiryDate = new Date()
|
||||
expiryDate.setDate(expiryDate.getDate() + 30) // 30天有效期
|
||||
|
||||
try {
|
||||
await query(`
|
||||
INSERT INTO referral_bindings (
|
||||
id, referrer_id, referee_id, referral_code, status, expiry_date
|
||||
) VALUES (?, ?, ?, ?, 'active', ?)
|
||||
`, [bindingId, referrer.id, user.id, referralCode, expiryDate])
|
||||
} catch (e) {
|
||||
console.log('[Referral Bind] 创建绑定记录失败(可能是重复绑定):', e)
|
||||
}
|
||||
|
||||
console.log(`[Referral Bind] 成功: ${user.id} -> ${referrer.id} (${referralCode})`)
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '绑定成功',
|
||||
referrer: {
|
||||
id: referrer.id,
|
||||
nickname: referrer.nickname
|
||||
}
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('[Referral Bind] 错误:', error)
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: '绑定失败: ' + (error as Error).message
|
||||
}, { 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, referred_by FROM users WHERE id = ?',
|
||||
[userId]
|
||||
) as any[]
|
||||
|
||||
if (users.length === 0) {
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
error: '用户不存在'
|
||||
}, { status: 404 })
|
||||
}
|
||||
|
||||
const user = users[0]
|
||||
|
||||
// 如果有推荐人,获取推荐人信息
|
||||
let referrer = null
|
||||
if (user.referred_by) {
|
||||
const referrers = await query(
|
||||
'SELECT id, nickname, avatar FROM users WHERE id = ?',
|
||||
[user.referred_by]
|
||||
) as any[]
|
||||
if (referrers.length > 0) {
|
||||
referrer = referrers[0]
|
||||
}
|
||||
}
|
||||
|
||||
// 获取该用户推荐的人
|
||||
const referees = await query(
|
||||
'SELECT id, nickname, avatar, created_at FROM users WHERE referred_by = ?',
|
||||
[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 })
|
||||
}
|
||||
}
|
||||
@@ -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