37 lines
1.3 KiB
TypeScript
37 lines
1.3 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 GET(request: NextRequest) {
|
|
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 params = new URLSearchParams({ user_id });
|
|
const response = await fetch(`${apiUrl}/community/stats?${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/stats] GET error:", error);
|
|
return NextResponse.json({ ok: false, error: "服务异常" }, { status: 500 });
|
|
}
|
|
}
|