2025-08-11 00:40:21 +08:00
|
|
|
|
import Toast from 'tdesign-miniprogram/toast/index';
|
2025-09-27 17:24:52 +08:00
|
|
|
|
import envConfig from '../../env';
|
2025-08-09 17:41:14 +08:00
|
|
|
|
Component({
|
|
|
|
|
|
data: {
|
|
|
|
|
|
img1: 'https://tdesign.gtimg.com/mobile/demos/example1.png',
|
|
|
|
|
|
img2: 'https://tdesign.gtimg.com/mobile/demos/example2.png',
|
|
|
|
|
|
img3: 'https://tdesign.gtimg.com/mobile/demos/example3.png',
|
|
|
|
|
|
image: 'https://tdesign.gtimg.com/mobile/demos/avatar1.png',
|
2025-08-11 00:40:21 +08:00
|
|
|
|
hasToken: false,
|
2025-09-27 17:24:52 +08:00
|
|
|
|
userinfo: null,
|
|
|
|
|
|
refresherTriggered: false,
|
|
|
|
|
|
article_num:0
|
|
|
|
|
|
|
2025-08-09 17:41:14 +08:00
|
|
|
|
},
|
|
|
|
|
|
methods: {
|
|
|
|
|
|
userlogin(){
|
|
|
|
|
|
wx.navigateTo({url:'/pages/user-login/user-login'})
|
2025-08-11 00:40:21 +08:00
|
|
|
|
},
|
|
|
|
|
|
refreshUserStatus(){
|
|
|
|
|
|
console.log("主动刷新user界面")
|
|
|
|
|
|
const app = getApp();
|
|
|
|
|
|
console.log("全局userinfo",app.globalData.userInfo)
|
|
|
|
|
|
const userInfo = app.globalData.userInfo || {};
|
2025-09-27 17:24:52 +08:00
|
|
|
|
console.log("userInfo 的数据类型:", typeof userInfo);
|
2025-08-11 00:40:21 +08:00
|
|
|
|
console.log("user中的userInfo:",userInfo)
|
2025-09-27 17:24:52 +08:00
|
|
|
|
let userInfoObj = userInfo;
|
|
|
|
|
|
if (typeof userInfo === "string") {
|
|
|
|
|
|
try {
|
|
|
|
|
|
userInfoObj = JSON.parse(userInfo); // 字符串转对象
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
|
console.error("解析 userInfo 字符串失败:", e);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
console.log("应保存的username:",userInfoObj.username)
|
2025-08-11 00:40:21 +08:00
|
|
|
|
this.setData({
|
|
|
|
|
|
hasToken: !!app.globalData.token, // 从全局变量获取token状态
|
2025-09-27 17:24:52 +08:00
|
|
|
|
userinfo: userInfoObj
|
2025-08-11 00:40:21 +08:00
|
|
|
|
});
|
|
|
|
|
|
// 调试:确认页面数据是否正确
|
|
|
|
|
|
console.log("页面username赋值后:", this.data.username);
|
|
|
|
|
|
},
|
2025-09-27 17:24:52 +08:00
|
|
|
|
onRefresherRefresh() {
|
|
|
|
|
|
console.log("触发刷新");
|
|
|
|
|
|
this.getArticleNum()
|
|
|
|
|
|
|
|
|
|
|
|
// 模拟接口请求延迟
|
|
|
|
|
|
this.setData({
|
|
|
|
|
|
refresherTriggered: true
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
|
wx.request({
|
|
|
|
|
|
url: `${envConfig.apiBaseUrl}/user/getInfo`, // 替换为你的后端接口地址
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
data: {
|
|
|
|
|
|
"uid": this.data.userinfo.uid
|
|
|
|
|
|
},
|
|
|
|
|
|
success: (res) => {
|
|
|
|
|
|
console.log("后端刷新数据返回成功:", res.data);
|
|
|
|
|
|
wx.setStorageSync('ttk_userInfo', JSON.stringify(res.data.data));
|
|
|
|
|
|
this.setData({
|
|
|
|
|
|
userinfo: res.data.data
|
|
|
|
|
|
}, () => {
|
|
|
|
|
|
console.log("视图已更新,新用户名为:", this.data.userinfo.username);
|
|
|
|
|
|
// 3. 关闭动画并提示成功
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
console.log("更新后用户名为:",this.data.userinfo.username)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 假设后端返回的数据包含在 res.data.data 或 res.data.userinfo 中
|
|
|
|
|
|
// const newUserData = res.data.data;
|
|
|
|
|
|
|
|
|
|
|
|
// 修正语法:补上右括号 `)`
|
|
|
|
|
|
this.setData({
|
|
|
|
|
|
refresherTriggered: false
|
|
|
|
|
|
}); // <-- 语法已修正
|
|
|
|
|
|
|
|
|
|
|
|
// ⭐️ 建议:调用方法来处理新数据并显示 Toast
|
|
|
|
|
|
// 假设你有一个方法来处理返回的用户数据,并同时关闭刷新动画和提示
|
|
|
|
|
|
// this.handleSuccessfulRefresh(res.data);
|
|
|
|
|
|
|
|
|
|
|
|
// 如果你只是想关闭刷新动画和显示成功提示:
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
|
this.setData({
|
|
|
|
|
|
refresherTriggered: false
|
|
|
|
|
|
});
|
|
|
|
|
|
console.error("后端刷新请求失败:", err);
|
|
|
|
|
|
// 建议:显示失败提示
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}, 2000);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 2. 模拟接口请求延迟(你的数据加载逻辑将放在这里)
|
|
|
|
|
|
// setTimeout(() => {
|
|
|
|
|
|
// // 执行你的数据刷新逻辑,例如:this.refreshData();
|
|
|
|
|
|
// console.log("数据加载完成,准备关闭刷新动画。");
|
|
|
|
|
|
|
|
|
|
|
|
// // 3. 刷新完成后,设置状态为 false,关闭动画
|
|
|
|
|
|
// this.setData({
|
|
|
|
|
|
// refresherTriggered: false
|
|
|
|
|
|
// }, () => {
|
|
|
|
|
|
// console.log("完成刷新")
|
|
|
|
|
|
// // 确保动画关闭后,再提示用户成功
|
|
|
|
|
|
// const toast = this.selectComponent('#t-toast');
|
|
|
|
|
|
|
|
|
|
|
|
// if (toast) {
|
|
|
|
|
|
// // 确保 Toast 实例存在,并调用其 show 方法
|
|
|
|
|
|
// toast.show({
|
|
|
|
|
|
// message: '刷新成功',
|
|
|
|
|
|
// theme: 'success',
|
|
|
|
|
|
// duration: 1500 // 可选:设置显示时间
|
|
|
|
|
|
// });
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// console.error("未找到 t-toast 组件实例,请检查 WXML ID。");
|
|
|
|
|
|
// }
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
// }, 3000); // ⭐️ 延迟时间已修改为 3000 毫秒
|
|
|
|
|
|
},
|
|
|
|
|
|
onRefresherPulling() {
|
|
|
|
|
|
console.log("正在下拉...");
|
|
|
|
|
|
},
|
|
|
|
|
|
onRefresherRestore() {
|
|
|
|
|
|
console.log("刷新完成,已恢复原位");
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
getArticleNum(){
|
|
|
|
|
|
wx.request({
|
|
|
|
|
|
url: `${envConfig.apiBaseUrl}/article/getnum`, // 替换为你的后端接口地址
|
|
|
|
|
|
method: 'POST',
|
|
|
|
|
|
data: {
|
|
|
|
|
|
"uid": this.data.userinfo.uid
|
|
|
|
|
|
},
|
|
|
|
|
|
success: (res) => {
|
|
|
|
|
|
console.log("后端刷新数据getArticleNum返回成功:", res.data);
|
|
|
|
|
|
|
|
|
|
|
|
this.setData({
|
|
|
|
|
|
article_num: res.data.article_num
|
|
|
|
|
|
}, () => {
|
|
|
|
|
|
console.log("视图已更新,新用户名为:", this.data.article_num);
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
fail: (err) => {
|
|
|
|
|
|
console.error("后端刷新请求失败:", err);
|
|
|
|
|
|
// 建议:显示失败提示
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2025-09-28 00:14:39 +08:00
|
|
|
|
},
|
|
|
|
|
|
gotoMessage(){
|
|
|
|
|
|
console.log("跳转到notifitions界面")
|
|
|
|
|
|
wx.navigateTo({
|
|
|
|
|
|
url: `/pages/notifications/notifications?id=${this.data.userinfo.uid}`,
|
|
|
|
|
|
});
|
2025-09-28 00:31:57 +08:00
|
|
|
|
},
|
|
|
|
|
|
gotoSetting(){
|
|
|
|
|
|
wx.navigateTo({
|
|
|
|
|
|
url: `/pages/setting/setting?id=${this.data.userinfo.uid}`,
|
|
|
|
|
|
});
|
2025-09-27 17:24:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-08-11 00:40:21 +08:00
|
|
|
|
|
2025-08-09 17:41:14 +08:00
|
|
|
|
},
|
2025-08-11 00:40:21 +08:00
|
|
|
|
lifetimes:{
|
|
|
|
|
|
attached(){
|
|
|
|
|
|
console.log("user界面刷新")
|
|
|
|
|
|
this.refreshUserStatus()
|
2025-09-27 17:24:52 +08:00
|
|
|
|
this.getArticleNum()
|
2025-08-11 00:40:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-27 17:24:52 +08:00
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-08-09 17:41:14 +08:00
|
|
|
|
})
|