export const WECHAT_PROFILE_KEY = "miniapp:wechat-profile"; export type WechatProfile = { avatarUrl: string; nickName?: string; }; function isWechatProfile(value: unknown): value is WechatProfile { if (!value || typeof value !== "object") return false; const profile = value as Partial; return typeof profile.avatarUrl === "string" && profile.avatarUrl.trim().length > 0; } export function getStoredWechatProfile(): WechatProfile | null { try { const value = uni.getStorageSync(WECHAT_PROFILE_KEY); return isWechatProfile(value) ? value : null; } catch { return null; } } export function saveWechatProfile(profile: WechatProfile) { if (!isWechatProfile(profile)) return; uni.setStorageSync(WECHAT_PROFILE_KEY, profile); } export function requestWechatProfile(): Promise { return new Promise((resolve) => { if (typeof uni.getUserProfile !== "function") { resolve(null); return; } uni.getUserProfile({ provider: "weixin", desc: "用于展示您的微信头像", lang: "zh_CN", success: (result) => { const profile = { avatarUrl: result.userInfo?.avatarUrl?.trim() ?? "", nickName: result.userInfo?.nickName?.trim() || undefined, }; resolve(isWechatProfile(profile) ? profile : null); }, fail: () => resolve(null), }); }); }