import envConfig from "../../env"; // 定义用户信息数据类型 interface UserInfo { uid: string; telephone: string; password: string; avatar_url: string; gender: 0 | 1 | 2; // 0:女, 1:男 birthdate_date: string; createtime: string; updatetime: string; bio: string; username: string; total_points: number; } Page({ data: { userInfo: {} as UserInfo }, onLoad() { // 从缓存或接口获取用户信息 console.log("tools.formatGender 是函数吗?", typeof this.data.tools?.formatGender); this.loadUserInfo(); }, // 加载用户信息 loadUserInfo() { 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: userInfoObj }); }, // 编辑昵称 editNickname() { const { username } = this.data.userInfo; wx.showModal({ title: '修改昵称', content: '', editable: true, placeholderText: username, success: (res) => { if (res.confirm && res.content) { wx.showToast({ title: '昵称已更新', icon: 'success', duration: 1500 }); } } }); }, // 编辑性别 editGender() { const { gender } = this.data.userInfo; wx.showActionSheet({ itemList: ['男', '女', '不设置'], // 建议将 '不设置' 放在最后,更符合习惯 itemColor: '#333', success: (res) => { // 如果用户点击了取消按钮,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 }); } }); // 调用后端 API 将 newGender 保存到数据库 // ... callApiToUpdate({ gender: newGender }) ... } } }); }, // 编辑生日 editBirthdate() { const { birthdate_date } = this.data.userInfo; wx.showDatePickerModal({ startDate: '1900-01-01', endDate: '2023-12-31', currentDate: birthdate_date, success: (res) => { if (res.confirm) { this.setData({ 'userInfo.birthdate_date': res.date }); wx.showToast({ title: '生日已更新', icon: 'success', duration: 1500 }); } } }); }, // 编辑手机号 editPhone() { wx.navigateTo({ url: '/pages/editPhone/editPhone' }); }, // 修改密码 changePassword() { wx.navigateTo({ url: '/pages/changePassword/changePassword' }); }, // 更换头像 changeAvatar() { wx.chooseMedia({ count: 1, mediaType: ['image'], sizeType: ['compressed'], sourceType: ['album', 'camera'], success: (res) => { const tempFilePath = res.tempFiles[0].tempFilePath; // 模拟上传头像 this.setData({ 'userInfo.avatar_url': tempFilePath }); wx.showToast({ title: '头像已更新', icon: 'success', duration: 1500 }); } }); }, // 隐私设置 managePrivacy() { wx.navigateTo({ url: '/pages/privacySettings/privacySettings' }); }, // 关于我们 showAbout() { wx.navigateTo({ url: '/pages/about/about' }); }, // 退出登录 logout() { wx.showModal({ title: '确认退出', content: '确定要退出当前账号吗?', confirmText: '退出', confirmColor: '#ff4d4f', cancelText: '取消', success: (res) => { if (res.confirm) { // 清除登录状态 wx.removeStorageSync('token'); wx.removeStorageSync('userInfo'); // 跳转到登录页 wx.reLaunch({ url: '/pages/login/login' }); } } }); } });