微信小程序端登录和头像调整

This commit is contained in:
2026-07-21 08:59:44 +08:00
parent c5f526d51d
commit 350a086a73
10 changed files with 84 additions and 263 deletions
+14 -23
View File
@@ -1,5 +1,5 @@
import Taro from '@tarojs/taro';
import { API_BASE, CLIENT_APP, getToken, request, type UserProfile } from './api';
import type { UserProfile } from './api';
export type MiniWechatProfile = {
nickname?: string;
@@ -57,6 +57,7 @@ export function mergeWxDisplayProfile(profile: UserProfile): UserProfile {
/** 上传 chooseAvatar 临时文件到 OSS,返回永久 URL */
export async function uploadAvatarTempFile(tempFilePath: string): Promise<string> {
const { API_BASE, CLIENT_APP, getToken } = await import('./api');
const token = getToken();
if (!token) throw new Error('请先登录');
@@ -91,6 +92,7 @@ export async function uploadAvatarTempFile(tempFilePath: string): Promise<string
export async function uploadMiniWechatProfile(info: MiniWechatProfile): Promise<UserProfile | null> {
if (!info.nickname && !info.avatarUrl) return null;
const { request } = await import('./api');
const updated = await request<UserProfile>('/auth/wechat/mini-profile', {
method: 'POST',
data: info,
@@ -103,26 +105,16 @@ export async function uploadMiniWechatProfile(info: MiniWechatProfile): Promise<
}
/**
* @deprecated getUserProfile 已收回真实头像昵称,仅作兼容;小程序请用 chooseAvatar + nickname
* 兼容旧调用:getUserProfile 已无法拿到真实头像昵称
* 始终导出为函数,避免循环依赖/旧包出现 “is not a function”。
*/
export async function fetchMiniWechatUserInfo(): Promise<MiniWechatProfile> {
if (process.env.TARO_ENV !== 'weapp') {
throw new Error('请在微信小程序中授权');
const cached = getCachedWxProfile();
if (cached?.nickname || cached?.avatarUrl) {
return cached;
}
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('未获取到微信头像或昵称');
}
// 灰色默认头像 / 「微信用户」视为无效,需走填写能力
if (info.nickname === '微信用户' || !info.avatarUrl) {
throw new Error('请使用头像昵称填写能力完善资料');
}
cacheWxProfile(info);
return info;
// 不再弹 getUserProfile;引导走「我的」页 chooseAvatar / nickname
throw new Error('请在「我的」页点击头像完善微信头像和昵称');
}
/** 绑定后上报微信资料(优先使用已拉取的信息) */
@@ -130,9 +122,8 @@ export async function syncMiniWechatProfile(
prefetched?: MiniWechatProfile | null,
): Promise<MiniWechatProfile | null> {
if (process.env.TARO_ENV !== 'weapp') return null;
if (!prefetched?.nickname && !prefetched?.avatarUrl) {
return getCachedWxProfile();
}
await uploadMiniWechatProfile(prefetched);
return prefetched;
const info = prefetched?.nickname || prefetched?.avatarUrl ? prefetched : getCachedWxProfile();
if (!info?.nickname && !info?.avatarUrl) return null;
await uploadMiniWechatProfile(info);
return info;
}
+26 -1
View File
@@ -3,7 +3,32 @@ import { isWxAuthorizeEnabled } from '@dukang/shared-types';
import { stripOAuthParamsFromLocation } from '@dukang/weixin-sdk';
import Taro from '@tarojs/taro';
import { saveAuth } from './api';
import { syncMiniWechatProfile } from './mini-wechat-profile';
import {
fetchMiniWechatUserInfo,
mergeWxDisplayProfile,
needsWxProfileFill,
syncMiniWechatProfile,
} from './mini-wechat-profile';
/**
* 兼容旧分包对资料 helper 的引用,避免 tree-shake 后出现 is not a function
*(开发者工具热更新时常见旧页 + 新 common 混用)
*/
export { fetchMiniWechatUserInfo, mergeWxDisplayProfile, needsWxProfileFill };
/** 强制保留导出绑定,防止打包器删掉未引用的 re-export */
const _wxProfileCompat = {
fetchMiniWechatUserInfo,
mergeWxDisplayProfile,
needsWxProfileFill,
};
if (
typeof _wxProfileCompat.needsWxProfileFill !== 'function' ||
typeof _wxProfileCompat.fetchMiniWechatUserInfo !== 'function' ||
typeof _wxProfileCompat.mergeWxDisplayProfile !== 'function'
) {
throw new Error('mini-wechat-profile helpers missing');
}
import {
authorizeWechatForPay,
fetchClientConfig,