基本成型
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import Toast from 'tdesign-miniprogram/toast/index';
|
||||
import envConfig from '../../env';
|
||||
Component({
|
||||
data: {
|
||||
img1: 'https://tdesign.gtimg.com/mobile/demos/example1.png',
|
||||
@@ -6,7 +7,10 @@ Component({
|
||||
img3: 'https://tdesign.gtimg.com/mobile/demos/example3.png',
|
||||
image: 'https://tdesign.gtimg.com/mobile/demos/avatar1.png',
|
||||
hasToken: false,
|
||||
username: ''
|
||||
userinfo: null,
|
||||
refresherTriggered: false,
|
||||
article_num:0
|
||||
|
||||
},
|
||||
methods: {
|
||||
userlogin(){
|
||||
@@ -16,23 +20,155 @@ Component({
|
||||
console.log("主动刷新user界面")
|
||||
const app = getApp();
|
||||
console.log("全局userinfo",app.globalData.userInfo)
|
||||
console.log("全局用户信息:", app.globalData.userInfo.username); // 这里打印的是 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)
|
||||
this.setData({
|
||||
hasToken: !!app.globalData.token, // 从全局变量获取token状态
|
||||
username: userInfo.username
|
||||
userinfo: userInfoObj
|
||||
});
|
||||
// 调试:确认页面数据是否正确
|
||||
console.log("页面username赋值后:", this.data.username);
|
||||
},
|
||||
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);
|
||||
// 建议:显示失败提示
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
},
|
||||
lifetimes:{
|
||||
attached(){
|
||||
console.log("user界面刷新")
|
||||
this.refreshUserStatus()
|
||||
this.getArticleNum()
|
||||
}
|
||||
|
||||
}
|
||||
},
|
||||
|
||||
|
||||
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user