Browse Source

更新头像切换功能,修复多张头像解析问题

pull/18/head
Default User 1 month ago
parent
commit
87bbe8b13c
  1. 164
      pages/profile/index.js

164
pages/profile/index.js

@ -25,7 +25,9 @@ Page({
needPhoneAuth: false, // 是否需要重新授权手机号 needPhoneAuth: false, // 是否需要重新授权手机号
locationInfo: '', // 位置信息 locationInfo: '', // 位置信息
hasLocationAuth: false, // 是否已经授权位置 hasLocationAuth: false, // 是否已经授权位置
isInPersonnel: false // 用户手机号是否在personnel表中 isInPersonnel: false, // 用户手机号是否在personnel表中
currentAvatarIndex: 0, // 当前显示的头像索引,默认显示第一张
rawAvatarUrl: null // 存储原始的头像URL数据(可能是数组)
}, },
onLoad() { onLoad() {
@ -95,19 +97,27 @@ Page({
const localUserInfo = wx.getStorageSync('userInfo') || {} const localUserInfo = wx.getStorageSync('userInfo') || {}
if (app.globalData.userInfo) { if (app.globalData.userInfo) {
const userInfo = { ...app.globalData.userInfo } const userInfo = { ...app.globalData.userInfo }
// 处理头像URL // 保存原始的头像URL数据
const rawAvatarUrl = userInfo.avatarUrl;
// 处理头像URL用于显示
userInfo.avatarUrl = this.processAvatarUrl(userInfo.avatarUrl); userInfo.avatarUrl = this.processAvatarUrl(userInfo.avatarUrl);
userInfo.hiddenPhoneNumber = this.hidePhoneNumber(userInfo.phoneNumber) userInfo.hiddenPhoneNumber = this.hidePhoneNumber(userInfo.phoneNumber)
this.setData({ userInfo }) this.setData({
userInfo,
rawAvatarUrl
})
} else { } else {
const userInfo = { ...localUserInfo } const userInfo = { ...localUserInfo }
// 处理头像URL // 保存原始的头像URL数据
const rawAvatarUrl = userInfo.avatarUrl;
// 处理头像URL用于显示
userInfo.avatarUrl = this.processAvatarUrl(userInfo.avatarUrl); userInfo.avatarUrl = this.processAvatarUrl(userInfo.avatarUrl);
userInfo.hiddenPhoneNumber = this.hidePhoneNumber(userInfo.phoneNumber) userInfo.hiddenPhoneNumber = this.hidePhoneNumber(userInfo.phoneNumber)
app.globalData.userInfo = userInfo app.globalData.userInfo = userInfo
this.setData({ this.setData({
userInfo, userInfo,
needPhoneAuth: !userInfo.phoneNumber needPhoneAuth: !userInfo.phoneNumber,
rawAvatarUrl
}) })
} }
@ -234,7 +244,9 @@ Page({
...serverUserInfo ...serverUserInfo
} }
// 处理头像URL // 保存原始的头像URL数据
const rawAvatarUrl = updatedUserInfo.avatarUrl;
// 处理头像URL用于显示
updatedUserInfo.avatarUrl = this.processAvatarUrl(updatedUserInfo.avatarUrl); updatedUserInfo.avatarUrl = this.processAvatarUrl(updatedUserInfo.avatarUrl);
// 添加隐藏的电话号码 // 添加隐藏的电话号码
@ -242,7 +254,10 @@ Page({
app.globalData.userInfo = updatedUserInfo app.globalData.userInfo = updatedUserInfo
wx.setStorageSync('userInfo', updatedUserInfo) wx.setStorageSync('userInfo', updatedUserInfo)
this.setData({ userInfo: updatedUserInfo }) this.setData({
userInfo: updatedUserInfo,
rawAvatarUrl
})
// 同步更新用户身份信息(当前身份由数据库决定) // 同步更新用户身份信息(当前身份由数据库决定)
if (serverUserInfo.type) { if (serverUserInfo.type) {
@ -270,7 +285,9 @@ Page({
...serverUserInfo ...serverUserInfo
} }
// 处理头像URL // 保存原始的头像URL数据
const rawAvatarUrl = updatedUserInfo.avatarUrl;
// 处理头像URL用于显示
updatedUserInfo.avatarUrl = this.processAvatarUrl(updatedUserInfo.avatarUrl); updatedUserInfo.avatarUrl = this.processAvatarUrl(updatedUserInfo.avatarUrl);
// 添加隐藏的电话号码 // 添加隐藏的电话号码
@ -278,7 +295,10 @@ Page({
app.globalData.userInfo = updatedUserInfo app.globalData.userInfo = updatedUserInfo
wx.setStorageSync('userInfo', updatedUserInfo) wx.setStorageSync('userInfo', updatedUserInfo)
this.setData({ userInfo: updatedUserInfo }) this.setData({
userInfo: updatedUserInfo,
rawAvatarUrl
})
// 同步更新用户身份信息(当前身份由数据库决定) // 同步更新用户身份信息(当前身份由数据库决定)
if (serverUserInfo.type) { if (serverUserInfo.type) {
@ -1114,74 +1134,80 @@ Page({
}); });
}, },
// 点击头像换图片 // 点击头像换图片
onAvatarClick() { onAvatarClick() {
wx.chooseImage({ // 使用rawAvatarUrl获取完整的头像URL数组
count: 1, let avatarUrls = [];
sizeType: ['compressed'], const rawAvatarUrl = this.data.rawAvatarUrl;
sourceType: ['album', 'camera'],
success: (res) => { if (rawAvatarUrl) {
const tempFilePaths = res.tempFilePaths; if (typeof rawAvatarUrl === 'string') {
if (tempFilePaths && tempFilePaths.length > 0) { // 如果是字符串,尝试解析为JSON数组
this.uploadAvatar(tempFilePaths[0]); try {
} const parsed = JSON.parse(rawAvatarUrl);
}, if (Array.isArray(parsed)) {
fail: (err) => { avatarUrls = parsed;
console.error('选择图片失败:', err); } else {
} // 如果不是数组,将当前URL作为第一个元素
}); avatarUrls = [rawAvatarUrl];
}, }
} catch (e) {
// 上传头像并更新 // 解析失败,将当前URL作为第一个元素
async uploadAvatar(filePath) { avatarUrls = [rawAvatarUrl];
wx.showLoading({ title: '上传中...', mask: true });
try {
// 上传图片到服务器
const API = require('../../utils/api.js');
const result = await API.uploadSettlementFile(filePath, 'avatar');
if (result && result.fileUrl) {
const avatarUrl = result.fileUrl;
// 更新本地和全局用户信息
const app = getApp();
const updatedUserInfo = {
...this.data.userInfo,
avatarUrl: avatarUrl
};
// 保存到本地存储和全局状态
app.globalData.userInfo = updatedUserInfo;
wx.setStorageSync('userInfo', updatedUserInfo);
// 更新页面显示
this.setData({ userInfo: updatedUserInfo });
// 上传到服务器更新数据库
const userId = wx.getStorageSync('userId');
if (userId) {
await this.uploadUserInfoToServer(updatedUserInfo, userId, this.data.userType);
} }
} else if (Array.isArray(rawAvatarUrl)) {
wx.showToast({ // 如果已经是数组,直接使用
title: '头像更新成功', avatarUrls = rawAvatarUrl;
icon: 'success',
duration: 2000
});
} else {
throw new Error('上传失败');
} }
} catch (error) { }
console.error('上传头像失败:', error);
// 检查是否有至少两张图片可以切换
if (avatarUrls.length < 2) {
wx.showToast({ wx.showToast({
title: '上传失败,请重试', title: '没有足够的头像可以切换',
icon: 'none', icon: 'none',
duration: 2000 duration: 2000
}); });
} finally { return;
wx.hideLoading(); }
// 切换到下一张头像
let nextIndex = this.data.currentAvatarIndex + 1;
if (nextIndex >= avatarUrls.length) {
nextIndex = 0; // 如果已经是最后一张,回到第一张
} }
// 显示切换头像的提示
wx.showModal({
title: '切换头像',
content: `确定要切换到第${nextIndex + 1}张头像吗?`,
success: (res) => {
if (res.confirm) {
// 切换到下一张图片
const app = getApp();
const updatedUserInfo = {
...this.data.userInfo,
avatarUrl: avatarUrls[nextIndex] // 直接使用下一张图片
};
// 保存到本地存储和全局状态
app.globalData.userInfo = updatedUserInfo;
wx.setStorageSync('userInfo', updatedUserInfo);
// 更新页面显示
this.setData({
userInfo: updatedUserInfo,
currentAvatarIndex: nextIndex
});
wx.showToast({
title: '头像切换成功',
icon: 'success',
duration: 2000
});
}
}
});
}, },
}) })

Loading…
Cancel
Save