/** * 分页混入 * 提供统一的分页逻辑管理 */ export default { data() { return { // 分页相关数据 pageNum: 1, pageSize: 20, total: 0, list: [], loading: false, hasMore: true, isRefreshing: false } }, methods: { /** * 初始化分页数据 */ initPagination() { this.pageNum = 1 this.list = [] this.total = 0 this.hasMore = true this.loading = false this.isRefreshing = false }, /** * 加载数据 * @param {Function} apiCall - API调用函数 * @param {Object} params - 请求参数 */ async loadData(apiCall, params = {}) { if (this.loading) return this.loading = true try { const requestParams = { pageNum: this.pageNum, pageSize: this.pageSize, ...params } const res = await apiCall(requestParams) if (res.code === 200) { this.total = res.total || 0 if (this.pageNum === 1) { // 第一页,直接替换数据 this.list = res.rows || res.data || [] } else { // 后续页面,追加数据 this.list = this.list.concat(res.rows || res.data || []) } // 判断是否还有更多数据 this.hasMore = this.list.length < this.total // 页码递增 if (this.hasMore) { this.pageNum++ } } else { uni.showToast({ title: res.msg || '请求失败', icon: 'none', duration: 2000 }) } } catch (error) { console.error('加载数据失败:', error) uni.showToast({ title: '网络错误,请重试', icon: 'none', duration: 2000 }) } finally { this.loading = false this.isRefreshing = false } }, /** * 下拉刷新 * @param {Function} apiCall - API调用函数 * @param {Object} params - 请求参数 */ async onRefresh(apiCall, params = {}) { this.isRefreshing = true this.pageNum = 1 this.hasMore = true await this.loadData(apiCall, params) }, /** * 上拉加载更多 * @param {Function} apiCall - API调用函数 * @param {Object} params - 请求参数 */ async onLoadMore(apiCall, params = {}) { if (!this.hasMore || this.loading) return await this.loadData(apiCall, params) }, /** * 搜索或筛选时重置分页 * @param {Function} apiCall - API调用函数 * @param {Object} params - 请求参数 */ async onSearch(apiCall, params = {}) { this.pageNum = 1 this.hasMore = true await this.loadData(apiCall, params) } } }