105 lines
2.6 KiB
JavaScript
Raw Normal View History

2026-03-30 18:08:27 +08:00
const auth = require('../../../utils/auth');
2026-03-31 23:27:35 +08:00
const { formatTime } = require('../../../utils/format');
const PAGE_SIZE = 10;
2026-03-30 18:08:27 +08:00
Page({
data: {
2026-03-31 23:27:35 +08:00
totalPoints: 0,
activeTab: 1, // 1-获取记录 2-消耗记录
records: [],
loading: false,
noMore: false,
pageNo: 1
2026-03-30 18:08:27 +08:00
},
onShow() {
const accessToken = wx.getStorageSync('accessToken') || '';
if (!accessToken) {
wx.redirectTo({ url: '/subpackages/mine/login/login' });
return;
}
this.loadProfile();
2026-03-31 23:27:35 +08:00
this.resetAndLoad();
},
onPullDownRefresh() {
this.loadProfile();
this.resetAndLoad().finally(() => wx.stopPullDownRefresh());
},
/** 返回 */
onBack() {
wx.navigateBack({ delta: 1 });
2026-03-30 18:08:27 +08:00
},
2026-03-31 23:27:35 +08:00
/** 加载积分总数 */
2026-03-30 18:08:27 +08:00
loadProfile() {
2026-03-31 23:27:35 +08:00
auth.fetchMyProfile().then((profile) => {
const p = profile || {};
this.setData({ totalPoints: p.points || 0 });
}).catch(() => {});
},
/** 切换Tab */
onSwitchTab(e) {
2026-04-04 18:57:50 +08:00
console.log('切换Tab', e.currentTarget.dataset);
2026-03-31 23:27:35 +08:00
const tab = Number(e.currentTarget.dataset.tab);
if (tab === this.data.activeTab) return;
this.setData({ activeTab: tab });
this.resetAndLoad();
},
/** 重置并加载 */
resetAndLoad() {
2026-04-04 18:57:50 +08:00
console.log('重置并加载');
2026-03-31 23:27:35 +08:00
this.setData({ records: [], pageNo: 1, noMore: false });
2026-04-04 18:57:50 +08:00
return this.loadRecords(true);
2026-03-31 23:27:35 +08:00
},
/** 上拉加载更多 */
onReachBottom() {
2026-03-31 23:27:35 +08:00
if (this.data.loading || this.data.noMore) return;
2026-04-04 18:57:50 +08:00
this.loadRecords(false);
2026-03-31 23:27:35 +08:00
},
/** 加载积分记录 */
2026-04-04 18:57:50 +08:00
loadRecords(isReset) {
if (this.data.loading && !isReset) return Promise.resolve();
2026-03-30 18:08:27 +08:00
this.setData({ loading: true });
2026-03-31 23:27:35 +08:00
const { pageNo, activeTab } = this.data;
return auth
2026-04-03 17:57:08 +08:00
.fetchMyPoints({ pageNo, pageSize: PAGE_SIZE, type: activeTab })
2026-03-31 23:27:35 +08:00
.then((result) => {
const res = result || {};
2026-04-03 17:57:08 +08:00
const allRows = res.rows || res.list || res.items || [];
2026-03-31 23:27:35 +08:00
2026-04-03 17:57:08 +08:00
const formatted = allRows.map((item) => ({
2026-03-31 23:27:35 +08:00
...item,
createdTimeStr: item.createdTime ? formatTime(item.createdTime) : '',
projectInfo: item.projectInfo || item.projectName || '',
rank: item.rank || ''
}));
const newRecords = this.data.records.concat(formatted);
2026-04-03 17:57:08 +08:00
const totalRows = res.total || res.totalCount || 0;
2026-03-31 23:27:35 +08:00
const currentTotal = pageNo * PAGE_SIZE;
2026-03-30 18:08:27 +08:00
this.setData({
2026-03-31 23:27:35 +08:00
records: newRecords,
pageNo: pageNo + 1,
noMore: currentTotal >= totalRows
2026-03-30 18:08:27 +08:00
});
})
2026-03-31 23:27:35 +08:00
.catch(() => {
wx.showToast({ title: '加载失败', icon: 'none' });
})
.finally(() => {
this.setData({ loading: false });
});
2026-03-30 18:08:27 +08:00
}
});