| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- 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<void> {
- 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<CustomUserInfo> {
- const raw = await apiPost<unknown>("/custom/info", {});
- 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 ?? 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<CustomUserInfo | null> {
- try {
- return await fetchCustomUserInfo();
- } catch {
- return null;
- }
- }
|