fix: match Xingtu profile endpoints

This commit is contained in:
2026-05-18 17:54:37 +08:00
parent c8287e8d8e
commit ca9ce02db5
4 changed files with 77 additions and 27 deletions
+32 -10
View File
@@ -16,6 +16,16 @@ type FetchLike = (
init?: RequestInit
) => Promise<FetchResponseLike>;
export type AudienceProfileRequestTarget =
| {
linkType: number;
source: "audienceDistribution";
}
| {
authorType: number;
source: "fansDistribution";
};
interface AudienceProfileClientOptions {
baseUrl?: string;
fetchImpl?: FetchLike;
@@ -49,10 +59,13 @@ const GENDER_LABELS: Record<string, string> = {
const AGE_ORDER = ["18-23", "24-30", "31-40", "41-50", "50+"];
const CITY_TIER_ORDER = ["一线", "新一线", "二线", "三线", "四线", "五线"];
export const AUDIENCE_PROFILE_LINK_TYPES: Record<AudienceProfileKind, number> = {
audience: 3,
fans: 1,
longtimeFans: 4
export const AUDIENCE_PROFILE_TARGETS: Record<
AudienceProfileKind,
AudienceProfileRequestTarget
> = {
audience: { linkType: 5, source: "audienceDistribution" },
fans: { authorType: 1, source: "fansDistribution" },
longtimeFans: { authorType: 5, source: "fansDistribution" }
};
export function createAudienceProfileClient(
@@ -65,14 +78,14 @@ export function createAudienceProfileClient(
return {
async loadAudienceProfile(
record: MarketRecord,
linkType: number
target: AudienceProfileRequestTarget
): Promise<AudienceProfileResult> {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
try {
const response = await fetchImpl(
buildAudienceProfileUrl(record.authorId, baseUrl, linkType),
buildAudienceProfileUrl(record.authorId, baseUrl, target),
{
credentials: "include",
method: "GET",
@@ -106,13 +119,22 @@ export function createAudienceProfileClient(
export function buildAudienceProfileUrl(
authorId: string,
baseUrl: string,
linkType = 3
target: AudienceProfileRequestTarget
): string {
const url = new URL("/gw/api/data_sp/author_audience_distribution", baseUrl);
const url = new URL(
target.source === "audienceDistribution"
? "/gw/api/data_sp/author_audience_distribution"
: "/gw/api/data_sp/get_author_fans_distribution",
baseUrl
);
url.searchParams.set("o_author_id", authorId);
url.searchParams.set("platform_source", "1");
url.searchParams.set("platform_channel", "1");
url.searchParams.set("link_type", String(linkType));
if (target.source === "audienceDistribution") {
url.searchParams.set("platform_channel", "1");
url.searchParams.set("link_type", String(target.linkType));
} else {
url.searchParams.set("author_type", String(target.authorType));
}
return url.toString();
}