用户端小程序修改

This commit is contained in:
2026-07-12 20:24:03 +08:00
parent e2dfb08de3
commit b267e885be
16 changed files with 551 additions and 214 deletions
+96 -22
View File
@@ -1,34 +1,108 @@
import Taro from '@tarojs/taro';
import { request } from './api';
type MiniProfilePayload = {
import type { UserProfile } from './api';
export type MiniWechatProfile = {
nickname?: string;
avatarUrl?: string;
};
/** 小程序授权后拉取微信昵称/头像并上报服务端 */
export async function syncMiniWechatProfile(): Promise<void> {
if (process.env.TARO_ENV !== 'weapp') return;
const WX_PROFILE_CACHE_KEY = 'wx_mini_profile_cache';
let profile: MiniProfilePayload | null = null;
export function cacheWxProfile(info: MiniWechatProfile) {
if (!info.nickname && !info.avatarUrl) return;
try {
const res = await Taro.getUserProfile({ desc: '用于完善会员资料' });
profile = {
nickname: res.userInfo?.nickName,
avatarUrl: res.userInfo?.avatarUrl,
};
Taro.setStorageSync(WX_PROFILE_CACHE_KEY, JSON.stringify(info));
} catch {
return;
}
if (!profile.nickname && !profile.avatarUrl) return;
try {
await request('/auth/wechat/mini-profile', {
method: 'POST',
data: profile,
});
} catch {
/* 用户拒绝或上报失败时不阻断主流程 */
/* ignore */
}
}
export function getCachedWxProfile(): MiniWechatProfile | null {
try {
const raw = Taro.getStorageSync(WX_PROFILE_CACHE_KEY);
if (!raw) return null;
const parsed = JSON.parse(String(raw)) as MiniWechatProfile;
if (!parsed?.nickname && !parsed?.avatarUrl) return null;
return parsed;
} catch {
return null;
}
}
export function isDefaultMiniNickname(nickname?: string | null): boolean {
if (!nickname || nickname === '访客') return true;
return /^用户\d{4}$/.test(nickname);
}
export function mergeWxDisplayProfile(profile: UserProfile): UserProfile {
if (!profile.hasWechat) return profile;
const cached = getCachedWxProfile();
if (!cached) return profile;
return {
...profile,
nickname:
cached.nickname ||
(!isDefaultMiniNickname(profile.nickname) ? profile.nickname : undefined) ||
profile.nickname ||
'微信用户',
avatarUrl: profile.avatarUrl || cached.avatarUrl || null,
};
}
/** 用户点击触发:拉取微信昵称/头像 */
export async function fetchMiniWechatUserInfo(): Promise<MiniWechatProfile> {
if (process.env.TARO_ENV !== 'weapp') {
throw new Error('请在微信小程序中授权');
}
const res = await Taro.getUserProfile({ desc: '用于完善会员资料' });
const info: MiniWechatProfile = {
nickname: res.userInfo?.nickName?.trim(),
avatarUrl: res.userInfo?.avatarUrl?.trim(),
};
if (!info.nickname && !info.avatarUrl) {
throw new Error('未获取到微信头像或昵称');
}
cacheWxProfile(info);
return info;
}
export async function uploadMiniWechatProfile(info: MiniWechatProfile): Promise<MiniWechatProfile | null> {
if (!info.nickname && !info.avatarUrl) return null;
try {
const updated = await request<{
nickname?: string | null;
avatarUrl?: string | null;
}>('/auth/wechat/mini-profile', {
method: 'POST',
data: info,
});
if (updated?.nickname || updated?.avatarUrl) {
cacheWxProfile({
nickname: updated.nickname ?? info.nickname,
avatarUrl: updated.avatarUrl ?? info.avatarUrl,
});
}
return info;
} catch {
return null;
}
}
/** 绑定后上报微信资料(优先使用已拉取的信息,避免重复弹窗) */
export async function syncMiniWechatProfile(prefetched?: MiniWechatProfile | null): Promise<MiniWechatProfile | null> {
if (process.env.TARO_ENV !== 'weapp') return null;
let info = prefetched ?? null;
if (!info) {
try {
info = await fetchMiniWechatUserInfo();
} catch {
return getCachedWxProfile();
}
}
await uploadMiniWechatProfile(info);
return info;
}