import { ref, unref } from "vue"; import { onReachBottom, onPullDownRefresh } from "@dcloudio/uni-app"; /** * usePageData页面公共方法 * * @param options */ interface AgmsTs { [propName : string] : any; } export function usePageData(ApiFn : Function, agms : AgmsTs) { const LoadStatus = ref<'loadmore' | 'loading' | 'nomore' | ''>(''); // 加载状态 const RowsList = ref([]); // 列表数据 const params = ref(null); // 请求参数 const Pages = ref({ pageNum: 1, pageSize: 10 }) const Total = ref(0); const initData = (p) => { params.value = p; Pages.value.pageNum = 0; getLsit() }; const getLsit = async () => { if (LoadStatus.value === 'loading') return try { Pages.value.pageNum += 1; if (unref(Pages).pageNum === 1) { RowsList.value = [] } LoadStatus.value = 'loading'; const P_ = { ...unref(Pages), ...(unref(params) || {}) }; const { rows, total } = await ApiFn(P_); RowsList.value = RowsList.value.concat(rows || []); Total.value = total; uni.stopPullDownRefresh(); if (unref(RowsList).length < total) { LoadStatus.value = 'loadmore'; } else { LoadStatus.value = 'nomore'; } } catch (error) { uni.stopPullDownRefresh(); LoadStatus.value = 'nomore'; } } onReachBottom(() => { if (LoadStatus.value === 'loadmore') getLsit(); }) onPullDownRefresh(() => { initData(params.value); }) return { initData, LoadStatus, RowsList, Total } }