You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
357 lines
7.6 KiB
357 lines
7.6 KiB
import http from '../../utils/api'
|
|
const baseUrl = require('../../utils/baseUrl')
|
|
|
|
Page({
|
|
data: {
|
|
// 用户信息
|
|
avatarUrl: '',
|
|
userInfo: {
|
|
user: {}
|
|
},
|
|
baseUrl: baseUrl,
|
|
|
|
// 弹窗状态
|
|
showFeedbackModal: false,
|
|
showNicknameModal: false,
|
|
showLogoutModal: false,
|
|
showToast: false,
|
|
|
|
// 反馈相关
|
|
feedbackContent: '',
|
|
canSubmit: false,
|
|
isSubmitting: false,
|
|
|
|
// 编辑相关
|
|
newNickname: '',
|
|
|
|
// 提示信息
|
|
toastText: '',
|
|
|
|
// 表单数据
|
|
formData: {
|
|
avatar: null,
|
|
nickName: null
|
|
},
|
|
|
|
// 上传状态
|
|
isUploadingAvatar: false,
|
|
isUpdatingNickname: false
|
|
},
|
|
|
|
onLoad() {
|
|
this.getUserInfo()
|
|
},
|
|
|
|
onShow() {
|
|
this.getUserInfo()
|
|
},
|
|
|
|
// 获取用户信息
|
|
getUserInfo() {
|
|
http.UserInfo({
|
|
data: {},
|
|
success: (res) => {
|
|
if (res.data && res.data.user) {
|
|
wx.setStorageSync('userInfo', res.data)
|
|
this.setData({
|
|
userInfo: res.data,
|
|
avatarUrl: res.data.user?.avatar ? baseUrl + res.data.user.avatar : ''
|
|
})
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
console.error('获取用户信息失败:', err)
|
|
// 使用缓存的用户信息
|
|
const cachedUserInfo = wx.getStorageSync('userInfo')
|
|
if (cachedUserInfo) {
|
|
this.setData({
|
|
userInfo: cachedUserInfo,
|
|
avatarUrl: cachedUserInfo.user?.avatar ? baseUrl + cachedUserInfo.user.avatar : ''
|
|
})
|
|
}
|
|
}
|
|
})
|
|
},
|
|
|
|
// 更新用户信息到服务器
|
|
updateUserInfo() {
|
|
const formData = this.data.formData
|
|
// 过滤掉空值
|
|
const dataToUpdate = {}
|
|
if (formData.avatar) dataToUpdate.avatar = formData.avatar
|
|
if (formData.nickName) dataToUpdate.nickName = formData.nickName
|
|
|
|
if (Object.keys(dataToUpdate).length === 0) return
|
|
|
|
http.revise({
|
|
data: dataToUpdate,
|
|
success: (res) => {
|
|
// 清空表单数据
|
|
this.setData({
|
|
'formData.avatar': null,
|
|
'formData.nickName': null
|
|
})
|
|
// 立即刷新用户信息
|
|
this.getUserInfo()
|
|
},
|
|
fail: (err) => {
|
|
console.error('更新失败:', err)
|
|
this.showToast('更新失败,请重试')
|
|
}
|
|
})
|
|
},
|
|
|
|
// 选择头像
|
|
onChooseAvatar(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) => {
|
|
wx.hideLoading()
|
|
|
|
try {
|
|
const result = JSON.parse(uploadRes.data)
|
|
if (result && result.fileName) {
|
|
// 立即更新本地显示
|
|
this.setData({
|
|
avatarUrl: avatarUrl,
|
|
'formData.avatar': result.fileName,
|
|
'userInfo.user.avatar': result.fileName
|
|
})
|
|
|
|
// 更新到服务器
|
|
this.updateUserInfo()
|
|
|
|
this.showToast('头像更新成功')
|
|
} else {
|
|
throw new Error('上传失败')
|
|
}
|
|
} catch (error) {
|
|
console.error('解析上传结果失败:', error)
|
|
this.showToast('上传失败,请重试')
|
|
}
|
|
},
|
|
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 || ''
|
|
})
|
|
},
|
|
|
|
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
|
|
}
|
|
|
|
// 如果昵称没有变化,直接关闭弹窗
|
|
if (newNickname === this.data.userInfo.user?.nickName) {
|
|
this.hideNicknameModal()
|
|
return
|
|
}
|
|
|
|
this.setData({ isUpdatingNickname: true })
|
|
|
|
// 立即更新本地显示
|
|
this.setData({
|
|
'userInfo.user.nickName': newNickname,
|
|
'formData.nickName': newNickname
|
|
})
|
|
|
|
// 更新到服务器
|
|
this.updateUserInfo()
|
|
|
|
this.setData({
|
|
showNicknameModal: false,
|
|
isUpdatingNickname: false
|
|
})
|
|
|
|
this.showToast('昵称修改成功')
|
|
},
|
|
|
|
// 查看问诊消息
|
|
goToConsultation() {
|
|
wx.navigateTo({
|
|
url: ''
|
|
})
|
|
},
|
|
|
|
// 查看问答消息
|
|
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
|
|
})
|
|
},
|
|
|
|
hideFeedback() {
|
|
this.setData({ showFeedbackModal: 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 })
|
|
|
|
// 提交
|
|
setTimeout(() => {
|
|
console.log('提交反馈:', content)
|
|
http.feedback({
|
|
data:{
|
|
content:content
|
|
},
|
|
success:res=>{
|
|
if(res.code==200){
|
|
this.showToast('感谢您的反馈!')
|
|
}else{
|
|
this.showToast('反馈失败!')
|
|
}
|
|
this.setData({
|
|
isSubmitting: false,
|
|
showFeedbackModal: false
|
|
})
|
|
}
|
|
})
|
|
|
|
|
|
}, 1500)
|
|
},
|
|
|
|
// 退出登录相关
|
|
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()
|
|
|
|
setTimeout(() => {
|
|
wx.stopPullDownRefresh()
|
|
this.showToast('刷新成功')
|
|
}, 1000)
|
|
},
|
|
|
|
// 分享
|
|
onShareAppMessage() {
|
|
return {
|
|
title: '健康守护 - 您的个人健康中心',
|
|
path: '/pages/personal-center/index'
|
|
}
|
|
}
|
|
})
|