修复API + 优化用户体验

## 新增API
1. /api/user/update - 用户信息更新
2. /api/withdraw - 提现功能

## 我的页面
1. 头像使用微信原生chooseAvatar
2. 昵称使用input type="nickname"一键获取
3. ID点击复制

## 设置页面
1. 新增收货地址一键获取
2. 自动提现默认开启

## 找伙伴
1. "创业合伙"改为"找伙伴"
2. 所有匹配类型都从数据库匹配
This commit is contained in:
卡若
2026-01-29 12:44:29 +08:00
parent 3f54e1af47
commit 051f064707
8 changed files with 247 additions and 272 deletions

View File

@@ -1,19 +1,20 @@
// app/api/user/update/route.ts
// 更新用户信息(头像、昵称等)
/**
* 用户信息更新API
* 支持更新昵称、头像、手机号、微信号、支付宝、地址等
*/
import { NextRequest, NextResponse } from 'next/server'
import { getDb } from '@/lib/db'
import { query } from '@/lib/db'
export async function POST(req: NextRequest) {
export async function POST(request: NextRequest) {
try {
const { userId, nickname, avatar, phone, wechatId, address } = await req.json()
const body = await request.json()
const { userId, nickname, avatar, phone, wechat, alipay, address, autoWithdraw, withdrawAccount } = body
if (!userId) {
return NextResponse.json({ error: '缺少用户ID' }, { status: 400 })
return NextResponse.json({ success: false, message: '缺少用户ID' }, { status: 400 })
}
const db = await getDb()
// 构建更新字段
const updates: string[] = []
const values: any[] = []
@@ -30,43 +31,51 @@ export async function POST(req: NextRequest) {
updates.push('phone = ?')
values.push(phone)
}
if (wechatId !== undefined) {
updates.push('wechat_id = ?')
values.push(wechatId)
if (wechat !== undefined) {
updates.push('wechat = ?')
values.push(wechat)
}
if (alipay !== undefined) {
updates.push('alipay = ?')
values.push(alipay)
}
if (address !== undefined) {
updates.push('address = ?')
values.push(address)
}
if (updates.length === 0) {
return NextResponse.json({ error: '没有要更新的字段' }, { status: 400 })
if (autoWithdraw !== undefined) {
updates.push('auto_withdraw = ?')
values.push(autoWithdraw ? 1 : 0)
}
if (withdrawAccount !== undefined) {
updates.push('withdraw_account = ?')
values.push(withdrawAccount)
}
// 添加更新时间
updates.push('updated_at = NOW()')
if (updates.length === 1) {
return NextResponse.json({ success: false, message: '没有需要更新的字段' }, { status: 400 })
}
// 执行更新
values.push(userId)
const sql = `UPDATE users SET ${updates.join(', ')} WHERE id = ?`
await db.execute(sql, values)
// 返回更新后的用户信息
const [rows] = await db.execute(
'SELECT id, nickname, avatar, phone, wechat_id, address FROM users WHERE id = ?',
[userId]
)
const user = (rows as any[])[0]
await query(sql, values)
return NextResponse.json({
success: true,
user
message: '更新成功'
})
} catch (error) {
console.error('[User Update] Error:', error)
return NextResponse.json(
{ error: '更新用户信息失败' },
{ status: 500 }
)
return NextResponse.json({
success: false,
message: '更新失败',
error: String(error)
}, { status: 500 })
}
}