Files
WAI_Project_UNIX/cool/hooks/pager.ts

114 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-08-11 14:40:55 +08:00
import { computed, ref } from "vue";
2025-08-11 09:54:23 +08:00
import { assign, parse } from "../utils";
2025-08-11 14:40:55 +08:00
import { useListView, type ClListViewItem } from "@/uni_modules/cool-ui";
2025-08-11 09:54:23 +08:00
2025-08-11 14:40:55 +08:00
// 分页参数类型
2025-08-11 09:54:23 +08:00
type Pagination = {
2025-08-11 14:40:55 +08:00
page: number; // 当前页码
size: number; // 每页数量
total: number; // 总数量
2025-08-11 09:54:23 +08:00
};
2025-08-11 14:40:55 +08:00
// 分页响应数据类型
2025-08-11 09:54:23 +08:00
type PagerResponse = {
2025-08-11 14:40:55 +08:00
list: UTSJSONObject[]; // 列表数据
pagination: Pagination; // 分页信息
2025-08-11 09:54:23 +08:00
};
2025-08-11 14:40:55 +08:00
// 分页回调函数类型
2025-09-15 11:25:24 +08:00
type PagerCallback = (params: UTSJSONObject, ctx: Pager) => void | Promise<void>;
2025-08-11 09:54:23 +08:00
2025-08-11 14:40:55 +08:00
// 分页器类
2025-08-11 09:54:23 +08:00
export class Pager {
2025-08-11 14:40:55 +08:00
public page = 1; // 当前页码
public size = 20; // 每页数量
public total = 0; // 总数量
public list = ref<UTSJSONObject[]>([]); // 列表数据
public loading = ref(false); // 加载状态
public refreshing = ref(false); // 刷新状态
public finished = ref(false); // 是否加载完成
public params = {} as UTSJSONObject; // 请求参数
public cb: PagerCallback | null = null; // 回调函数
// 构造函数
2025-08-11 09:54:23 +08:00
constructor(cb: PagerCallback) {
this.cb = cb;
}
2025-08-11 14:40:55 +08:00
// 完成加载
2025-08-11 09:54:23 +08:00
done() {
this.loading.value = false;
}
2025-08-11 14:40:55 +08:00
// 清空数据
2025-08-11 09:54:23 +08:00
clear() {
this.list.value = [];
this.finished.value = false;
this.refreshing.value = false;
this.loading.value = false;
}
2025-09-15 11:25:24 +08:00
// 渲染数据
public render = (res: any) => {
const { list, pagination } = parse<PagerResponse>(res)!;
// 更新分页信息
this.page = pagination.page;
this.size = pagination.size;
this.total = pagination.total;
// 更新列表数据
if (this.params.page == 1) {
this.list.value = [...list];
} else {
this.list.value.push(...list);
}
// 更新加载完成状态
this.finished.value = this.list.value.length >= this.total;
// 完成加载
this.done();
};
2025-08-11 14:40:55 +08:00
// 刷新数据
2025-09-15 11:25:24 +08:00
public refresh = async (params: UTSJSONObject) => {
// 合并参数
this.params = assign(this.params, params);
// 构建请求参数
const data = {
page: this.page,
size: this.size,
...this.params
};
// 开始加载
this.loading.value = true;
// 发起请求
await this.cb!(data, this);
2025-08-11 09:54:23 +08:00
};
2025-08-11 14:40:55 +08:00
// 加载更多数据
2025-08-11 09:54:23 +08:00
public loadMore = () => {
if (this.loading.value || this.finished.value) {
return;
}
2025-08-11 14:40:55 +08:00
this.refresh({
page: this.page + 1
});
2025-08-11 09:54:23 +08:00
};
2025-08-11 14:40:55 +08:00
// 列表视图数据
public listView = computed<ClListViewItem[]>(() => {
return useListView(this.list.value);
});
2025-08-11 09:54:23 +08:00
}
2025-08-11 14:40:55 +08:00
// 创建分页器实例
2025-08-11 16:25:32 +08:00
export const usePager = (cb: PagerCallback): Pager => {
2025-08-11 09:54:23 +08:00
return new Pager(cb);
2025-08-11 16:25:32 +08:00
};