|
|
@ -4,13 +4,13 @@ const baseUrl = require('../../utils/baseUrl') |
|
|
Page({ |
|
|
Page({ |
|
|
data: { |
|
|
data: { |
|
|
// 用户信息
|
|
|
// 用户信息
|
|
|
avatarUrl: '', // 原始头像路径(相对路径)
|
|
|
|
|
|
avatarFullUrl: '', // 完整的头像URL(用于显示)
|
|
|
|
|
|
|
|
|
avatarUrl: '', // 原始头像路径(相对路径)
|
|
|
|
|
|
avatarFullUrl: '', // 完整的头像URL(用于显示)
|
|
|
userInfo: { |
|
|
userInfo: { |
|
|
user: {} |
|
|
user: {} |
|
|
}, |
|
|
}, |
|
|
baseUrl: baseUrl, |
|
|
baseUrl: baseUrl, |
|
|
displayNickName: '', // 显示用的昵称
|
|
|
|
|
|
|
|
|
displayNickName: '', // 显示用的昵称
|
|
|
|
|
|
|
|
|
// 弹窗状态
|
|
|
// 弹窗状态
|
|
|
showFeedbackModal: false, |
|
|
showFeedbackModal: false, |
|
|
@ -45,8 +45,7 @@ Page({ |
|
|
|
|
|
|
|
|
onLoad() { |
|
|
onLoad() { |
|
|
this.gettoday() |
|
|
this.gettoday() |
|
|
// 初始化时从缓存加载用户信息
|
|
|
|
|
|
this.loadUserInfoFromCache() |
|
|
|
|
|
|
|
|
|
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
onShow() { |
|
|
onShow() { |
|
|
@ -57,37 +56,6 @@ Page({ |
|
|
this.gettoday() |
|
|
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() { |
|
|
getUserInfo() { |
|
|
@ -95,26 +63,19 @@ Page({ |
|
|
data: {}, |
|
|
data: {}, |
|
|
success: (res) => { |
|
|
success: (res) => { |
|
|
console.log('获取用户信息成功', 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 || '微信用户' |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
// 更新数据
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
userInfo: res.data, |
|
|
|
|
|
displayNickName: res.data.user.nickName, |
|
|
|
|
|
avatarUrl: baseUrl + res.data.user.avatar |
|
|
|
|
|
}) |
|
|
|
|
|
// 更新缓存
|
|
|
|
|
|
wx.setStorageSync('userInfo', res.data) |
|
|
|
|
|
|
|
|
// 更新缓存
|
|
|
|
|
|
wx.setStorageSync('userInfo', res.data) |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
}, |
|
|
fail: (err) => { |
|
|
fail: (err) => { |
|
|
console.error('获取用户信息失败:', err) |
|
|
console.error('获取用户信息失败:', err) |
|
|
// 使用缓存的用户信息
|
|
|
|
|
|
this.loadUserInfoFromCache() |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
}, |
|
|
}, |
|
|
@ -139,19 +100,24 @@ Page({ |
|
|
|
|
|
|
|
|
// 选择头像
|
|
|
// 选择头像
|
|
|
onChooseAvatar(e) { |
|
|
onChooseAvatar(e) { |
|
|
|
|
|
console.log(7788, e); |
|
|
// 防止重复上传
|
|
|
// 防止重复上传
|
|
|
if (this.data.isUploadingAvatar) { |
|
|
if (this.data.isUploadingAvatar) { |
|
|
this.showToast('正在上传中...') |
|
|
this.showToast('正在上传中...') |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const { avatarUrl } = e.detail |
|
|
|
|
|
|
|
|
const { |
|
|
|
|
|
avatarUrl |
|
|
|
|
|
} = e.detail |
|
|
if (!avatarUrl) { |
|
|
if (!avatarUrl) { |
|
|
this.showToast('选择头像失败') |
|
|
this.showToast('选择头像失败') |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this.setData({ isUploadingAvatar: true }) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
isUploadingAvatar: true |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
wx.showLoading({ |
|
|
wx.showLoading({ |
|
|
title: '上传中...', |
|
|
title: '上传中...', |
|
|
@ -167,44 +133,33 @@ Page({ |
|
|
filePath: avatarUrl, |
|
|
filePath: avatarUrl, |
|
|
name: 'file', |
|
|
name: 'file', |
|
|
success: (uploadRes) => { |
|
|
success: (uploadRes) => { |
|
|
try { |
|
|
|
|
|
|
|
|
|
|
|
const result = JSON.parse(uploadRes.data) |
|
|
const result = JSON.parse(uploadRes.data) |
|
|
console.log('上传结果', result) |
|
|
console.log('上传结果', result) |
|
|
|
|
|
|
|
|
if (result && result.fileName) { |
|
|
if (result && result.fileName) { |
|
|
// 获取上传后的文件路径
|
|
|
// 获取上传后的文件路径
|
|
|
const uploadedFilePath = result.fileName |
|
|
const uploadedFilePath = result.fileName |
|
|
// 构建完整的头像URL
|
|
|
|
|
|
const fullAvatarUrl = this.buildAvatarUrl(uploadedFilePath) |
|
|
|
|
|
|
|
|
|
|
|
console.log('上传成功,新头像路径:', uploadedFilePath) |
|
|
|
|
|
console.log('完整头像URL:', fullAvatarUrl) |
|
|
|
|
|
|
|
|
|
|
|
// 1. 立即更新本地显示
|
|
|
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
avatarUrl: uploadedFilePath, |
|
|
avatarUrl: uploadedFilePath, |
|
|
avatarFullUrl: fullAvatarUrl, |
|
|
|
|
|
'userInfo.user.avatar': uploadedFilePath |
|
|
|
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
// 2. 更新缓存中的用户信息
|
|
|
|
|
|
|
|
|
// 更新缓存中的用户信息
|
|
|
const cachedUserInfo = wx.getStorageSync('userInfo') || {} |
|
|
const cachedUserInfo = wx.getStorageSync('userInfo') || {} |
|
|
if (!cachedUserInfo.user) { |
|
|
if (!cachedUserInfo.user) { |
|
|
cachedUserInfo.user = {} |
|
|
cachedUserInfo.user = {} |
|
|
} |
|
|
} |
|
|
cachedUserInfo.user.avatar = uploadedFilePath |
|
|
cachedUserInfo.user.avatar = uploadedFilePath |
|
|
wx.setStorageSync('userInfo', cachedUserInfo) |
|
|
wx.setStorageSync('userInfo', cachedUserInfo) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(3435353,uploadedFilePath); |
|
|
|
|
|
// 3. 单独调用更新头像的API
|
|
|
|
|
|
|
|
|
// 更新头像的API
|
|
|
http.revise({ |
|
|
http.revise({ |
|
|
data: { avatar: uploadedFilePath }, |
|
|
|
|
|
|
|
|
data: { |
|
|
|
|
|
avatar: uploadedFilePath |
|
|
|
|
|
}, |
|
|
success: (res) => { |
|
|
success: (res) => { |
|
|
console.log('头像更新成功') |
|
|
console.log('头像更新成功') |
|
|
wx.hideLoading() |
|
|
wx.hideLoading() |
|
|
this.showToast('头像更新成功') |
|
|
this.showToast('头像更新成功') |
|
|
|
|
|
|
|
|
// 4. 重新获取用户信息以确保数据同步
|
|
|
// 4. 重新获取用户信息以确保数据同步
|
|
|
setTimeout(() => { |
|
|
setTimeout(() => { |
|
|
this.getUserInfo() |
|
|
this.getUserInfo() |
|
|
@ -216,15 +171,9 @@ Page({ |
|
|
this.showToast('头像保存失败,请重试') |
|
|
this.showToast('头像保存失败,请重试') |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
} else { |
|
|
} else { |
|
|
throw new Error('上传失败:返回数据格式错误') |
|
|
throw new Error('上传失败:返回数据格式错误') |
|
|
} |
|
|
} |
|
|
} catch (error) { |
|
|
|
|
|
console.error('解析上传结果失败:', error) |
|
|
|
|
|
wx.hideLoading() |
|
|
|
|
|
this.showToast('上传失败,请重试') |
|
|
|
|
|
} |
|
|
|
|
|
}, |
|
|
}, |
|
|
fail: (err) => { |
|
|
fail: (err) => { |
|
|
wx.hideLoading() |
|
|
wx.hideLoading() |
|
|
@ -232,7 +181,9 @@ Page({ |
|
|
this.showToast('上传失败,请检查网络') |
|
|
this.showToast('上传失败,请检查网络') |
|
|
}, |
|
|
}, |
|
|
complete: () => { |
|
|
complete: () => { |
|
|
this.setData({ isUploadingAvatar: false }) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
isUploadingAvatar: false |
|
|
|
|
|
}) |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
}, |
|
|
}, |
|
|
@ -246,11 +197,15 @@ Page({ |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
hideNicknameModal() { |
|
|
hideNicknameModal() { |
|
|
this.setData({ showNicknameModal: false }) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
showNicknameModal: false |
|
|
|
|
|
}) |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
onNicknameInput(e) { |
|
|
onNicknameInput(e) { |
|
|
this.setData({ newNickname: e.detail.value }) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
newNickname: e.detail.value |
|
|
|
|
|
}) |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
saveNickname() { |
|
|
saveNickname() { |
|
|
@ -273,7 +228,9 @@ Page({ |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this.setData({ isUpdatingNickname: true }) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
isUpdatingNickname: true |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
// 立即更新本地显示
|
|
|
// 立即更新本地显示
|
|
|
this.setData({ |
|
|
this.setData({ |
|
|
@ -292,7 +249,9 @@ Page({ |
|
|
|
|
|
|
|
|
// 更新到服务器
|
|
|
// 更新到服务器
|
|
|
http.revise({ |
|
|
http.revise({ |
|
|
data: { nickName: newNickname }, |
|
|
|
|
|
|
|
|
data: { |
|
|
|
|
|
nickName: newNickname |
|
|
|
|
|
}, |
|
|
success: (res) => { |
|
|
success: (res) => { |
|
|
console.log('昵称更新成功') |
|
|
console.log('昵称更新成功') |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
@ -309,7 +268,9 @@ Page({ |
|
|
}, |
|
|
}, |
|
|
fail: (err) => { |
|
|
fail: (err) => { |
|
|
console.error('昵称更新失败:', err) |
|
|
console.error('昵称更新失败:', err) |
|
|
this.setData({ isUpdatingNickname: false }) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
isUpdatingNickname: false |
|
|
|
|
|
}) |
|
|
this.showToast('修改失败,请重试') |
|
|
this.showToast('修改失败,请重试') |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
@ -378,7 +339,9 @@ Page({ |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
this.setData({ isSubmitting: true }) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
isSubmitting: true |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
http.feedback({ |
|
|
http.feedback({ |
|
|
data: { |
|
|
data: { |
|
|
@ -399,18 +362,24 @@ Page({ |
|
|
fail: (err) => { |
|
|
fail: (err) => { |
|
|
console.error('反馈失败:', err) |
|
|
console.error('反馈失败:', err) |
|
|
this.showToast('反馈失败,请重试') |
|
|
this.showToast('反馈失败,请重试') |
|
|
this.setData({ isSubmitting: false }) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
isSubmitting: false |
|
|
|
|
|
}) |
|
|
} |
|
|
} |
|
|
}) |
|
|
}) |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
// 退出登录相关
|
|
|
// 退出登录相关
|
|
|
showLogoutConfirm() { |
|
|
showLogoutConfirm() { |
|
|
this.setData({ showLogoutModal: true }) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
showLogoutModal: true |
|
|
|
|
|
}) |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
hideLogoutModal() { |
|
|
hideLogoutModal() { |
|
|
this.setData({ showLogoutModal: false }) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
showLogoutModal: false |
|
|
|
|
|
}) |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
doLogout() { |
|
|
doLogout() { |
|
|
@ -432,7 +401,9 @@ Page({ |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
setTimeout(() => { |
|
|
setTimeout(() => { |
|
|
this.setData({ showToast: false }) |
|
|
|
|
|
|
|
|
this.setData({ |
|
|
|
|
|
showToast: false |
|
|
|
|
|
}) |
|
|
}, 2000) |
|
|
}, 2000) |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|