const app = getApp(); const util = require("../../../utils/util"); Page({ data: { matches: [], loading: false, page: 1, pageSize: 20, hasMore: true, }, onLoad() { this.fetchMatches(); }, onShow() { // 门店切换后刷新数据 if (app.globalData.storeChanged) { app.globalData.storeChanged = false; this.setData({ page: 1, hasMore: true, matches: [] }); this.fetchMatches(); } }, onPullDownRefresh() { this.setData({ page: 1, hasMore: true }); this.fetchMatches().then(() => { wx.stopPullDownRefresh(); }); }, onReachBottom() { if (this.data.hasMore && !this.data.loading) { this.loadMore(); } }, async fetchMatches() { this.setData({ loading: true }); try { // 不传 store_id,获取全部门店的比赛记录 const res = await app.request("/api/match/my-matches", { page: this.data.page, pageSize: this.data.pageSize, }); const matches = (res.data.list || []).map((match) => { // 确保 powerChange 是数字类型,移除可能存在的加号和其他非数字字符 let powerChange = match.powerChange; if (powerChange != null && powerChange !== undefined) { // 如果是字符串,移除所有加号、空格等非数字字符(保留负号) if (typeof powerChange === "string") { // 保留负号,移除所有加号和其他字符 const cleaned = powerChange.replace(/\+/g, "").trim(); powerChange = parseFloat(cleaned) || 0; } // 确保是数字类型 powerChange = Number(powerChange); // 如果是 NaN,设为 0 if (isNaN(powerChange)) { powerChange = 0; } } else { powerChange = 0; } return Object.assign({}, match, { powerChange: powerChange, confirmedAt: util.formatDate(match.confirmedAt), }); }); this.setData({ matches: this.data.page === 1 ? matches : this.data.matches.concat(matches), hasMore: matches.length >= this.data.pageSize, }); } catch (e) { console.error("获取比赛记录失败:", e); } finally { this.setData({ loading: false }); } }, loadMore() { this.setData({ page: this.data.page + 1 }); this.fetchMatches(); }, });