2025/9/29/2:10

This commit is contained in:
2025-09-29 02:10:32 +08:00
parent c4da898efe
commit 178caaf28b
7 changed files with 160 additions and 50 deletions

View File

@@ -1,10 +1,12 @@
import envConfig from "../../env";
// 定义用户信息数据类型
interface UserInfo {
uid: string;
telephone: string;
password: string;
avatar_url: string;
gender: 0 | 1; // 0:女, 1:男
gender: 0 | 1 | 2; // 0:女, 1:男
birthdate_date: string;
createtime: string;
updatetime: string;
@@ -20,28 +22,30 @@ Page({
onLoad() {
// 从缓存或接口获取用户信息
console.log("tools.formatGender 是函数吗?", typeof this.data.tools?.formatGender);
this.loadUserInfo();
},
// 加载用户信息
loadUserInfo() {
// 模拟数据,实际项目中应从接口获取
const mockUserInfo: UserInfo = {
uid: '10086',
telephone: '138****8888',
password: '',
avatar_url: 'https://picsum.photos/200/200',
gender: 1,
birthdate_date: '1995-08-15',
createtime: '2023-01-01',
updatetime: '2023-06-10',
bio: '热爱生活,喜欢探索',
username: '张小明',
total_points: 1250
};
const app = getApp();
console.log("全局userinfo",app.globalData.userInfo)
const userInfo = app.globalData.userInfo || {};
console.log("userInfo 的数据类型:", typeof userInfo);
console.log("user中的userInfo",userInfo)
let userInfoObj = userInfo;
if (typeof userInfo === "string") {
try {
userInfoObj = JSON.parse(userInfo); // 字符串转对象
} catch (e) {
console.error("解析 userInfo 字符串失败:", e);
return;
}
}
console.log("应保存的username",userInfoObj.username)
console.log("保存的性别:",userInfoObj.gender)
this.setData({
userInfo: mockUserInfo
userInfo: userInfoObj
});
},
@@ -51,15 +55,11 @@ Page({
wx.showModal({
title: '修改昵称',
content: '请输入新的昵称',
content: '',
editable: true,
placeholderText: username,
success: (res) => {
if (res.confirm && res.content) {
// 调用接口修改昵称
this.setData({
'userInfo.username': res.content
});
wx.showToast({
title: '昵称已更新',
@@ -76,21 +76,79 @@ Page({
const { gender } = this.data.userInfo;
wx.showActionSheet({
itemList: ['男', '女'],
itemList: ['男', '女', '不设置'], // 建议将 '不设置' 放在最后,更符合习惯
itemColor: '#333',
success: (res) => {
const newGender = res.tapIndex === 0 ? 1 : 0;
// 如果用户点击了取消按钮res.tapIndex 会是 undefined
if (typeof res.tapIndex === 'undefined') {
return;
}
let newGender: 0 | 1 | 2;
// ⭐️ 修正后的正确逻辑
switch (res.tapIndex) {
case 0: // 用户点击了 '男'
newGender = 1;
break;
case 1: // 用户点击了 '女'
newGender = 2;
break;
case 2: // 用户点击了 '不设置'
newGender = 0;
break;
default:
return; // 其他情况直接返回
}
console.log("修改的newgender",newGender)
if (newGender !== gender) {
// 更新页面数据
this.setData({
'userInfo.gender': newGender
});
wx.request({
url: `${envConfig.apiBaseUrl}/user/change`, // 替换为你的后端接口地址
method: 'POST',
data: {
"uid": this.data.userInfo.uid,
"gender":newGender
},
success: (res) => {
if(res.data.success){
wx.showToast({
title: '性别已更新',
icon: 'success',
duration: 1500
});
}else{
wx.showToast({
title: '更新失败',
icon: 'error',
duration: 1500
});
}
},
fail: (err) => {
wx.showToast({
title: '更新失败',
icon: 'error',
duration: 1500
});
}
});
wx.showToast({
title: '性别已更新',
icon: 'success',
duration: 1500
});
// 调用后端 API 将 newGender 保存到数据库
// ... callApiToUpdate({ gender: newGender }) ...
}
}
});