96 lines
2.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextRequest, NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth';
import { prisma } from '@/lib/prisma';
// POST - 初始化AI原图素材记录
export async function POST(request: NextRequest) {
try {
const session = await getServerSession(authOptions);
if (!session) {
return NextResponse.json({ error: '请先登录' }, { status: 401 });
}
const body = await request.json();
const { taskNo, productModel, categoryName, sceneName, platformName, productCategoryName } = body;
// 验证必填字段
if (!productModel || !categoryName || !sceneName) {
return NextResponse.json(
{ error: '缺少必填字段' },
{ status: 400 }
);
}
// 验证任务号格式(如果提供)
if (taskNo) {
const taskNoRegex = /^TASK\d{12,18}(-\d{2})*$/;
if (!taskNoRegex.test(taskNo)) {
return NextResponse.json(
{ error: '任务号格式错误' },
{ status: 400 }
);
}
// 检查任务号是否已存在(排除已删除的素材)
const existingMaterial = await prisma.material.findFirst({
where: {
taskNo,
deletedAt: null, // 只检查未删除的素材
},
select: { id: true, productModel: true, materialType: true },
});
if (existingMaterial) {
return NextResponse.json(
{ error: `任务号 ${taskNo} 已存在,对应素材:${existingMaterial.productModel}(${existingMaterial.materialType})` },
{ status: 400 }
);
}
}
// 获取用户信息
const user = await prisma.user.findUnique({
where: { id: (session.user as any).id },
select: { username: true },
});
if (!user) {
return NextResponse.json(
{ error: '用户不存在' },
{ status: 404 }
);
}
// 创建素材记录(使用临时预览图)
const material = await prisma.material.create({
data: {
materialType: '任务-AI原图',
productModel,
categoryName,
sceneName,
platformName: platformName || '',
productCategoryName: productCategoryName || '',
authorName: user.username,
userId: (session.user as any).id,
previewUrl: '', // 临时空值,完成上传时更新
zipFilePath: '', // 临时空值,完成上传时更新
zipFileName: '', // 临时空值,完成上传时更新
renderer: '', // AI原图不需要渲染器
taskNo: taskNo || null, // 任务号(可选)
},
});
return NextResponse.json({
success: true,
materialId: material.id,
});
} catch (error: any) {
console.error('[AI Original Init Error]', error);
return NextResponse.json(
{ error: error.message || '初始化失败' },
{ status: 500 }
);
}
}