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 }); } }