Files
pj_h5_cloud/src/sheep/hooks/use-page.ts
T
2025-01-09 14:14:43 +08:00

78 lines
1.7 KiB
TypeScript

import { reactive, onMounted } from "vue";
export const usePage = (
ref: any = null,
fn: Function | null = null,
sticky = null,
struct = "data"
) => {
const state = reactive<any>({
loading: true,
list: [],
headerHeight: 0,
scrollable: true,
});
const scroll = (e: any) => {
const scrollTop = e.detail.scrollTop;
if (scrollTop < state.headerHeight) {
state.scrollable = true;
} else {
state.scrollable = false;
}
};
onMounted(() => {
if (sticky != null) {
const query = uni.createSelectorQuery();
query
.select(sticky)
.boundingClientRect((data) => {
state.headerHeight = (data as any).height;
})
.exec();
}
});
const setPage = async (list: Array<any>) => {
if (ref == null) return;
state.list.totalList = await ref.value.complete(list);
};
const startPage = async (
page: number,
pagesize: number,
isPage = false,
query = {}
) => {
if (!fn || typeof fn !== "function") {
console.warn("startPage 方法请传入 pageFunc 参数 模拟5条假数据");
state.list = Array.from(new Array(5));
setPage(state.list);
state.startPage = startPage;
state.scroll = scroll;
return state;
}
const _data = {
current: page,
size: pagesize,
...query,
};
try {
const res = await fn(_data);
let _arr = res;
for (let key of struct.split(".")) {
_arr = _arr[key];
}
setPage(_arr);
} catch (e) {
console.log(e);
}
setTimeout((_) => {
if (state.loading == true) {
state.loading = false;
}
}, 500);
};
state.startPage = startPage;
state.scroll = scroll;
return state;
};