| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- 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<CustomUserLabel> {
- const raw = await apiPost<unknown>("/custom/get/label", {});
- const root = raw && typeof raw === "object" ? (raw as Record<string, unknown>) : {};
- const data =
- root.data && typeof root.data === "object" && root.data !== null
- ? (root.data as Record<string, unknown>)
- : 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<CustomUserLabel | null> {
- try {
- return await fetchCustomUserLabel();
- } catch {
- return null;
- }
- }
|