微信小程序端登录和头像调整
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user