Commit 1cb56752 authored by hewenhao's avatar hewenhao

feat: paging

parent d5254d3c
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router"; import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import Bonus from "../views/bonus/index.vue"; import Bonus from "../views/bonus/index.vue";
const routes: Array<RouteRecordRaw> = [ const routes: Array<RouteRecordRaw> = [
{ {
path: "/", path: "/",
name: "Bonus", name: "Bonus",
component: Bonus component: Bonus
}, },
{ {
path: "/bonusDetail", path: "/bonusDetail",
name: "bonusDetail", name: "bonusDetail",
component: () => import("../views/bonus/bonus-detail/index.vue") component: () => import("../views/bonus/bonus-detail/index.vue")
}, },
{ {
path: "/withdraw", path: "/withdraw",
name: "withdraw", name: "withdraw",
component: () => import("../views/bonus/withdraw/index.vue") component: () => import("../views/bonus/withdraw/index.vue")
}, },
{ {
path: "/test", path: "/test",
name: "test", name: "test",
component: () => import("../views/test/index.vue") component: () => import("../views/test/index.vue")
} },
]; {
path: "/test/paging",
const router = createRouter({ name: "test-paging",
history: createWebHashHistory(), component: () => import("../views/test/paging.vue")
routes }
}); ];
export default router; const router = createRouter({
history: createWebHashHistory(),
routes
});
export default router;
/* eslint-disable @typescript-eslint/no-explicit-any */
import { reactive, UnwrapRef } from "vue";
type PagingFuncRet<T = object> = {
list: T[] | null;
hasNextPage: boolean;
total: number;
};
type BasePagingParams = {
pageNum: number;
pageSize: number;
};
interface Paging<T = object> extends BasePagingParams {
list: T[];
isLoad: boolean;
loading: boolean;
refreshing: boolean;
hasNextPage: boolean;
error: null | string;
total: number;
}
export type PagingFunc<T = object, P = any> = (
arg: Partial<P> & BasePagingParams
) => Promise<PagingFuncRet<T>>;
function usePaging<T = object, P = any>(
fn: PagingFunc<T, P>,
paramsFn?: () => Partial<P>
): [UnwrapRef<Paging<T>>, (arg?: Partial<P>) => void, (arg?: Partial<P>) => void] {
const paging = reactive<Paging<T>>({
list: [],
isLoad: false,
pageNum: 1,
pageSize: 15,
loading: false,
refreshing: false,
hasNextPage: false,
error: null,
total: 0
});
const request = (isRefresh: boolean) => {
return async (arg: Partial<P> = {}) => {
if (paging.loading || paging.refreshing) return;
if (!isRefresh && !paging.hasNextPage) return;
if (isRefresh) {
paging.refreshing = true;
} else {
paging.loading = true;
}
const currentPage = isRefresh ? 1 : paging.pageNum + 1;
try {
const params = paramsFn ? paramsFn() : {};
const ret = await fn({
...arg,
...params,
pageNum: currentPage,
pageSize: paging.pageSize
});
if (Array.isArray(ret.list)) {
if (isRefresh) {
paging.list = ret.list as UnwrapRef<T[]>;
} else {
paging.list = paging.list.concat(ret.list as UnwrapRef<T[]>);
}
}
paging.total = ret.total;
paging.hasNextPage = ret.hasNextPage;
paging.pageNum = currentPage;
} catch (error) {
paging.error = error.message;
} finally {
if (isRefresh) {
paging.refreshing = false;
} else {
paging.loading = false;
}
paging.isLoad = true;
}
};
};
const loadMore = request(false);
const refresh = request(true);
return [paging, loadMore, refresh];
}
export { usePaging };
<template>
<div>
<div v-if="paging.refreshing">refreshing</div>
<PullRefresh :modelValue="paging.refreshing" @refresh="onRefresh">
<List @load="loadMore" :modelValue="paging.loading" :finished="!paging.hasNextPage">
<div v-for="item in paging.list" class="item" :key="item.index">
{{ item.a }}
</div>
</List>
</PullRefresh>
<div v-if="paging.loading">loading</div>
<div v-if="paging.isLoad && !paging.hasNextPage">没有更多了</div>
</div>
</template>
<script lang="ts" setup>
import type { PagingFunc } from '@/util/use-paging';
import { usePaging } from "@/util/use-paging";
import { onMounted } from "vue";
import { PullRefresh, List } from 'vant';
type ListType = {
a: number;
index: number;
};
const fn: PagingFunc<ListType, { test: number }> = async (arg) => {
await new Promise(resolve => setTimeout(resolve, 1000));
console.log(arg)
return {
hasNextPage: arg.pageNum < 4,
total: 54,
list: new Array(20).fill(0).map((_, index) => ({
a: Math.random(),
index
}))
};
};
const [paging, loadMore, onRefresh] = usePaging(fn, () => ({ test: 1 }));
onMounted(() => {
onRefresh();
});
</script>
<style lang="scss">
.item {
height: 80px;
}
</style>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment