import http from '../../utils/api' const baseUrl = require('../../utils/baseUrl') Page({ data: { // 用户信息 avatarUrl: '', // 原始头像路径(相对路径) avatarFullUrl: '', // 完整的头像URL(用于显示) userInfo: { user: {} }, baseUrl: baseUrl, displayNickName: '', // 显示用的昵称 // 弹窗状态 showFeedbackModal: false, showNicknameModal: false, showLogoutModal: false, showToast: false, // 反馈相关 feedbackContent: '', canSubmit: false, isSubmitting: false, // 编辑相关 newNickname: '', // 提示信息 toastText: '', // 表单数据 formData: { avatar: null, nickName: null }, // 上传状态 isUploadingAvatar: false, isUpdatingNickname: false, // 消息数字 totalToday: 0 }, onLoad() { this.gettoday() }, onShow() { console.log('个人中心页面显示,重新获取用户信息') // 每次显示页面都从服务器获取最新数据 this.getUserInfo() // 刷新消息数 this.gettoday() }, // 获取用户信息 getUserInfo() { http.UserInfo({ data: {}, success: (res) => { console.log('获取用户信息成功', res) // 更新数据 this.setData({ userInfo: res.data, displayNickName: res.data.user.nickName, avatarUrl: baseUrl + res.data.user.avatar }) // 更新缓存 wx.setStorageSync('userInfo', res.data) }, fail: (err) => { console.error('获取用户信息失败:', err) } }) }, // 问诊消息 - 获取今日消息数 gettoday() { http.today({ data: {}, success: res => { if (res.rows && res.rows[0]) { const num = res.rows[0].totalTodayReplyCount || 0 this.setData({ totalToday: num }) } }, fail: err => { console.error('获取消息数失败', err) } }) }, // 选择头像 onChooseAvatar(e) { console.log(7788, e); // 防止重复上传 if (this.data.isUploadingAvatar) { this.showToast('正在上传中...') return } const { avatarUrl } = e.detail if (!avatarUrl) { this.showToast('选择头像失败') return } this.setData({ isUploadingAvatar: true }) wx.showLoading({ title: '上传中...', mask: true }) // 上传头像到服务器 wx.uploadFile({ url: baseUrl + '/common/upload', header: { 'Authorization': 'Bearer ' + wx.getStorageSync('token') }, filePath: avatarUrl, name: 'file', success: (uploadRes) => { const result = JSON.parse(uploadRes.data) console.log('上传结果', result) if (result && result.fileName) { // 获取上传后的文件路径 const uploadedFilePath = result.fileName this.setData({ avatarUrl: uploadedFilePath, }) // 更新缓存中的用户信息 const cachedUserInfo = wx.getStorageSync('userInfo') || {} if (!cachedUserInfo.user) { cachedUserInfo.user = {} } cachedUserInfo.user.avatar = uploadedFilePath wx.setStorageSync('userInfo', cachedUserInfo) // 更新头像的API http.revise({ data: { avatar: uploadedFilePath }, success: (res) => { console.log('头像更新成功') wx.hideLoading() this.showToast('头像更新成功') // 4. 重新获取用户信息以确保数据同步 setTimeout(() => { this.getUserInfo() }, 500) }, fail: (err) => { console.error('头像更新失败:', err) wx.hideLoading() this.showToast('头像保存失败,请重试') } }) } else { throw new Error('上传失败:返回数据格式错误') } }, fail: (err) => { wx.hideLoading() console.error('上传失败:', err) this.showToast('上传失败,请检查网络') }, complete: () => { this.setData({ isUploadingAvatar: false }) } }) }, // 编辑昵称 editNickname() { this.setData({ showNicknameModal: true, newNickname: this.data.userInfo.user?.nickName || this.data.displayNickName || '' }) }, hideNicknameModal() { this.setData({ showNicknameModal: false }) }, onNicknameInput(e) { this.setData({ newNickname: e.detail.value }) }, saveNickname() { const newNickname = this.data.newNickname.trim() if (!newNickname) { this.showToast('昵称不能为空') return } if (newNickname.length > 10) { this.showToast('昵称不能超过10个字符') return } // 如果昵称没有变化,直接关闭弹窗 const currentNickName = this.data.userInfo.user?.nickName || this.data.displayNickName if (newNickname === currentNickName) { this.hideNicknameModal() return } this.setData({ isUpdatingNickname: true }) // 立即更新本地显示 this.setData({ 'userInfo.user.nickName': newNickname, displayNickName: newNickname, 'formData.nickName': newNickname }) // 更新缓存 const cachedUserInfo = wx.getStorageSync('userInfo') || {} if (!cachedUserInfo.user) { cachedUserInfo.user = {} } cachedUserInfo.user.nickName = newNickname wx.setStorageSync('userInfo', cachedUserInfo) // 更新到服务器 http.revise({ data: { nickName: newNickname }, success: (res) => { console.log('昵称更新成功') this.setData({ showNicknameModal: false, isUpdatingNickname: false, 'formData.nickName': null }) this.showToast('昵称修改成功') // 重新获取用户信息以确保数据同步 setTimeout(() => { this.getUserInfo() }, 500) }, fail: (err) => { console.error('昵称更新失败:', err) this.setData({ isUpdatingNickname: false }) this.showToast('修改失败,请重试') } }) }, // 查看问诊消息 goToConsultation() { wx.navigateTo({ url: '/pagesA/pages/todayInquiry/todayInquiry' }) }, // 查看问答消息 goToQA() { wx.navigateTo({ url: '' // 请填写实际的问答消息页面路径 }) }, // 实名认证 goToAuth() { if (this.data.userInfo.authStatus == '已认证') { this.showToast('您已完成实名认证') } else { wx.navigateTo({ url: '/pagesA/pages/attestation/attestation' }) } }, // 显示反馈弹窗 showFeedback() { this.setData({ showFeedbackModal: true, feedbackContent: '', canSubmit: false, isSubmitting: false, textareaFocus: true }) }, hideFeedback() { this.setData({ showFeedbackModal: false, textareaFocus: false }) }, // 反馈内容输入 onFeedbackInput(e) { const content = e.detail.value const canSubmit = content.trim().length > 0 this.setData({ feedbackContent: content, canSubmit }) }, // 提交反馈 submitFeedback() { if (!this.data.canSubmit || this.data.isSubmitting) return const content = this.data.feedbackContent.trim() if (content.length < 5) { this.showToast('请填写详细的反馈内容') return } this.setData({ isSubmitting: true }) http.feedback({ data: { content: content }, success: res => { if (res.code == 200) { this.showToast('感谢您的反馈!') } else { this.showToast('反馈失败!') } this.setData({ isSubmitting: false, showFeedbackModal: false, textareaFocus: false }) }, fail: (err) => { console.error('反馈失败:', err) this.showToast('反馈失败,请重试') this.setData({ isSubmitting: false }) } }) }, // 退出登录相关 showLogoutConfirm() { this.setData({ showLogoutModal: true }) }, hideLogoutModal() { this.setData({ showLogoutModal: false }) }, doLogout() { // 清除本地存储 wx.clearStorageSync() // 跳转到登录页 wx.reLaunch({ url: '/pages/login/login' }) this.showToast('已退出登录') }, // 显示提示 showToast(text) { this.setData({ toastText: text, showToast: true }) setTimeout(() => { this.setData({ showToast: false }) }, 2000) }, // 下拉刷新 onPullDownRefresh() { this.getUserInfo() this.gettoday() // 刷新消息数 setTimeout(() => { wx.stopPullDownRefresh() this.showToast('刷新成功') }, 1000) }, })