yingsa/miniprogram/pages/match/history/index.js

96 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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() {
const currentStore = app.globalData.currentStore
if (!currentStore?.storeId) {
return
}
this.setData({ loading: true })
try {
const res = await app.request('/api/match/my-matches', {
store_id: currentStore.storeId,
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 {
...match,
powerChange: powerChange,
confirmedAt: util.formatDate(match.confirmedAt)
}
})
this.setData({
matches: this.data.page === 1 ? matches : [...this.data.matches, ...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()
}
})