43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getApiUrlFromRequest, getPaymentConfig } from "@/config/services.config";
|
|
|
|
function resolvePayApiUrl(request: NextRequest): string {
|
|
const payConfig = getPaymentConfig();
|
|
const hostHeader = request.headers.get("host") || request.headers.get("x-forwarded-host");
|
|
return ((hostHeader ? getApiUrlFromRequest(hostHeader) : null) || payConfig.apiUrl).replace(/\/$/, "");
|
|
}
|
|
|
|
export async function DELETE(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id } = await params;
|
|
const { searchParams } = new URL(request.url);
|
|
const user_id = searchParams.get("user_id")?.trim() || "";
|
|
|
|
if (!user_id) {
|
|
return NextResponse.json({ ok: false, error: "缺少 user_id" }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const apiUrl = resolvePayApiUrl(request);
|
|
const res = await fetch(
|
|
`${apiUrl}/community/comments/${encodeURIComponent(id)}?user_id=${encodeURIComponent(user_id)}`,
|
|
{ method: "DELETE" }
|
|
);
|
|
const data = await res.json().catch(() => ({}));
|
|
|
|
if (!res.ok) {
|
|
return NextResponse.json(
|
|
{ ok: false, error: data?.detail || "删除评论失败" },
|
|
{ status: res.status }
|
|
);
|
|
}
|
|
|
|
return NextResponse.json(data);
|
|
} catch (error) {
|
|
console.error("[community/comments/:id] DELETE error:", error);
|
|
return NextResponse.json({ ok: false, error: "服务异常" }, { status: 500 });
|
|
}
|
|
}
|