// pages/zuzhangfenhongjilu/zuzhangfenhongjilu.js import request from '../../utils/request.js'; const PAGE_SIZE = 5; // 每页数据条数 const REFRESH_COOLDOWN = 2000; // 刷新冷却时间 (ms) const app = getApp(); Page({ data: { // 统计数据 fenhongZonge: '0.00', // 分红总额 yaoqingGuanshi: 0, // 邀请管事数 fenhongCishu: 0, // 分红次数 // 列表数据 jiluList: [], // 分红记录列表 totalCount: 0, // 总记录数(后端返回) currentPage: 1, // 当前页码 hasMore: true, // 是否还有更多数据 loading: false, // 首次加载中 loadingMore: false, // 加载更多中 refreshDisabled: false, // 刷新按钮是否冷却 // 错误状态 errorMsg: '', // 错误提示信息 }, onLoad() { this.loadFirstPage(); }, onShow() { this.registerNotification(); }, // 注册全局通知组件 registerNotification() { const notificationComp = this.selectComponent('#global-notification'); if (notificationComp && notificationComp.showNotification) { app.globalData.globalNotification = { show: (data) => notificationComp.showNotification(data), hide: () => notificationComp.hideNotification() }; } }, // 加载第一页数据 loadFirstPage() { this.setData({ currentPage: 1, hasMore: true, jiluList: [], errorMsg: '' }); this.fetchData(true); }, // 刷新数据(点击刷新按钮) onRefresh() { if (this.data.refreshDisabled) { wx.showToast({ title: `请稍后重试`, icon: 'none', duration: 1000 }); return; } // 启用冷却 this.setData({ refreshDisabled: true }); setTimeout(() => { this.setData({ refreshDisabled: false }); }, REFRESH_COOLDOWN); // 显示刷新动画(让按钮旋转) // 执行刷新 this.loadFirstPage(); }, // 上拉加载更多 onLoadMore() { const { hasMore, loading, loadingMore, currentPage } = this.data; if (!hasMore || loading || loadingMore) return; this.setData({ loadingMore: true }); this.fetchData(false, currentPage + 1); }, // 核心数据获取方法 async fetchData(reset = false, page = 1) { const url = '/shangpin/zuzhangfenhong'; // 假设后端接口地址 if (reset) { this.setData({ loading: true, errorMsg: '' }); } try { const res = await request({ url, method: 'POST', data: { page: page, page_size: PAGE_SIZE } }); if (res.data.code === 200) { const data = res.data.data || {}; // 更新统计数据 this.setData({ fenhongZonge: data.fenhong_zonge || '0.00', yaoqingGuanshi: data.yaoqing_guanshi || 0, fenhongCishu: data.fenhong_cishu || 0, totalCount: data.total || 0 }); // 处理列表数据(拼接头像) const rawList = data.list || []; const processedList = this.processList(rawList); if (reset) { // 第一页 this.setData({ jiluList: processedList, currentPage: page, hasMore: processedList.length === PAGE_SIZE, // 如果等于页大小,认为可能还有更多 }); } else { // 加载更多 const newList = [...this.data.jiluList, ...processedList]; this.setData({ jiluList: newList, currentPage: page, hasMore: processedList.length === PAGE_SIZE, }); } // 如果加载后数据为空且是第一页,可以给出提示 if (reset && processedList.length === 0) { // 空状态已在 wxml 处理 } } else { // 业务错误 this.setData({ errorMsg: res.data.msg || '数据加载失败' }); this.showNotification('error', res.data.msg || '数据加载失败'); } } catch (err) { console.error('获取分红记录失败', err); this.setData({ errorMsg: '网络异常,请检查网络' }); this.showNotification('error', '网络异常'); } finally { if (reset) { this.setData({ loading: false }); } else { this.setData({ loadingMore: false }); } } }, // 处理列表项:拼接头像、格式化金额和时间 processList(list) { const ossUrl = app.globalData.ossImageUrl || ''; const defaultAvatar = ossUrl + (app.globalData.morentouxiang || 'avatar/default.jpg'); return list.map(item => ({ // 打手信息 dashouAvatar: this.getFullUrl(item.dashou_avatar, ossUrl, defaultAvatar), dashouNicheng: item.dashou_nicheng || '打手', dashouYonghuid: item.dashou_yonghuid || '', // 管事信息 guanshiAvatar: this.getFullUrl(item.guanshi_avatar, ossUrl, defaultAvatar), guanshiNicheng: item.guanshi_nicheng || '管事', guanshiYonghuid: item.guanshi_yonghuid || '', // 分红信息 fenhong: parseFloat(item.fenhong || 0).toFixed(2), shijian: this.formatTime(item.create_time) })); }, // 拼接完整图片URL getFullUrl(relativePath, ossUrl, defaultUrl) { if (!relativePath) return defaultUrl; if (relativePath.startsWith('http')) return relativePath; const full = ossUrl + (relativePath.startsWith('/') ? relativePath : '/' + relativePath); return full || defaultUrl; }, // 格式化时间(根据实际后端返回调整) formatTime(timestamp) { if (!timestamp) return ''; try { const date = new Date(timestamp); return `${date.getMonth() + 1}月${date.getDate()}日 ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`; } catch { return timestamp.substring(0, 10) || ''; } }, // 重试加载(当错误提示出现时) onRetry() { this.loadFirstPage(); }, // 显示通知(可选) showNotification(type, msg) { const noti = app.globalData.globalNotification; if (noti && noti.show) { noti.show({ type, message: msg }); } } });