2025/9/29/2:10
This commit is contained in:
26
miniprogram/pages/setting/format.wxs
Normal file
26
miniprogram/pages/setting/format.wxs
Normal file
@@ -0,0 +1,26 @@
|
||||
// format.wxs (新版:使用 switch 语句,更清晰)
|
||||
|
||||
/**
|
||||
* 根据传入的 gender 整数,返回对应的文本
|
||||
* @param {number} genderCode - 0, 1, 或 2
|
||||
* @returns {string} - '未设置', '男', 或 '女'
|
||||
*/
|
||||
function formatGender(genderCode) {
|
||||
// 直接对传入的整数进行判断
|
||||
switch (genderCode) {
|
||||
case 0:
|
||||
return '未设置';
|
||||
case 1:
|
||||
return '男';
|
||||
case 2:
|
||||
return '女';
|
||||
default:
|
||||
// 如果传入的不是 0, 1, 2, 或者为 null/undefined,则返回默认值
|
||||
return '未设置';
|
||||
}
|
||||
}
|
||||
|
||||
// 将 formatGender 函数导出,以便 WXML 文件可以使用
|
||||
module.exports = {
|
||||
formatGender: formatGender
|
||||
};
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"component": true,
|
||||
"usingComponents": {
|
||||
|
||||
"t-icon": "tdesign-miniprogram/icon/icon"
|
||||
}
|
||||
}
|
||||
@@ -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 }) ...
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
<wxs src="./format.wxs" module="tools" />
|
||||
<view class="setting-page">
|
||||
|
||||
<!-- 用户信息卡片 -->
|
||||
@@ -24,7 +25,7 @@
|
||||
<view class="setting-card">
|
||||
<view class="setting-item" bindtap="editNickname">
|
||||
<view class="item-icon">
|
||||
<image src="/images/icon-nickname.png" mode="widthFix"></image>
|
||||
<t-icon name="article" size="40rpx" />
|
||||
</view>
|
||||
<view class="item-content">
|
||||
<text class="item-label">昵称</text>
|
||||
@@ -38,23 +39,23 @@
|
||||
<view class="item-divider"></view>
|
||||
|
||||
<view class="setting-item" bindtap="editGender">
|
||||
<view class="item-icon">
|
||||
<image src="/images/icon-gender.png" mode="widthFix"></image>
|
||||
</view>
|
||||
<view class="item-content">
|
||||
<text class="item-label">性别</text>
|
||||
<text class="item-value">{{userInfo.gender === 1 ? '男' : '女'}}</text>
|
||||
</view>
|
||||
<view class="item-arrow">
|
||||
<image src="/images/arrow-right.png" mode="widthFix"></image>
|
||||
</view>
|
||||
</view>
|
||||
<view class="item-icon">
|
||||
<t-icon name="accessibility" size="40rpx" />
|
||||
</view>
|
||||
<view class="item-content">
|
||||
<text class="item-label">性别</text>
|
||||
<text class="item-value">{{tools.formatGender(userInfo.gender)}}</text>
|
||||
</view>
|
||||
<view class="item-arrow">
|
||||
<image src="/images/arrow-right.png" mode="widthFix"></image>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="item-divider"></view>
|
||||
|
||||
<view class="setting-item" bindtap="editBirthdate">
|
||||
<view class="item-icon">
|
||||
<image src="/images/icon-birthday.png" mode="widthFix"></image>
|
||||
<t-icon name="calendar-1" size="40rpx" />
|
||||
</view>
|
||||
<view class="item-content">
|
||||
<text class="item-label">生日</text>
|
||||
@@ -69,7 +70,7 @@
|
||||
|
||||
<view class="setting-item" bindtap="editPhone">
|
||||
<view class="item-icon">
|
||||
<image src="/images/icon-phone.png" mode="widthFix"></image>
|
||||
<t-icon name="call" size="40rpx" />
|
||||
</view>
|
||||
<view class="item-content">
|
||||
<text class="item-label">手机号</text>
|
||||
@@ -89,7 +90,7 @@
|
||||
<view class="setting-card">
|
||||
<view class="setting-item" bindtap="changePassword">
|
||||
<view class="item-icon">
|
||||
<image src="/images/icon-password.png" mode="widthFix"></image>
|
||||
<t-icon name="fingerprint-1" size="40rpx" />
|
||||
</view>
|
||||
<view class="item-content">
|
||||
<text class="item-label">修改密码</text>
|
||||
@@ -103,7 +104,7 @@
|
||||
|
||||
<view class="setting-item" bindtap="managePrivacy">
|
||||
<view class="item-icon">
|
||||
<image src="/images/icon-privacy.png" mode="widthFix"></image>
|
||||
<t-icon name="hard-drive" size="40rpx" />
|
||||
</view>
|
||||
<view class="item-content">
|
||||
<text class="item-label">隐私设置</text>
|
||||
@@ -122,7 +123,7 @@
|
||||
<view class="setting-card">
|
||||
<view class="setting-item" bindtap="showAbout">
|
||||
<view class="item-icon">
|
||||
<image src="/images/icon-about.png" mode="widthFix"></image>
|
||||
<t-icon name="lighthouse" size="40rpx" />
|
||||
</view>
|
||||
<view class="item-content">
|
||||
<text class="item-label">关于我们</text>
|
||||
|
||||
@@ -19,7 +19,7 @@ Component({
|
||||
refreshUserStatus(){
|
||||
console.log("主动刷新user界面")
|
||||
const app = getApp();
|
||||
console.log("全局userinfo",app.globalData.userInfo)
|
||||
console.log("全局userinfo头像:",app.globalData.userInfo.avatar_url)
|
||||
const userInfo = app.globalData.userInfo || {};
|
||||
console.log("userInfo 的数据类型:", typeof userInfo);
|
||||
console.log("user中的userInfo:",userInfo)
|
||||
@@ -39,6 +39,7 @@ Component({
|
||||
});
|
||||
// 调试:确认页面数据是否正确
|
||||
console.log("页面username赋值后:", this.data.username);
|
||||
console.log("用户页面profile:",this.data.userinfo.avatar_url)
|
||||
},
|
||||
onRefresherRefresh() {
|
||||
console.log("触发刷新");
|
||||
@@ -67,6 +68,24 @@ Component({
|
||||
});
|
||||
|
||||
console.log("更新后用户名为:",this.data.userinfo.username)
|
||||
const app = getApp();
|
||||
try {
|
||||
app.globalData.userInfo = res.data.data;
|
||||
console.log("用户信息、Token 已成功存入微信本地缓存");
|
||||
} catch (e) {
|
||||
// 捕获缓存失败的异常(如存储空间不足)
|
||||
console.error("微信本地缓存存储失败:", e);
|
||||
Toast({
|
||||
selector: '#t-toast',
|
||||
message: '缓存用户信息失败,请稍后重试',
|
||||
theme: 'error',
|
||||
direction: 'column',
|
||||
});
|
||||
}
|
||||
|
||||
console.log("缓存app中的头像为:",wx.getStorageSync("ttk_userInfo"))
|
||||
|
||||
console.log("app.ts中性别:",app.globalData.userInfo.gender)
|
||||
|
||||
|
||||
// 假设后端返回的数据包含在 res.data.data 或 res.data.userinfo 中
|
||||
@@ -76,6 +95,7 @@ Component({
|
||||
this.setData({
|
||||
refresherTriggered: false
|
||||
}); // <-- 语法已修正
|
||||
|
||||
|
||||
// ⭐️ 建议:调用方法来处理新数据并显示 Toast
|
||||
// 假设你有一个方法来处理返回的用户数据,并同时关闭刷新动画和提示
|
||||
@@ -124,6 +144,11 @@ Component({
|
||||
// });
|
||||
|
||||
// }, 3000); // ⭐️ 延迟时间已修改为 3000 毫秒
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
},
|
||||
onRefresherPulling() {
|
||||
console.log("正在下拉...");
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
<view class="user-contain">
|
||||
<!-- 头像与用户名区域 -->
|
||||
<view class="user-avatar">
|
||||
<t-avatar class="avatar-example" image="{{image || 'https://picsum.photos/id/64/200/200'}}" size="large" />
|
||||
<t-avatar class="avatar-example" image="{{userinfo.avatar_url || 'https://img1.baidu.com/it/u=105216936,2956740654&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500'}}" size="large" />
|
||||
<a class="avatar-font" bind:tap="userlogin">
|
||||
{{hasToken ? (userinfo.username || '用户中心') : '点击登录/注册'}}
|
||||
</a>
|
||||
|
||||
Reference in New Issue
Block a user