126 lines
3.8 KiB
TypeScript
126 lines
3.8 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,
|
|
{ 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/memos/${encodeURIComponent(id)}?user_id=${encodeURIComponent(user_id)}`,
|
|
{ cache: "no-store" }
|
|
);
|
|
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/memos/:id] GET error:", error);
|
|
return NextResponse.json({ ok: false, error: "服务异常" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
export async function PATCH(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
const { id } = await params;
|
|
const body = await request.json().catch(() => ({}));
|
|
const user_id = String(body?.user_id ?? body?.userId ?? "").trim();
|
|
|
|
if (!user_id) {
|
|
return NextResponse.json({ ok: false, error: "缺少 user_id" }, { status: 401 });
|
|
}
|
|
|
|
const payload: Record<string, unknown> = { user_id };
|
|
if (body?.pinned !== undefined) {
|
|
payload.pinned = Boolean(body.pinned);
|
|
}
|
|
if (body?.content !== undefined) {
|
|
payload.content = String(body.content ?? "");
|
|
}
|
|
if (body?.state !== undefined) {
|
|
payload.state = String(body.state ?? "");
|
|
}
|
|
if (body?.visibility !== undefined) {
|
|
payload.visibility = String(body.visibility ?? "");
|
|
}
|
|
|
|
try {
|
|
const apiUrl = resolvePayApiUrl(request);
|
|
const res = await fetch(`${apiUrl}/community/memos/${encodeURIComponent(id)}`, {
|
|
method: "PATCH",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(payload),
|
|
});
|
|
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/memos/:id] PATCH error:", error);
|
|
return NextResponse.json({ ok: false, error: "服务异常" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
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/memos/${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/memos/:id] DELETE error:", error);
|
|
return NextResponse.json({ ok: false, error: "服务异常" }, { status: 500 });
|
|
}
|
|
}
|