92 lines
2.7 KiB
TypeScript
92 lines
2.7 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { requireAdmin } from "@/lib/admin-auth";
|
|
import { badRequest, notFound, serverError } from "@/lib/api-error";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
function generateSlug(name: string): string {
|
|
return name
|
|
.toLowerCase()
|
|
.replace(/[^a-z0-9\u4e00-\u9fa5]+/g, "-")
|
|
.replace(/^-|-$/g, "")
|
|
|| "tool";
|
|
}
|
|
|
|
function pinyinSlug(name: string): string {
|
|
const map: Record<string, string> = {
|
|
"文本生成": "text-generation", "图像生成": "image-generation",
|
|
"视频生成": "video-generation", "音频处理": "audio-processing",
|
|
"代码助手": "code-assistant", "办公效率": "office-efficiency",
|
|
"AI 角色": "ai-character", "设计工具": "design-tools",
|
|
"数据分析": "data-analysis", "内容检测": "content-detection",
|
|
};
|
|
if (map[name]) return map[name];
|
|
const slug = generateSlug(name);
|
|
if (/^[a-z0-9-]+$/.test(slug)) return slug;
|
|
return "tool-" + Date.now().toString(36);
|
|
}
|
|
|
|
export async function POST(
|
|
_request: Request,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
const authError = await requireAdmin();
|
|
if (authError) return authError;
|
|
|
|
try {
|
|
const id = parseInt(params.id);
|
|
|
|
const pending = await prisma.pendingTool.findUnique({ where: { id } });
|
|
if (!pending) {
|
|
return NextResponse.json({ error: "待审核工具不存在" }, { status: 404 });
|
|
}
|
|
|
|
if (pending.status !== "pending") {
|
|
return NextResponse.json({ error: "该记录已被处理" }, { status: 409 });
|
|
}
|
|
|
|
let slug = pinyinSlug(pending.name);
|
|
const existing = await prisma.tool.findUnique({ where: { slug } });
|
|
if (existing) {
|
|
slug = slug + "-" + Math.floor(Math.random() * 10000);
|
|
}
|
|
|
|
const categoryId = pending.categoryId ?? ((
|
|
await prisma.category.findFirst({ orderBy: { sortOrder: "asc" } })
|
|
)?.id ?? 1);
|
|
|
|
let toolId = 0;
|
|
await prisma.$transaction(async (tx) => {
|
|
const tool = await tx.tool.create({
|
|
data: {
|
|
name: pending.name,
|
|
slug,
|
|
categoryId,
|
|
description: pending.description ?? "",
|
|
logoUrl: pending.logoUrl,
|
|
websiteUrl: pending.websiteUrl ?? "",
|
|
pricingModel: pending.pricingModel ?? "freemium",
|
|
pricingDetail: pending.pricingDetail,
|
|
features: pending.features as any,
|
|
tags: pending.tags as any,
|
|
status: "published",
|
|
},
|
|
});
|
|
toolId = tool.id;
|
|
await tx.pendingTool.delete({ where: { id } });
|
|
});
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
toolId,
|
|
message: "发布成功",
|
|
});
|
|
} catch (error) {
|
|
return serverError(error);
|
|
}
|
|
}
|
|
|
|
|