import { apiPost } from "@/lib/api"; import { parseLevelLabel, type UserLevel } from "@/lib/user-level"; export type CustomUserLabel = { levelLabel?: UserLevel; referralCode?: string; }; function pickString(v: unknown): string { return typeof v === "string" ? v.trim() : String(v ?? "").trim(); } export async function fetchCustomUserLabel(): Promise { const raw = await apiPost("/custom/get/label", {}); const root = raw && typeof raw === "object" ? (raw as Record) : {}; const data = root.data && typeof root.data === "object" && root.data !== null ? (root.data as Record) : root; const levelRaw = data.levelLabel ?? data.level ?? data.label ?? root.levelLabel ?? root.level ?? root.label; const levelLabel = parseLevelLabel(levelRaw) ?? undefined; const referralCode = pickString( data.referralCode ?? data.referral_code ?? data.code ?? root.referralCode ?? root.referral_code ?? root.code, ) || undefined; return { levelLabel, referralCode }; } /** 静默拉取用户标签,接口失败时返回 null,避免阻断页面渲染 */ export async function tryFetchCustomUserLabel(): Promise { try { return await fetchCustomUserLabel(); } catch { return null; } }