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