Browse Source

修复微信头像获取与存储问题,确保所有登录入口优先使用微信用户真实头像

pull/1/head
徐飞洋 3 months ago
parent
commit
466dbfbd96
  1. 3
      pages/index/index.js
  2. 18
      pages/profile/index.js
  3. 31
      pages/seller/index.js

3
pages/index/index.js

@ -296,7 +296,8 @@ Page({
// 6. 创建用户信息
const tempUserInfo = {
nickName: userProfile ? userProfile.userInfo.nickName : '微信用户',
avatarUrl: userProfile ? userProfile.userInfo.avatarUrl : this.data.avatarUrl,
// 获取微信头像失败时使用微信默认头像,而不是本地头像
avatarUrl: userProfile ? userProfile.userInfo.avatarUrl : 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
gender: userProfile ? userProfile.userInfo.gender : 0,
country: userProfile ? userProfile.userInfo.country : '',
province: userProfile ? userProfile.userInfo.province : '',

18
pages/profile/index.js

@ -449,13 +449,14 @@ Page({
const app = getApp()
const existingUserInfo = app.globalData.userInfo || wx.getStorageSync('userInfo') || {}
const userInfo = {
nickName: existingUserInfo.nickName || (userProfile ? userProfile.userInfo.nickName : '微信用户'),
avatarUrl: existingUserInfo.avatarUrl || (userProfile ? userProfile.userInfo.avatarUrl : 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'),
gender: existingUserInfo.gender || (userProfile ? userProfile.userInfo.gender : 0),
country: existingUserInfo.country || (userProfile ? userProfile.userInfo.country : ''),
province: existingUserInfo.province || (userProfile ? userProfile.userInfo.province : ''),
city: existingUserInfo.city || (userProfile ? userProfile.userInfo.city : ''),
language: existingUserInfo.language || (userProfile ? userProfile.userInfo.language : 'zh_CN'),
// 优先使用最新获取的微信头像和昵称,如果没有获取到则使用本地存储的
nickName: (userProfile ? userProfile.userInfo.nickName : existingUserInfo.nickName) || '微信用户',
avatarUrl: (userProfile ? userProfile.userInfo.avatarUrl : existingUserInfo.avatarUrl) || 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0',
gender: (userProfile ? userProfile.userInfo.gender : existingUserInfo.gender) || 0,
country: (userProfile ? userProfile.userInfo.country : existingUserInfo.country) || '',
province: (userProfile ? userProfile.userInfo.province : existingUserInfo.province) || '',
city: (userProfile ? userProfile.userInfo.city : existingUserInfo.city) || '',
language: (userProfile ? userProfile.userInfo.language : existingUserInfo.language) || 'zh_CN',
phoneNumber: phoneNumber
}
@ -555,11 +556,12 @@ Page({
return;
}
// 构造上传数据(包含所有必要字段,包括phoneNumber)
// 构造上传数据(包含所有必要字段,包括phoneNumber和头像URL
const uploadData = {
userId: userId,
openid: openid,
nickName: userInfo.nickName,
avatarUrl: userInfo.avatarUrl, // 添加头像URL字段
phoneNumber: userInfo.phoneNumber, // 添加phoneNumber字段,满足服务器要求
type: type,
timestamp: Date.now()

31
pages/seller/index.js

@ -2373,8 +2373,9 @@ Page({
isNewPhone: isNewPhone
})
// 5. 创建临时用户信息并保存
const tempUserInfo = {
// 5. 获取用户微信头像和昵称
let userProfile = null;
let tempUserInfo = {
nickName: '微信用户',
avatarUrl: this.data.avatarUrl,
gender: 0,
@ -2383,6 +2384,32 @@ Page({
city: '',
language: 'zh_CN',
phoneNumber: finalPhoneNumber
};
try {
userProfile = await new Promise((resolve, reject) => {
wx.getUserProfile({
desc: '用于完善会员资料',
success: resolve,
fail: reject
});
});
console.log('获取用户信息成功:', userProfile);
// 更新临时用户信息
tempUserInfo = {
nickName: userProfile.userInfo.nickName,
avatarUrl: userProfile.userInfo.avatarUrl,
gender: userProfile.userInfo.gender,
country: userProfile.userInfo.country,
province: userProfile.userInfo.province,
city: userProfile.userInfo.city,
language: userProfile.userInfo.language,
phoneNumber: finalPhoneNumber
};
} catch (err) {
console.warn('获取用户信息失败:', err);
// 如果获取失败,继续使用现有临时用户信息
}
const storedUserId = wx.getStorageSync('userId')

Loading…
Cancel
Save