import { apiPost } from "@/lib/api"; import { parseLevelLabel, type UserLevel } from "@/lib/user-level"; export type CustomUserInfo = { name: string; phone: string; identity: string; email?: string; levelLabel?: UserLevel; referralCode?: string; }; export async function updateUserInfo(input: { name: string; phone: string; identity: string; }): Promise { await apiPost("/custom/update/info", { name: input.name, phone: input.phone, identity: input.identity, }); } function pickString(v: unknown): string { return typeof v === "string" ? v.trim() : String(v ?? "").trim(); } export async function fetchCustomUserInfo(): Promise { const raw = await apiPost("/custom/info", {}); 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 ?? root.levelLabel; const levelLabel = parseLevelLabel(levelRaw) ?? undefined; const referralCode = pickString(data.referralCode ?? root.referralCode) || undefined; return { name: pickString(data.name ?? data.nickname ?? root.name ?? root.nickname), phone: pickString(data.phone ?? data.mobile ?? root.phone ?? root.mobile), identity: pickString(data.identity ?? data.idCard ?? root.identity ?? root.idCard), email: pickString(data.email ?? data.loginName ?? root.email ?? root.loginName) || undefined, levelLabel, referralCode, }; } /** 静默拉取用户信息,接口失败时返回 null,避免阻断页面渲染 */ export async function tryFetchCustomUserInfo(): Promise { try { return await fetchCustomUserInfo(); } catch { return null; } }