|
|
@ -130,29 +130,51 @@ Page({ |
|
|
const userInfo = { ...app.globalData.userInfo } |
|
|
const userInfo = { ...app.globalData.userInfo } |
|
|
// 使用固定的头像URL数组
|
|
|
// 使用固定的头像URL数组
|
|
|
const avatarUrls = this.data.avatarUrls; |
|
|
const avatarUrls = this.data.avatarUrls; |
|
|
// 使用第一张头像作为默认显示
|
|
|
// 保持用户当前的头像URL,如果没有则使用第一张作为默认
|
|
|
|
|
|
if (!userInfo.avatarUrl) { |
|
|
userInfo.avatarUrl = avatarUrls[0]; |
|
|
userInfo.avatarUrl = avatarUrls[0]; |
|
|
|
|
|
} |
|
|
userInfo.hiddenPhoneNumber = this.hidePhoneNumber(userInfo.phoneNumber) |
|
|
userInfo.hiddenPhoneNumber = this.hidePhoneNumber(userInfo.phoneNumber) |
|
|
|
|
|
// 根据当前的avatarUrl更新currentAvatarIndex
|
|
|
|
|
|
let currentAvatarIndex = 0; |
|
|
|
|
|
if (userInfo.avatarUrl) { |
|
|
|
|
|
const index = avatarUrls.indexOf(userInfo.avatarUrl); |
|
|
|
|
|
if (index !== -1) { |
|
|
|
|
|
currentAvatarIndex = index; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
userInfo, |
|
|
userInfo, |
|
|
rawAvatarUrl: avatarUrls |
|
|
rawAvatarUrl: avatarUrls, |
|
|
|
|
|
currentAvatarIndex: currentAvatarIndex |
|
|
}) |
|
|
}) |
|
|
// 更新全局状态中的头像URL,确保下次加载时使用固定的头像
|
|
|
// 更新全局状态中的头像URL,确保下次加载时使用处理后的URL
|
|
|
app.globalData.userInfo = userInfo; |
|
|
app.globalData.userInfo = userInfo; |
|
|
wx.setStorageSync('userInfo', userInfo); |
|
|
wx.setStorageSync('userInfo', userInfo); |
|
|
} else { |
|
|
} else { |
|
|
const userInfo = { ...localUserInfo } |
|
|
const userInfo = { ...localUserInfo } |
|
|
// 使用固定的头像URL数组
|
|
|
// 使用固定的头像URL数组
|
|
|
const avatarUrls = this.data.avatarUrls; |
|
|
const avatarUrls = this.data.avatarUrls; |
|
|
// 使用第一张头像作为默认显示
|
|
|
// 保持用户当前的头像URL,如果没有则使用第一张作为默认
|
|
|
|
|
|
if (!userInfo.avatarUrl) { |
|
|
userInfo.avatarUrl = avatarUrls[0]; |
|
|
userInfo.avatarUrl = avatarUrls[0]; |
|
|
|
|
|
} |
|
|
userInfo.hiddenPhoneNumber = this.hidePhoneNumber(userInfo.phoneNumber) |
|
|
userInfo.hiddenPhoneNumber = this.hidePhoneNumber(userInfo.phoneNumber) |
|
|
|
|
|
// 根据当前的avatarUrl更新currentAvatarIndex
|
|
|
|
|
|
let currentAvatarIndex = 0; |
|
|
|
|
|
if (userInfo.avatarUrl) { |
|
|
|
|
|
const index = avatarUrls.indexOf(userInfo.avatarUrl); |
|
|
|
|
|
if (index !== -1) { |
|
|
|
|
|
currentAvatarIndex = index; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
app.globalData.userInfo = userInfo |
|
|
app.globalData.userInfo = userInfo |
|
|
wx.setStorageSync('userInfo', userInfo); |
|
|
wx.setStorageSync('userInfo', userInfo); |
|
|
this.setData({ |
|
|
this.setData({ |
|
|
userInfo, |
|
|
userInfo, |
|
|
needPhoneAuth: !userInfo.phoneNumber, |
|
|
needPhoneAuth: !userInfo.phoneNumber, |
|
|
rawAvatarUrl: avatarUrls |
|
|
rawAvatarUrl: avatarUrls, |
|
|
|
|
|
currentAvatarIndex: currentAvatarIndex |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -274,6 +296,10 @@ Page({ |
|
|
|
|
|
|
|
|
// 更新本地用户信息
|
|
|
// 更新本地用户信息
|
|
|
const app = getApp() |
|
|
const app = getApp() |
|
|
|
|
|
// 先保存当前的头像URL
|
|
|
|
|
|
const currentAvatarUrl = app.globalData.userInfo.avatarUrl; |
|
|
|
|
|
|
|
|
|
|
|
// 构建更新后的用户信息,优先使用服务器返回的值
|
|
|
const updatedUserInfo = { |
|
|
const updatedUserInfo = { |
|
|
...app.globalData.userInfo, |
|
|
...app.globalData.userInfo, |
|
|
...serverUserInfo |
|
|
...serverUserInfo |
|
|
@ -281,8 +307,21 @@ Page({ |
|
|
|
|
|
|
|
|
// 使用固定的头像URL数组
|
|
|
// 使用固定的头像URL数组
|
|
|
const avatarUrls = this.data.avatarUrls; |
|
|
const avatarUrls = this.data.avatarUrls; |
|
|
// 使用第一张头像作为默认显示
|
|
|
|
|
|
updatedUserInfo.avatarUrl = avatarUrls[0]; |
|
|
// 检查服务器返回的用户信息中是否包含avatarUrl
|
|
|
|
|
|
let finalAvatarUrl = updatedUserInfo.avatarUrl; |
|
|
|
|
|
|
|
|
|
|
|
// 如果服务器返回的avatarUrl为空字符串或null,且本地有,则使用本地的
|
|
|
|
|
|
if ((!finalAvatarUrl || finalAvatarUrl === '') && currentAvatarUrl) { |
|
|
|
|
|
finalAvatarUrl = currentAvatarUrl; |
|
|
|
|
|
} |
|
|
|
|
|
// 如果两者都没有,则使用第一张作为默认
|
|
|
|
|
|
else if (!finalAvatarUrl || finalAvatarUrl === '') { |
|
|
|
|
|
finalAvatarUrl = avatarUrls[0]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 更新头像URL
|
|
|
|
|
|
updatedUserInfo.avatarUrl = finalAvatarUrl; |
|
|
|
|
|
|
|
|
// 添加隐藏的电话号码
|
|
|
// 添加隐藏的电话号码
|
|
|
updatedUserInfo.hiddenPhoneNumber = this.hidePhoneNumber(updatedUserInfo.phoneNumber) |
|
|
updatedUserInfo.hiddenPhoneNumber = this.hidePhoneNumber(updatedUserInfo.phoneNumber) |
|
|
@ -302,7 +341,7 @@ Page({ |
|
|
// 重新检查用户手机号是否在personnel表中
|
|
|
// 重新检查用户手机号是否在personnel表中
|
|
|
this.checkPhoneInPersonnel() |
|
|
this.checkPhoneInPersonnel() |
|
|
|
|
|
|
|
|
console.log('用户信息已更新,昵称:', updatedUserInfo.name, '手机号:', updatedUserInfo.phoneNumber, '身份:', serverUserInfo.type) |
|
|
console.log('用户信息已更新,昵称:', updatedUserInfo.name, '手机号:', updatedUserInfo.phoneNumber, '身份:', serverUserInfo.type, '头像URL:', finalAvatarUrl) |
|
|
} |
|
|
} |
|
|
}).catch(err => { |
|
|
}).catch(err => { |
|
|
console.error('从服务器获取用户信息失败:', err) |
|
|
console.error('从服务器获取用户信息失败:', err) |
|
|
@ -315,6 +354,9 @@ Page({ |
|
|
|
|
|
|
|
|
// 更新本地用户信息
|
|
|
// 更新本地用户信息
|
|
|
const app = getApp() |
|
|
const app = getApp() |
|
|
|
|
|
// 先保存当前的头像URL
|
|
|
|
|
|
const currentAvatarUrl = app.globalData.userInfo.avatarUrl; |
|
|
|
|
|
|
|
|
const updatedUserInfo = { |
|
|
const updatedUserInfo = { |
|
|
...app.globalData.userInfo, |
|
|
...app.globalData.userInfo, |
|
|
...serverUserInfo |
|
|
...serverUserInfo |
|
|
@ -322,8 +364,21 @@ Page({ |
|
|
|
|
|
|
|
|
// 使用固定的头像URL数组
|
|
|
// 使用固定的头像URL数组
|
|
|
const avatarUrls = this.data.avatarUrls; |
|
|
const avatarUrls = this.data.avatarUrls; |
|
|
// 使用第一张头像作为默认显示
|
|
|
|
|
|
updatedUserInfo.avatarUrl = avatarUrls[0]; |
|
|
// 检查服务器返回的用户信息中是否包含avatarUrl
|
|
|
|
|
|
let finalAvatarUrl = updatedUserInfo.avatarUrl; |
|
|
|
|
|
|
|
|
|
|
|
// 如果服务器返回的avatarUrl为空字符串或null,且本地有,则使用本地的
|
|
|
|
|
|
if ((!finalAvatarUrl || finalAvatarUrl === '') && currentAvatarUrl) { |
|
|
|
|
|
finalAvatarUrl = currentAvatarUrl; |
|
|
|
|
|
} |
|
|
|
|
|
// 如果两者都没有,则使用第一张作为默认
|
|
|
|
|
|
else if (!finalAvatarUrl || finalAvatarUrl === '') { |
|
|
|
|
|
finalAvatarUrl = avatarUrls[0]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 更新头像URL
|
|
|
|
|
|
updatedUserInfo.avatarUrl = finalAvatarUrl; |
|
|
|
|
|
|
|
|
// 添加隐藏的电话号码
|
|
|
// 添加隐藏的电话号码
|
|
|
updatedUserInfo.hiddenPhoneNumber = this.hidePhoneNumber(updatedUserInfo.phoneNumber) |
|
|
updatedUserInfo.hiddenPhoneNumber = this.hidePhoneNumber(updatedUserInfo.phoneNumber) |
|
|
@ -343,7 +398,7 @@ Page({ |
|
|
// 重新检查用户手机号是否在personnel表中
|
|
|
// 重新检查用户手机号是否在personnel表中
|
|
|
this.checkPhoneInPersonnel() |
|
|
this.checkPhoneInPersonnel() |
|
|
|
|
|
|
|
|
console.log('用户信息已更新(备选方案):', updatedUserInfo) |
|
|
console.log('用户信息已更新(备选方案):', updatedUserInfo, '头像URL:', finalAvatarUrl) |
|
|
} |
|
|
} |
|
|
}).catch(validateErr => { |
|
|
}).catch(validateErr => { |
|
|
console.error('从服务器获取用户信息失败(包括备选方案):', validateErr) |
|
|
console.error('从服务器获取用户信息失败(包括备选方案):', validateErr) |
|
|
@ -761,12 +816,11 @@ Page({ |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 构造上传数据(包含所有必要字段,包括phoneNumber和头像URL)
|
|
|
// 构造上传数据(不包含头像URL,避免登录时覆盖用户选择的头像)
|
|
|
const uploadData = { |
|
|
const uploadData = { |
|
|
userId: userId, |
|
|
userId: userId, |
|
|
openid: openid, |
|
|
openid: openid, |
|
|
name: userInfo.name, |
|
|
name: userInfo.name, |
|
|
avatarUrl: userInfo.avatarUrl, // 添加头像URL字段
|
|
|
|
|
|
phoneNumber: userInfo.phoneNumber, // 添加phoneNumber字段,满足服务器要求
|
|
|
phoneNumber: userInfo.phoneNumber, // 添加phoneNumber字段,满足服务器要求
|
|
|
type: type, |
|
|
type: type, |
|
|
timestamp: Date.now() |
|
|
timestamp: Date.now() |
|
|
@ -1171,8 +1225,10 @@ Page({ |
|
|
|
|
|
|
|
|
// 点击头像切换图片
|
|
|
// 点击头像切换图片
|
|
|
onAvatarClick() { |
|
|
onAvatarClick() { |
|
|
|
|
|
console.log('开始切换头像'); |
|
|
// 使用固定的头像URL数组
|
|
|
// 使用固定的头像URL数组
|
|
|
const avatarUrls = this.data.avatarUrls; |
|
|
const avatarUrls = this.data.avatarUrls; |
|
|
|
|
|
console.log('头像URL数组:', avatarUrls); |
|
|
|
|
|
|
|
|
// 检查是否有至少两张图片可以切换
|
|
|
// 检查是否有至少两张图片可以切换
|
|
|
if (avatarUrls.length < 2) { |
|
|
if (avatarUrls.length < 2) { |
|
|
@ -1184,18 +1240,36 @@ Page({ |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 先根据当前的avatarUrl更新currentAvatarIndex,确保它们保持一致
|
|
|
|
|
|
let currentAvatarIndex = 0; |
|
|
|
|
|
const currentAvatarUrl = this.data.userInfo.avatarUrl; |
|
|
|
|
|
if (currentAvatarUrl) { |
|
|
|
|
|
const index = avatarUrls.indexOf(currentAvatarUrl); |
|
|
|
|
|
if (index !== -1) { |
|
|
|
|
|
currentAvatarIndex = index; |
|
|
|
|
|
// 更新currentAvatarIndex到页面数据
|
|
|
|
|
|
this.setData({ currentAvatarIndex: currentAvatarIndex }); |
|
|
|
|
|
console.log('根据当前avatarUrl更新currentAvatarIndex:', currentAvatarIndex); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 切换到下一张头像
|
|
|
// 切换到下一张头像
|
|
|
let nextIndex = this.data.currentAvatarIndex + 1; |
|
|
let nextIndex = currentAvatarIndex + 1; |
|
|
if (nextIndex >= avatarUrls.length) { |
|
|
if (nextIndex >= avatarUrls.length) { |
|
|
nextIndex = 0; // 如果已经是最后一张,回到第一张
|
|
|
nextIndex = 0; // 如果已经是最后一张,回到第一张
|
|
|
} |
|
|
} |
|
|
|
|
|
console.log('当前头像索引:', currentAvatarIndex); |
|
|
|
|
|
console.log('下一张头像索引:', nextIndex); |
|
|
|
|
|
console.log('下一张头像URL:', avatarUrls[nextIndex]); |
|
|
|
|
|
|
|
|
// 显示切换头像的提示
|
|
|
// 显示切换头像的提示
|
|
|
|
|
|
const avatarType = nextIndex === 0 ? '男' : '女'; |
|
|
wx.showModal({ |
|
|
wx.showModal({ |
|
|
title: '切换头像', |
|
|
title: '切换头像', |
|
|
content: `确定要切换到第${nextIndex + 1}张头像吗?`, |
|
|
content: `确定要切换到${avatarType}头像吗?`, |
|
|
success: (res) => { |
|
|
success: (res) => { |
|
|
if (res.confirm) { |
|
|
if (res.confirm) { |
|
|
|
|
|
console.log('用户确认切换头像'); |
|
|
// 切换到下一张图片
|
|
|
// 切换到下一张图片
|
|
|
const app = getApp(); |
|
|
const app = getApp(); |
|
|
// 只更新显示用的avatarUrl
|
|
|
// 只更新显示用的avatarUrl
|
|
|
@ -1209,6 +1283,11 @@ Page({ |
|
|
userInfo: updatedUserInfo, |
|
|
userInfo: updatedUserInfo, |
|
|
currentAvatarIndex: nextIndex |
|
|
currentAvatarIndex: nextIndex |
|
|
}); |
|
|
}); |
|
|
|
|
|
console.log('页面显示已更新'); |
|
|
|
|
|
|
|
|
|
|
|
// 将选中的头像URL写入数据库
|
|
|
|
|
|
console.log('准备更新头像到数据库:', avatarUrls[nextIndex]); |
|
|
|
|
|
this.updateAvatarInDatabase(avatarUrls[nextIndex]); |
|
|
|
|
|
|
|
|
wx.showToast({ |
|
|
wx.showToast({ |
|
|
title: '头像切换成功', |
|
|
title: '头像切换成功', |
|
|
@ -1220,4 +1299,48 @@ Page({ |
|
|
}); |
|
|
}); |
|
|
}, |
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
// 将头像URL更新到数据库
|
|
|
|
|
|
updateAvatarInDatabase(avatarUrl) { |
|
|
|
|
|
console.log('开始更新头像到数据库:', avatarUrl); |
|
|
|
|
|
const API = require('../../utils/api.js'); |
|
|
|
|
|
const openid = wx.getStorageSync('openid'); |
|
|
|
|
|
const userId = wx.getStorageSync('userId'); |
|
|
|
|
|
|
|
|
|
|
|
console.log('用户信息:', { openid, userId }); |
|
|
|
|
|
|
|
|
|
|
|
if (!openid) { |
|
|
|
|
|
console.error('更新头像失败:缺少openid'); |
|
|
|
|
|
return; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 构造更新数据
|
|
|
|
|
|
const updateData = { |
|
|
|
|
|
openid: openid, |
|
|
|
|
|
userId: userId, |
|
|
|
|
|
avatarUrl: avatarUrl |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
console.log('构造的更新数据:', updateData); |
|
|
|
|
|
|
|
|
|
|
|
// 调用API更新用户信息
|
|
|
|
|
|
console.log('准备调用API.uploadUserInfo'); |
|
|
|
|
|
API.uploadUserInfo(updateData).then(res => { |
|
|
|
|
|
console.log('更新头像到数据库成功:', res); |
|
|
|
|
|
|
|
|
|
|
|
// 更新本地存储和全局数据中的头像URL
|
|
|
|
|
|
const app = getApp(); |
|
|
|
|
|
app.globalData.userInfo.avatarUrl = avatarUrl; |
|
|
|
|
|
wx.setStorageSync('userInfo', app.globalData.userInfo); |
|
|
|
|
|
|
|
|
|
|
|
console.log('更新本地存储和全局数据中的头像URL成功:', avatarUrl); |
|
|
|
|
|
}).catch(err => { |
|
|
|
|
|
console.error('更新头像到数据库失败:', err); |
|
|
|
|
|
wx.showToast({ |
|
|
|
|
|
title: '更新头像失败,请重试', |
|
|
|
|
|
icon: 'none', |
|
|
|
|
|
duration: 2000 |
|
|
|
|
|
}); |
|
|
|
|
|
}); |
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
}) |
|
|
}) |
|
|
|