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.
450 lines
11 KiB
450 lines
11 KiB
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()
|
|
// 初始化时从缓存加载用户信息
|
|
this.loadUserInfoFromCache()
|
|
},
|
|
|
|
onShow() {
|
|
console.log('个人中心页面显示,重新获取用户信息')
|
|
// 每次显示页面都从服务器获取最新数据
|
|
this.getUserInfo()
|
|
// 刷新消息数
|
|
this.gettoday()
|
|
},
|
|
|
|
// 从缓存加载用户信息(用于快速显示)
|
|
loadUserInfoFromCache() {
|
|
const cachedUserInfo = wx.getStorageSync('userInfo')
|
|
if (cachedUserInfo) {
|
|
const avatarFullUrl = this.buildAvatarUrl(cachedUserInfo.user?.avatar)
|
|
this.setData({
|
|
userInfo: cachedUserInfo,
|
|
avatarUrl: cachedUserInfo.user?.avatar || '',
|
|
avatarFullUrl: avatarFullUrl,
|
|
displayNickName: cachedUserInfo.user?.nickName || '微信用户'
|
|
})
|
|
}
|
|
},
|
|
|
|
// 构建完整的头像URL
|
|
buildAvatarUrl(avatarPath) {
|
|
if (!avatarPath) {
|
|
return '/pages/images/tx.png'
|
|
}
|
|
// 如果已经是完整URL,直接返回
|
|
if (avatarPath.startsWith('http')) {
|
|
return avatarPath
|
|
}
|
|
// 如果是相对路径,拼接baseUrl
|
|
// 确保路径格式正确
|
|
let cleanPath = avatarPath
|
|
if (cleanPath.startsWith('/')) {
|
|
cleanPath = cleanPath.substring(1)
|
|
}
|
|
return baseUrl + '/' + cleanPath
|
|
},
|
|
|
|
// 获取用户信息
|
|
getUserInfo() {
|
|
http.UserInfo({
|
|
data: {},
|
|
success: (res) => {
|
|
console.log('获取用户信息成功', res)
|
|
if (res.data && res.data.user) {
|
|
// 构建完整的头像URL
|
|
const avatarFullUrl = this.buildAvatarUrl(res.data.user?.avatar)
|
|
|
|
// 更新数据
|
|
this.setData({
|
|
userInfo: res.data,
|
|
avatarUrl: res.data.user?.avatar || '',
|
|
avatarFullUrl: avatarFullUrl,
|
|
displayNickName: res.data.user?.nickName || '微信用户'
|
|
})
|
|
|
|
// 更新缓存
|
|
wx.setStorageSync('userInfo', res.data)
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
console.error('获取用户信息失败:', err)
|
|
// 使用缓存的用户信息
|
|
this.loadUserInfoFromCache()
|
|
}
|
|
})
|
|
},
|
|
|
|
// 问诊消息 - 获取今日消息数
|
|
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) {
|
|
// 防止重复上传
|
|
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) => {
|
|
try {
|
|
const result = JSON.parse(uploadRes.data)
|
|
console.log('上传结果', result)
|
|
|
|
if (result && result.fileName) {
|
|
// 获取上传后的文件路径
|
|
const uploadedFilePath = result.fileName
|
|
// 构建完整的头像URL
|
|
const fullAvatarUrl = this.buildAvatarUrl(uploadedFilePath)
|
|
|
|
console.log('上传成功,新头像路径:', uploadedFilePath)
|
|
console.log('完整头像URL:', fullAvatarUrl)
|
|
|
|
// 1. 立即更新本地显示
|
|
this.setData({
|
|
avatarUrl: uploadedFilePath,
|
|
avatarFullUrl: fullAvatarUrl,
|
|
'userInfo.user.avatar': uploadedFilePath
|
|
})
|
|
|
|
// 2. 更新缓存中的用户信息
|
|
const cachedUserInfo = wx.getStorageSync('userInfo') || {}
|
|
if (!cachedUserInfo.user) {
|
|
cachedUserInfo.user = {}
|
|
}
|
|
cachedUserInfo.user.avatar = uploadedFilePath
|
|
wx.setStorageSync('userInfo', cachedUserInfo)
|
|
|
|
|
|
console.log(3435353,uploadedFilePath);
|
|
// 3. 单独调用更新头像的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('上传失败:返回数据格式错误')
|
|
}
|
|
} catch (error) {
|
|
console.error('解析上传结果失败:', error)
|
|
wx.hideLoading()
|
|
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 || 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)
|
|
},
|
|
|
|
})
|