/** * VIP会员资料填写/更新 */ import { NextRequest, NextResponse } from 'next/server' import { query } from '@/lib/db' export async function POST(request: NextRequest) { try { const { userId, name, project, contact, avatar, bio } = await request.json() if (!userId) { return NextResponse.json({ success: false, error: '缺少userId' }, { status: 400 }) } const users = await query('SELECT is_vip, vip_expire_date FROM users WHERE id = ?', [userId]) as any[] if (!users.length) { return NextResponse.json({ success: false, error: '用户不存在' }, { status: 404 }) } const user = users[0] if (!user.is_vip || !user.vip_expire_date || new Date(user.vip_expire_date) <= new Date()) { return NextResponse.json({ success: false, error: '仅VIP会员可填写资料' }, { status: 403 }) } const updates: string[] = [] const params: any[] = [] if (name !== undefined) { updates.push('vip_name = ?'); params.push(name) } if (project !== undefined) { updates.push('vip_project = ?'); params.push(project) } if (contact !== undefined) { updates.push('vip_contact = ?'); params.push(contact) } if (avatar !== undefined) { updates.push('vip_avatar = ?'); params.push(avatar) } if (bio !== undefined) { updates.push('vip_bio = ?'); params.push(bio) } if (!updates.length) { return NextResponse.json({ success: false, error: '无更新内容' }, { status: 400 }) } params.push(userId) await query(`UPDATE users SET ${updates.join(', ')} WHERE id = ?`, params) return NextResponse.json({ success: true, message: '资料已更新' }) } catch (error) { console.error('[VIP Profile]', error) return NextResponse.json({ success: false, error: '更新失败' }, { status: 500 }) } } export async function GET(request: NextRequest) { const userId = new URL(request.url).searchParams.get('userId') if (!userId) { return NextResponse.json({ success: false, error: '缺少userId' }, { status: 400 }) } try { const rows = await query( 'SELECT vip_name, vip_project, vip_contact, vip_avatar, vip_bio FROM users WHERE id = ?', [userId] ) as any[] if (!rows.length) { return NextResponse.json({ success: false, error: '用户不存在' }, { status: 404 }) } return NextResponse.json({ success: true, data: { name: rows[0].vip_name || '', project: rows[0].vip_project || '', contact: rows[0].vip_contact || '', avatar: rows[0].vip_avatar || '', bio: rows[0].vip_bio || '' } }) } catch (error) { console.error('[VIP Profile GET]', error) return NextResponse.json({ success: false, error: '查询失败' }, { status: 500 }) } }