Files
2026-03-16 04:28:16 -05:00

64 lines
2.4 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { NextRequest, NextResponse } from "next/server";
import { resolvePaymentApiUrl } from "@/config/domain.config";
export const maxDuration = 60;
/** 开发环境优先使用本地 salonapi避免 PAYMENT_API_URL 指向远程时 404 */
function getSalonApiBase(): string {
if (process.env.NODE_ENV === "development") {
const port = process.env.PAYJSAPI_PORT || "8007";
const host = process.env.SALONAPI_HOST || "127.0.0.1";
return `http://${host}:${port}`;
}
return resolvePaymentApiUrl();
}
/** 代理到 salonapi 获取小红书用户资料 */
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const url = String(body?.url || "").trim();
if (!url) {
return NextResponse.json({ ok: false, error: "请输入小红书链接" }, { status: 400 });
}
const apiBase = getSalonApiBase();
const res = await fetch(`${apiBase}/api/salon/xiaohongshu/profile`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ url }),
signal: AbortSignal.timeout(50000),
});
const data = await res.json().catch(() => ({}));
if (res.ok && data?.ok) {
return NextResponse.json({
ok: true,
nickname: data.nickname,
gender: data.gender,
avatarUrl: data.avatarUrl ?? undefined,
followers: data.followersText ?? data.followers ?? 0,
followersText: data.followersText ?? undefined,
following: data.followingText ?? data.following ?? 0,
followingText: data.followingText ?? undefined,
likesAndCollects: data.likesAndCollectsText ?? data.likesAndCollects ?? 0,
likesAndCollectsText: data.likesAndCollectsText ?? undefined,
postCount: data.postCount ?? 0,
postCountText: data.postCountText ?? undefined,
redbookId: data.redbookId ?? undefined,
ipLocation: data.ipLocation ?? undefined,
personalLink: data.personalLink ?? undefined,
resolvedUrl: data.resolvedUrl ?? undefined,
});
}
const error = data?.detail ?? data?.error ?? "无法解析用户资料,请确认链接有效";
return NextResponse.json({ ok: false, error }, { status: res.status >= 400 ? res.status : 400 });
} catch (e) {
const msg = e instanceof Error ? e.message : "获取资料失败";
return NextResponse.json({ ok: false, error: msg }, { status: 500 });
}
}