diff --git a/App.uvue b/App.uvue index 4fbd438..ce9b43c 100644 --- a/App.uvue +++ b/App.uvue @@ -48,4 +48,9 @@ export default { margin-top: 0; } } + +.uni-toast { + border-radius: 32rpx; + background-color: rgba(0, 0, 0, 0.8) !important; +} diff --git a/cool/hooks/index.ts b/cool/hooks/index.ts index 2cf6176..fddd1ec 100644 --- a/cool/hooks/index.ts +++ b/cool/hooks/index.ts @@ -1,5 +1,6 @@ export * from "./refs"; export * from "./page"; +export * from "./pager"; export * from "./long-press"; export * from "./cache"; export * from "./parent"; diff --git a/cool/hooks/pager.ts b/cool/hooks/pager.ts new file mode 100644 index 0000000..62d10a2 --- /dev/null +++ b/cool/hooks/pager.ts @@ -0,0 +1,94 @@ +import { ref } from "vue"; +import { assign, parse } from "../utils"; +import type { Response } from "../service"; + +type Pagination = { + page: number; + size: number; + total: number; +}; + +type PagerResponse = { + list: UTSJSONObject[]; + pagination: Pagination; +}; + +type PagerCallback = (params: UTSJSONObject) => Promise; + +export class Pager { + public page = 1; + public size = 20; + public total = 0; + public list = ref([]); + public loading = ref(false); + public refreshing = ref(false); + public finished = ref(false); + public params = {} as UTSJSONObject; + public cb: PagerCallback | null = null; + + constructor(cb: PagerCallback) { + this.cb = cb; + } + + done() { + this.loading.value = false; + } + + clear() { + this.list.value = []; + this.finished.value = false; + this.refreshing.value = false; + this.loading.value = false; + } + + public refresh = async (params: UTSJSONObject): Promise => { + return new Promise((resolve, reject) => { + assign(this.params, params); + + const data = { + page: this.page, + size: this.size, + ...this.params + }; + + this.loading.value = true; + + this.cb!(data) + .then((res) => { + const { list, pagination } = parse(res)!; + + this.page = pagination.page; + this.size = pagination.size; + this.total = pagination.total; + this.finished.value = this.list.value.length >= this.total; + + if (data.page == 1) { + this.list.value = list; + } else { + this.list.value.push(...list); + } + + resolve(res); + }) + .catch((err) => { + reject(err); + }) + .finally(() => { + this.loading.value = false; + }); + }); + }; + + public loadMore = () => { + if (this.loading.value || this.finished.value) { + return; + } + + this.page += 1; + this.refresh({}); + }; +} + +export function usePager(cb: PagerCallback) { + return new Pager(cb); +} diff --git a/cool/store/user.ts b/cool/store/user.ts index b32cb5b..97679cd 100644 --- a/cool/store/user.ts +++ b/cool/store/user.ts @@ -57,7 +57,7 @@ export class User { } }) .catch(() => { - this.logout(); + // this.logout(); }); } } diff --git a/pages/demo/components/goods-item.uvue b/pages/demo/components/goods-item.uvue index 2c8bd4e..c1fde8f 100644 --- a/pages/demo/components/goods-item.uvue +++ b/pages/demo/components/goods-item.uvue @@ -1,13 +1,15 @@ diff --git a/pages/demo/data/list-view-refresh.uvue b/pages/demo/data/list-view-refresh.uvue index e4f92b5..d85963d 100644 --- a/pages/demo/data/list-view-refresh.uvue +++ b/pages/demo/data/list-view-refresh.uvue @@ -1,77 +1,117 @@ diff --git a/pages/index/home.uvue b/pages/index/home.uvue index 277e1d6..229f7ad 100644 --- a/pages/index/home.uvue +++ b/pages/index/home.uvue @@ -263,7 +263,7 @@ const data = computed(() => { label: t("列表刷新"), icon: "refresh-line", path: "/pages/demo/data/list-view-refresh", - disabled: true + disabled: false }, { label: t("瀑布流"), diff --git a/types/uni-app.d.ts b/types/uni-app.d.ts index d7756b4..52c6fb1 100644 --- a/types/uni-app.d.ts +++ b/types/uni-app.d.ts @@ -179,6 +179,24 @@ declare interface UniScrollEvent extends UniEvent { }; } +declare interface UniScrollToUpperEvent extends UniEvent { + detail: { + direction: string; + }; +} + +declare interface UniScrollToLowerEvent extends UniEvent { + detail: { + direction: string; + }; +} + +declare interface UniRefresherEvent extends UniEvent { + detail: { + dy: number; + }; +} + declare interface UniSwiperChangeEvent extends UniEvent { detail: { current: number; diff --git a/uni_modules/cool-ui/components/cl-list-view/cl-list-view.uvue b/uni_modules/cool-ui/components/cl-list-view/cl-list-view.uvue index a70f797..9a3156c 100644 --- a/uni_modules/cool-ui/components/cl-list-view/cl-list-view.uvue +++ b/uni_modules/cool-ui/components/cl-list-view/cl-list-view.uvue @@ -1,5 +1,5 @@