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