2025-12-24 16:42:58 +08:00

40 lines
1.0 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server'
import { getServerSession } from 'next-auth/next'
import { authOptions } from '@/lib/authOptions'
import { db } from '@/lib/db'
export const dynamic = 'force-dynamic'
function parseCallback(raw?: string | null): Record<string, any> {
if (!raw) return {}
try {
return JSON.parse(raw)
} catch {
return { raw }
}
}
export async function GET(request: NextRequest) {
try {
const session = await getServerSession(authOptions)
if (!session || !session.user?.id) {
return NextResponse.json({ message: '请先登录' }, { status: 401 })
}
const userId = parseInt(session.user.id)
const orders = await db.getUserOrders(userId)
return NextResponse.json({
success: true,
orders: orders.map((o) => ({
...o,
metadata: parseCallback(o.callback_data),
})),
})
} catch (error) {
console.error('获取订单列表失败:', error)
return NextResponse.json({ message: '获取订单失败' }, { status: 500 })
}
}