53 lines
1.5 KiB
TypeScript
53 lines
1.5 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getApiUrlFromRequest, getPaymentConfig } from "@/config/services.config";
|
|
|
|
function resolvePayApiUrl(request: NextRequest): string {
|
|
const hostHeader =
|
|
request.headers.get("host") || request.headers.get("x-forwarded-host");
|
|
return (
|
|
(hostHeader ? getApiUrlFromRequest(hostHeader) : null) ||
|
|
getPaymentConfig().apiUrl
|
|
).replace(/\/$/, "");
|
|
}
|
|
|
|
export async function POST(request: NextRequest) {
|
|
try {
|
|
const formData = await request.formData();
|
|
const image = formData.get("image");
|
|
if (!(image instanceof File)) {
|
|
return NextResponse.json(
|
|
{ ok: false, error: "缺少图片文件 image" },
|
|
{ status: 400 }
|
|
);
|
|
}
|
|
|
|
const payload = new FormData();
|
|
payload.append("image", image, image.name || "proof.jpg");
|
|
|
|
const apiUrl = resolvePayApiUrl(request);
|
|
const res = await fetch(`${apiUrl}/nomadvip/recognize_discount_proof`, {
|
|
method: "POST",
|
|
body: payload,
|
|
cache: "no-store",
|
|
});
|
|
|
|
const data = await res.json().catch(() => ({}));
|
|
if (!res.ok) {
|
|
return NextResponse.json(
|
|
{ ok: false, error: data?.error || "识别服务异常" },
|
|
{ status: res.status }
|
|
);
|
|
}
|
|
return NextResponse.json(data);
|
|
} catch (error) {
|
|
return NextResponse.json(
|
|
{
|
|
ok: false,
|
|
error: error instanceof Error ? error.message : "识别请求失败",
|
|
},
|
|
{ status: 500 }
|
|
);
|
|
}
|
|
}
|
|
|