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) {
const tab = Number(e.currentTarget.dataset.tab);
if (tab === this.data.activeTab) return;
this.setData({ activeTab: tab });
this.resetAndLoad();
},
/** 重置并加载 */
resetAndLoad() {
this.setData({ records: [], pageNo: 1, noMore: false });
return this.loadRecords();
},
/** 加载更多 */
onLoadMore() {
if (this.data.loading || this.data.noMore) return;
this.loadRecords();
},
/** 加载积分记录 */
loadRecords() {
if (this.data.loading) 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
.fetchMyPoints({ pageNo, pageSize: PAGE_SIZE })
.then((result) => {
const res = result || {};
const allRows = res.rows || [];
// 按类型筛选: type 1=获取 2=消耗
const filtered = allRows.filter((item) => item.type === activeTab);
const formatted = filtered.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 || 0;
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
}
});