/** * VIP会员状态查询 */ import { NextRequest, NextResponse } from 'next/server' import { query, getConfig } from '@/lib/db' 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 is_vip, vip_expire_date, vip_name, vip_project, vip_contact, vip_avatar, vip_bio, has_full_book, nickname, avatar FROM users WHERE id = ?`, [userId] ) as any[] if (!rows.length) { return NextResponse.json({ success: false, error: '用户不存在' }, { status: 404 }) } const user = rows[0] const now = new Date() const isVip = user.is_vip && user.vip_expire_date && new Date(user.vip_expire_date) > now // 若过期则自动标记 if (user.is_vip && !isVip) { await query('UPDATE users SET is_vip = FALSE WHERE id = ?', [userId]).catch(() => {}) } let vipPrice = 1980 let vipRights: string[] = [] try { const priceConfig = await getConfig('vip_price') if (priceConfig) vipPrice = Number(priceConfig) || 1980 const rightsConfig = await getConfig('vip_rights') if (rightsConfig) vipRights = Array.isArray(rightsConfig) ? rightsConfig : JSON.parse(rightsConfig) } catch { /* 使用默认 */ } if (!vipRights.length) { vipRights = [ '解锁全部章节内容(365天)', '匹配所有创业伙伴', '创业老板排行榜展示', '专属VIP标识' ] } return NextResponse.json({ success: true, data: { isVip, expireDate: user.vip_expire_date, daysRemaining: isVip ? Math.ceil((new Date(user.vip_expire_date).getTime() - now.getTime()) / 86400000) : 0, profile: { name: user.vip_name || '', project: user.vip_project || '', contact: user.vip_contact || '', avatar: user.vip_avatar || user.avatar || '', bio: user.vip_bio || '' }, price: vipPrice, rights: vipRights } }) } catch (error) { console.error('[VIP Status]', error) return NextResponse.json({ success: false, error: '查询失败' }, { status: 500 }) } }