// pages/match/index.js const app = getApp(); Page({ /** * 页面的初始数据 */ data: { appAssetsUrl2: app.appAssetsUrl2, matchList: [], statusBarHeightTop: 0, statusBarHeight: 0, // 分页相关数据 pageNum: 1, pageSize: 10, total: 0, hasMore: true, isLoading: false }, /** * 赛事项目点击事件 */ onMatchItemTap(e) { const index = e.currentTarget.dataset.index; const matchItem = this.data.matchList[index]; console.log('点击赛事项目:', matchItem); // 跳转到详情页面 wx.navigateTo({ url: `/pages/match/details/index?id=${matchItem.id}` }); }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.height(); // 加载赛事数据 this.loadMatchData(); }, /** * 加载赛事数据 */ loadMatchData() { // 如果正在加载或没有更多数据,则不执行 if (this.data.isLoading || !this.data.hasMore) { return; } this.setData({ isLoading: true }); wx.showLoading({ title: this.data.pageNum === 1 ? '加载中...' : '加载更多...' }); // 调用API获取赛事数据 const params = { pageNum: this.data.pageNum, pageSize: this.data.pageSize }; // 使用项目中已有的请求方法 app._get('news/page', params, (res) => { wx.hideLoading(); this.setData({ isLoading: false }); if (res.code === 0) { // 处理返回的数据 const newList = res.page.list || []; const total = res.page.totalPage || 0; // 如果是第一页,直接设置数据;否则追加数据 const matchList = this.data.pageNum === 1 ? newList : [...this.data.matchList, ...newList]; this.setData({ matchList: matchList, total: total, hasMore: matchList.length < total }); } else { wx.showToast({ title: '数据加载失败', icon: 'none' }); } }, (fail) => { wx.hideLoading(); this.setData({ isLoading: false }); wx.showToast({ title: '请求失败', icon: 'none' }); }); }, /** * 重置并重新加载数据 */ reloadMatchData() { this.setData({ pageNum: 1, matchList: [], hasMore: true }); this.loadMatchData(); }, height() { const { platform, statusBarHeight } = wx.getSystemInfoSync(); let statusBarHeightTop = statusBarHeight; let height = statusBarHeight + 4; //ios 24px let mH = statusBarHeight + 4; if (platform.toLowerCase() == "android") { height += 4; //android 28px mH += 4; } height = height + 100; // height = height + 38 + 118; // 胶囊高度 32px 下边框6px height 状态栏高度 this.setData({ statusBarHeightTop: statusBarHeightTop + "px", statusBarHeight: height + "px", statusBarMH: mH + "px", }); }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { // 页面渲染完成 }, /** * 生命周期函数--监听页面显示 */ onShow() { // 页面显示时刷新数据 }, /** * 生命周期函数--监听页面隐藏 */ onHide() { // 页面隐藏 }, /** * 生命周期函数--监听页面卸载 */ onUnload() { // 页面卸载 }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { // 下拉刷新 this.reloadMatchData(); wx.stopPullDownRefresh(); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { // 上拉加载更多 console.log('加载更多赛事数据'); if (this.data.hasMore && !this.data.isLoading) { this.setData({ pageNum: this.data.pageNum + 1 }); this.loadMatchData(); } }, /** * 用户点击右上角分享 */ onShareAppMessage() { } });