's'
This commit is contained in:
42
app/api/community/attachments/route.ts
Normal file
42
app/api/community/attachments/route.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
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 GET(request: NextRequest) {
|
||||
const { searchParams } = new URL(request.url);
|
||||
const user_id = searchParams.get("user_id")?.trim() || "";
|
||||
const page = Math.max(1, Number.parseInt(searchParams.get("page") || "1", 10));
|
||||
const per_page = Math.min(60, Math.max(1, Number.parseInt(searchParams.get("per_page") || "24", 10)));
|
||||
|
||||
if (!user_id) {
|
||||
return NextResponse.json({ ok: false, error: "缺少 user_id" }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const apiUrl = resolvePayApiUrl(request);
|
||||
const params = new URLSearchParams({
|
||||
user_id,
|
||||
page: String(page),
|
||||
per_page: String(per_page),
|
||||
});
|
||||
const response = await fetch(`${apiUrl}/community/attachments?${params.toString()}`, { cache: "no-store" });
|
||||
const data = await response.json().catch(() => ({}));
|
||||
|
||||
if (!response.ok) {
|
||||
return NextResponse.json(
|
||||
{ ok: false, error: data?.detail || data?.error || "请求失败" },
|
||||
{ status: response.status }
|
||||
);
|
||||
}
|
||||
|
||||
return NextResponse.json(data);
|
||||
} catch (error) {
|
||||
console.error("[community/attachments] GET error:", error);
|
||||
return NextResponse.json({ ok: false, error: "服务异常" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user