import { NextRequest, NextResponse } from 'next/server'; import { getAdminSession } from '@/lib/auth'; import { prisma } from '@/lib/db'; export async function GET() { const session = await getAdminSession(); if (!session) { return NextResponse.json({ error: '未授权' }, { status: 401 }); } try { const customers = await prisma.customer.findMany({ orderBy: { createdAt: 'desc' }, include: { _count: { select: { licenses: true } }, }, }); return NextResponse.json({ customers }); } catch (error) { console.error('获取客户列表失败:', error); return NextResponse.json({ error: '服务器错误' }, { status: 500 }); } } export async function POST(request: NextRequest) { const session = await getAdminSession(); if (!session) { return NextResponse.json({ error: '未授权' }, { status: 401 }); } try { const { companyName, contactName, contactEmail, contactPhone } = await request.json(); if (!companyName) { return NextResponse.json( { success: false, error: '公司名称不能为空' }, { status: 400 } ); } const customer = await prisma.customer.create({ data: { companyName, contactName: contactName || null, contactEmail: contactEmail || null, contactPhone: contactPhone || null, }, }); return NextResponse.json({ success: true, customer }); } catch (error) { console.error('创建客户失败:', error); return NextResponse.json({ success: false, error: '服务器错误' }, { status: 500 }); } }