## 新增API 1. /api/user/update - 用户信息更新 2. /api/withdraw - 提现功能 ## 我的页面 1. 头像使用微信原生chooseAvatar 2. 昵称使用input type="nickname"一键获取 3. ID点击复制 ## 设置页面 1. 新增收货地址一键获取 2. 自动提现默认开启 ## 找伙伴 1. "创业合伙"改为"找伙伴" 2. 所有匹配类型都从数据库匹配
102 lines
3.0 KiB
TypeScript
102 lines
3.0 KiB
TypeScript
/**
|
|
* 提现API
|
|
* 用户提现到微信零钱或支付宝
|
|
*/
|
|
|
|
import { NextRequest, NextResponse } from 'next/server'
|
|
import { query } from '@/lib/db'
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const body = await request.json()
|
|
const { userId, amount } = body
|
|
|
|
if (!userId) {
|
|
return NextResponse.json({ success: false, message: '缺少用户ID' }, { status: 400 })
|
|
}
|
|
|
|
if (!amount || amount <= 0) {
|
|
return NextResponse.json({ success: false, message: '提现金额无效' }, { status: 400 })
|
|
}
|
|
|
|
// 查询用户信息
|
|
const users = await query('SELECT * FROM users WHERE id = ?', [userId]) as any[]
|
|
if (!users || users.length === 0) {
|
|
return NextResponse.json({ success: false, message: '用户不存在' }, { status: 404 })
|
|
}
|
|
|
|
const user = users[0]
|
|
|
|
// 检查是否绑定支付方式
|
|
if (!user.wechat && !user.alipay) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '请先绑定微信号或支付宝',
|
|
needBind: true
|
|
}, { status: 400 })
|
|
}
|
|
|
|
// 查询可提现金额(待结算收益)
|
|
const earningsResult = await query(`
|
|
SELECT COALESCE(SUM(commission), 0) as total_commission
|
|
FROM referral_bindings
|
|
WHERE referrer_id = ? AND status = 'converted'
|
|
`, [userId]) as any[]
|
|
|
|
const totalEarnings = parseFloat(earningsResult[0]?.total_commission || 0)
|
|
|
|
// 查询已提现金额
|
|
const withdrawnResult = await query(`
|
|
SELECT COALESCE(SUM(amount), 0) as withdrawn
|
|
FROM withdrawals
|
|
WHERE user_id = ? AND status = 'completed'
|
|
`, [userId]) as any[]
|
|
|
|
const withdrawnAmount = parseFloat(withdrawnResult[0]?.withdrawn || 0)
|
|
const availableAmount = totalEarnings - withdrawnAmount
|
|
|
|
if (amount > availableAmount) {
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: `可提现金额不足,当前可提现 ¥${availableAmount.toFixed(2)}`
|
|
}, { status: 400 })
|
|
}
|
|
|
|
// 创建提现记录
|
|
const withdrawId = `W${Date.now()}`
|
|
await query(`
|
|
INSERT INTO withdrawals (id, user_id, amount, account_type, account, status, created_at)
|
|
VALUES (?, ?, ?, ?, ?, 'pending', NOW())
|
|
`, [
|
|
withdrawId,
|
|
userId,
|
|
amount,
|
|
user.alipay ? 'alipay' : 'wechat',
|
|
user.alipay || user.wechat
|
|
])
|
|
|
|
// TODO: 实际调用微信企业付款或支付宝转账API
|
|
// 这里先模拟成功
|
|
await query(`UPDATE withdrawals SET status = 'completed', completed_at = NOW() WHERE id = ?`, [withdrawId])
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
message: '提现成功',
|
|
data: {
|
|
withdrawId,
|
|
amount,
|
|
account: user.alipay || user.wechat,
|
|
accountType: user.alipay ? '支付宝' : '微信'
|
|
}
|
|
})
|
|
|
|
} catch (error) {
|
|
console.error('[Withdraw] Error:', error)
|
|
return NextResponse.json({
|
|
success: false,
|
|
message: '提现失败',
|
|
error: String(error)
|
|
}, { status: 500 })
|
|
}
|
|
}
|