2025/9/29/2:10
This commit is contained in:
@@ -1 +1 @@
|
|||||||
当前版本:2.1.8
|
当前版本:2.1.9
|
||||||
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,
|
"component": true,
|
||||||
"usingComponents": {
|
"usingComponents": {
|
||||||
|
"t-icon": "tdesign-miniprogram/icon/icon"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
|
import envConfig from "../../env";
|
||||||
|
|
||||||
// 定义用户信息数据类型
|
// 定义用户信息数据类型
|
||||||
interface UserInfo {
|
interface UserInfo {
|
||||||
uid: string;
|
uid: string;
|
||||||
telephone: string;
|
telephone: string;
|
||||||
password: string;
|
password: string;
|
||||||
avatar_url: string;
|
avatar_url: string;
|
||||||
gender: 0 | 1; // 0:女, 1:男
|
gender: 0 | 1 | 2; // 0:女, 1:男
|
||||||
birthdate_date: string;
|
birthdate_date: string;
|
||||||
createtime: string;
|
createtime: string;
|
||||||
updatetime: string;
|
updatetime: string;
|
||||||
@@ -20,28 +22,30 @@ Page({
|
|||||||
|
|
||||||
onLoad() {
|
onLoad() {
|
||||||
// 从缓存或接口获取用户信息
|
// 从缓存或接口获取用户信息
|
||||||
|
console.log("tools.formatGender 是函数吗?", typeof this.data.tools?.formatGender);
|
||||||
this.loadUserInfo();
|
this.loadUserInfo();
|
||||||
},
|
},
|
||||||
|
|
||||||
// 加载用户信息
|
// 加载用户信息
|
||||||
loadUserInfo() {
|
loadUserInfo() {
|
||||||
// 模拟数据,实际项目中应从接口获取
|
const app = getApp();
|
||||||
const mockUserInfo: UserInfo = {
|
console.log("全局userinfo",app.globalData.userInfo)
|
||||||
uid: '10086',
|
const userInfo = app.globalData.userInfo || {};
|
||||||
telephone: '138****8888',
|
console.log("userInfo 的数据类型:", typeof userInfo);
|
||||||
password: '',
|
console.log("user中的userInfo:",userInfo)
|
||||||
avatar_url: 'https://picsum.photos/200/200',
|
let userInfoObj = userInfo;
|
||||||
gender: 1,
|
if (typeof userInfo === "string") {
|
||||||
birthdate_date: '1995-08-15',
|
try {
|
||||||
createtime: '2023-01-01',
|
userInfoObj = JSON.parse(userInfo); // 字符串转对象
|
||||||
updatetime: '2023-06-10',
|
} catch (e) {
|
||||||
bio: '热爱生活,喜欢探索',
|
console.error("解析 userInfo 字符串失败:", e);
|
||||||
username: '张小明',
|
return;
|
||||||
total_points: 1250
|
}
|
||||||
};
|
}
|
||||||
|
console.log("应保存的username:",userInfoObj.username)
|
||||||
|
console.log("保存的性别:",userInfoObj.gender)
|
||||||
this.setData({
|
this.setData({
|
||||||
userInfo: mockUserInfo
|
userInfo: userInfoObj
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -51,15 +55,11 @@ Page({
|
|||||||
|
|
||||||
wx.showModal({
|
wx.showModal({
|
||||||
title: '修改昵称',
|
title: '修改昵称',
|
||||||
content: '请输入新的昵称',
|
content: '',
|
||||||
editable: true,
|
editable: true,
|
||||||
placeholderText: username,
|
placeholderText: username,
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
if (res.confirm && res.content) {
|
if (res.confirm && res.content) {
|
||||||
// 调用接口修改昵称
|
|
||||||
this.setData({
|
|
||||||
'userInfo.username': res.content
|
|
||||||
});
|
|
||||||
|
|
||||||
wx.showToast({
|
wx.showToast({
|
||||||
title: '昵称已更新',
|
title: '昵称已更新',
|
||||||
@@ -76,21 +76,79 @@ Page({
|
|||||||
const { gender } = this.data.userInfo;
|
const { gender } = this.data.userInfo;
|
||||||
|
|
||||||
wx.showActionSheet({
|
wx.showActionSheet({
|
||||||
itemList: ['男', '女'],
|
itemList: ['男', '女', '不设置'], // 建议将 '不设置' 放在最后,更符合习惯
|
||||||
itemColor: '#333',
|
itemColor: '#333',
|
||||||
success: (res) => {
|
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) {
|
if (newGender !== gender) {
|
||||||
|
// 更新页面数据
|
||||||
this.setData({
|
this.setData({
|
||||||
'userInfo.gender': newGender
|
'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: '性别已更新',
|
// 调用后端 API 将 newGender 保存到数据库
|
||||||
icon: 'success',
|
// ... callApiToUpdate({ gender: newGender }) ...
|
||||||
duration: 1500
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
<wxs src="./format.wxs" module="tools" />
|
||||||
<view class="setting-page">
|
<view class="setting-page">
|
||||||
|
|
||||||
<!-- 用户信息卡片 -->
|
<!-- 用户信息卡片 -->
|
||||||
@@ -24,7 +25,7 @@
|
|||||||
<view class="setting-card">
|
<view class="setting-card">
|
||||||
<view class="setting-item" bindtap="editNickname">
|
<view class="setting-item" bindtap="editNickname">
|
||||||
<view class="item-icon">
|
<view class="item-icon">
|
||||||
<image src="/images/icon-nickname.png" mode="widthFix"></image>
|
<t-icon name="article" size="40rpx" />
|
||||||
</view>
|
</view>
|
||||||
<view class="item-content">
|
<view class="item-content">
|
||||||
<text class="item-label">昵称</text>
|
<text class="item-label">昵称</text>
|
||||||
@@ -38,23 +39,23 @@
|
|||||||
<view class="item-divider"></view>
|
<view class="item-divider"></view>
|
||||||
|
|
||||||
<view class="setting-item" bindtap="editGender">
|
<view class="setting-item" bindtap="editGender">
|
||||||
<view class="item-icon">
|
<view class="item-icon">
|
||||||
<image src="/images/icon-gender.png" mode="widthFix"></image>
|
<t-icon name="accessibility" size="40rpx" />
|
||||||
</view>
|
</view>
|
||||||
<view class="item-content">
|
<view class="item-content">
|
||||||
<text class="item-label">性别</text>
|
<text class="item-label">性别</text>
|
||||||
<text class="item-value">{{userInfo.gender === 1 ? '男' : '女'}}</text>
|
<text class="item-value">{{tools.formatGender(userInfo.gender)}}</text>
|
||||||
</view>
|
</view>
|
||||||
<view class="item-arrow">
|
<view class="item-arrow">
|
||||||
<image src="/images/arrow-right.png" mode="widthFix"></image>
|
<image src="/images/arrow-right.png" mode="widthFix"></image>
|
||||||
</view>
|
</view>
|
||||||
</view>
|
</view>
|
||||||
|
|
||||||
<view class="item-divider"></view>
|
<view class="item-divider"></view>
|
||||||
|
|
||||||
<view class="setting-item" bindtap="editBirthdate">
|
<view class="setting-item" bindtap="editBirthdate">
|
||||||
<view class="item-icon">
|
<view class="item-icon">
|
||||||
<image src="/images/icon-birthday.png" mode="widthFix"></image>
|
<t-icon name="calendar-1" size="40rpx" />
|
||||||
</view>
|
</view>
|
||||||
<view class="item-content">
|
<view class="item-content">
|
||||||
<text class="item-label">生日</text>
|
<text class="item-label">生日</text>
|
||||||
@@ -69,7 +70,7 @@
|
|||||||
|
|
||||||
<view class="setting-item" bindtap="editPhone">
|
<view class="setting-item" bindtap="editPhone">
|
||||||
<view class="item-icon">
|
<view class="item-icon">
|
||||||
<image src="/images/icon-phone.png" mode="widthFix"></image>
|
<t-icon name="call" size="40rpx" />
|
||||||
</view>
|
</view>
|
||||||
<view class="item-content">
|
<view class="item-content">
|
||||||
<text class="item-label">手机号</text>
|
<text class="item-label">手机号</text>
|
||||||
@@ -89,7 +90,7 @@
|
|||||||
<view class="setting-card">
|
<view class="setting-card">
|
||||||
<view class="setting-item" bindtap="changePassword">
|
<view class="setting-item" bindtap="changePassword">
|
||||||
<view class="item-icon">
|
<view class="item-icon">
|
||||||
<image src="/images/icon-password.png" mode="widthFix"></image>
|
<t-icon name="fingerprint-1" size="40rpx" />
|
||||||
</view>
|
</view>
|
||||||
<view class="item-content">
|
<view class="item-content">
|
||||||
<text class="item-label">修改密码</text>
|
<text class="item-label">修改密码</text>
|
||||||
@@ -103,7 +104,7 @@
|
|||||||
|
|
||||||
<view class="setting-item" bindtap="managePrivacy">
|
<view class="setting-item" bindtap="managePrivacy">
|
||||||
<view class="item-icon">
|
<view class="item-icon">
|
||||||
<image src="/images/icon-privacy.png" mode="widthFix"></image>
|
<t-icon name="hard-drive" size="40rpx" />
|
||||||
</view>
|
</view>
|
||||||
<view class="item-content">
|
<view class="item-content">
|
||||||
<text class="item-label">隐私设置</text>
|
<text class="item-label">隐私设置</text>
|
||||||
@@ -122,7 +123,7 @@
|
|||||||
<view class="setting-card">
|
<view class="setting-card">
|
||||||
<view class="setting-item" bindtap="showAbout">
|
<view class="setting-item" bindtap="showAbout">
|
||||||
<view class="item-icon">
|
<view class="item-icon">
|
||||||
<image src="/images/icon-about.png" mode="widthFix"></image>
|
<t-icon name="lighthouse" size="40rpx" />
|
||||||
</view>
|
</view>
|
||||||
<view class="item-content">
|
<view class="item-content">
|
||||||
<text class="item-label">关于我们</text>
|
<text class="item-label">关于我们</text>
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ Component({
|
|||||||
refreshUserStatus(){
|
refreshUserStatus(){
|
||||||
console.log("主动刷新user界面")
|
console.log("主动刷新user界面")
|
||||||
const app = getApp();
|
const app = getApp();
|
||||||
console.log("全局userinfo",app.globalData.userInfo)
|
console.log("全局userinfo头像:",app.globalData.userInfo.avatar_url)
|
||||||
const userInfo = app.globalData.userInfo || {};
|
const userInfo = app.globalData.userInfo || {};
|
||||||
console.log("userInfo 的数据类型:", typeof userInfo);
|
console.log("userInfo 的数据类型:", typeof userInfo);
|
||||||
console.log("user中的userInfo:",userInfo)
|
console.log("user中的userInfo:",userInfo)
|
||||||
@@ -39,6 +39,7 @@ Component({
|
|||||||
});
|
});
|
||||||
// 调试:确认页面数据是否正确
|
// 调试:确认页面数据是否正确
|
||||||
console.log("页面username赋值后:", this.data.username);
|
console.log("页面username赋值后:", this.data.username);
|
||||||
|
console.log("用户页面profile:",this.data.userinfo.avatar_url)
|
||||||
},
|
},
|
||||||
onRefresherRefresh() {
|
onRefresherRefresh() {
|
||||||
console.log("触发刷新");
|
console.log("触发刷新");
|
||||||
@@ -67,6 +68,24 @@ Component({
|
|||||||
});
|
});
|
||||||
|
|
||||||
console.log("更新后用户名为:",this.data.userinfo.username)
|
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 中
|
// 假设后端返回的数据包含在 res.data.data 或 res.data.userinfo 中
|
||||||
@@ -76,6 +95,7 @@ Component({
|
|||||||
this.setData({
|
this.setData({
|
||||||
refresherTriggered: false
|
refresherTriggered: false
|
||||||
}); // <-- 语法已修正
|
}); // <-- 语法已修正
|
||||||
|
|
||||||
|
|
||||||
// ⭐️ 建议:调用方法来处理新数据并显示 Toast
|
// ⭐️ 建议:调用方法来处理新数据并显示 Toast
|
||||||
// 假设你有一个方法来处理返回的用户数据,并同时关闭刷新动画和提示
|
// 假设你有一个方法来处理返回的用户数据,并同时关闭刷新动画和提示
|
||||||
@@ -124,6 +144,11 @@ Component({
|
|||||||
// });
|
// });
|
||||||
|
|
||||||
// }, 3000); // ⭐️ 延迟时间已修改为 3000 毫秒
|
// }, 3000); // ⭐️ 延迟时间已修改为 3000 毫秒
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
},
|
},
|
||||||
onRefresherPulling() {
|
onRefresherPulling() {
|
||||||
console.log("正在下拉...");
|
console.log("正在下拉...");
|
||||||
|
|||||||
@@ -15,7 +15,7 @@
|
|||||||
<view class="user-contain">
|
<view class="user-contain">
|
||||||
<!-- 头像与用户名区域 -->
|
<!-- 头像与用户名区域 -->
|
||||||
<view class="user-avatar">
|
<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">
|
<a class="avatar-font" bind:tap="userlogin">
|
||||||
{{hasToken ? (userinfo.username || '用户中心') : '点击登录/注册'}}
|
{{hasToken ? (userinfo.username || '用户中心') : '点击登录/注册'}}
|
||||||
</a>
|
</a>
|
||||||
|
|||||||
Reference in New Issue
Block a user