From 8bcbe163e8a1abb4996d27cdd62f9cba40a6290d Mon Sep 17 00:00:00 2001 From: Default User Date: Wed, 28 Jan 2026 16:45:51 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=A4=B4=E5=83=8F=E6=9B=B4?= =?UTF-8?q?=E6=8D=A2=E5=8A=9F=E8=83=BD=EF=BC=9A=E6=B7=BB=E5=8A=A0onAvatarC?= =?UTF-8?q?lick=E6=96=B9=E6=B3=95=EF=BC=8C=E7=82=B9=E5=87=BB=E5=A4=B4?= =?UTF-8?q?=E5=83=8F=E6=97=B6=E5=BC=B9=E5=87=BA=E6=9B=B4=E6=8D=A2=E9=80=89?= =?UTF-8?q?=E6=8B=A9=EF=BC=8C=E4=B8=8A=E4=BC=A0=E5=90=8E=E6=9B=B4=E6=96=B0?= =?UTF-8?q?avatarUrl=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/profile/index.js | 70 ++++++++++++++++++++++++++++++++++++++++ pages/profile/index.wxml | 3 +- 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/pages/profile/index.js b/pages/profile/index.js index 43fe239..3de6a01 100644 --- a/pages/profile/index.js +++ b/pages/profile/index.js @@ -1114,4 +1114,74 @@ 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); + } + + wx.showToast({ + title: '头像更新成功', + icon: 'success', + duration: 2000 + }); + } else { + throw new Error('上传失败'); + } + } catch (error) { + console.error('上传头像失败:', error); + wx.showToast({ + title: '上传失败,请重试', + icon: 'none', + duration: 2000 + }); + } finally { + wx.hideLoading(); + } + }, + }) diff --git a/pages/profile/index.wxml b/pages/profile/index.wxml index 635b06e..b3762cc 100644 --- a/pages/profile/index.wxml +++ b/pages/profile/index.wxml @@ -3,7 +3,8 @@ {{userInfo.hiddenPhoneNumber || userInfo.phoneNumber || '未登录'}}