105 lines
2.6 KiB
JavaScript

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() {
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) {
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();
this.setData({ loading: true });
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;
this.setData({
records: newRecords,
pageNo: pageNo + 1,
noMore: currentTotal >= totalRows
});
})
.catch(() => {
wx.showToast({ title: '加载失败', icon: 'none' });
})
.finally(() => {
this.setData({ loading: false });
});
}
});