226 lines
7.7 KiB
TypeScript
226 lines
7.7 KiB
TypeScript
import Toast from 'tdesign-miniprogram/toast/index';
|
||
import envConfig from '../../env';
|
||
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',
|
||
hasToken: false,
|
||
userinfo: null,
|
||
refresherTriggered: false,
|
||
article_num:0
|
||
|
||
},
|
||
methods: {
|
||
userlogin(){
|
||
wx.navigateTo({url:'/pages/user-login/user-login'})
|
||
},
|
||
refreshUserStatus(){
|
||
console.log("主动刷新user界面")
|
||
const app = getApp();
|
||
console.log("全局userinfo头像:",app.globalData.userInfo.avatar_url)
|
||
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状态
|
||
userinfo: userInfoObj
|
||
});
|
||
// 调试:确认页面数据是否正确
|
||
console.log("页面username赋值后:", this.data.username);
|
||
console.log("用户页面profile:",this.data.userinfo.avatar_url)
|
||
},
|
||
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)
|
||
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 中
|
||
// 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);
|
||
// 建议:显示失败提示
|
||
|
||
}
|
||
});
|
||
},
|
||
gotoMessage(){
|
||
console.log("跳转到notifitions界面")
|
||
wx.navigateTo({
|
||
url: `/pages/notifications/notifications?id=${this.data.userinfo.uid}`,
|
||
});
|
||
},
|
||
gotoSetting(){
|
||
wx.navigateTo({
|
||
url: `/pages/setting/setting?id=${this.data.userinfo.uid}`,
|
||
});
|
||
},
|
||
gotoFeedback(){
|
||
wx.navigateTo({
|
||
url: `/pages/opinion/opinion?id=${this.data.userinfo.uid}`,
|
||
});
|
||
},
|
||
gotoHelp(){
|
||
wx.navigateTo({
|
||
url: `/pages/helpcenter/helpcenter?id=${this.data.userinfo.uid}`,
|
||
});
|
||
},
|
||
gotoDaily(){
|
||
wx.navigateTo({
|
||
url: `/pages/daily/daily?id=${this.data.userinfo.uid}`,
|
||
});
|
||
}
|
||
|
||
|
||
},
|
||
lifetimes:{
|
||
attached(){
|
||
console.log("user界面刷新")
|
||
this.refreshUserStatus()
|
||
this.getArticleNum()
|
||
}
|
||
|
||
},
|
||
|
||
|
||
|
||
})
|