Parcourir la source

Merge branch 'develop' of ssh://gogs.zlhuiyun.com:8080/szr190/ShenhaiH5 into develop

fengyu il y a 2 mois
Parent
commit
74f272a41f
3 fichiers modifiés avec 85 ajouts et 2 suppressions
  1. 83 0
      src/composables/useListLoader.ts
  2. 1 1
      src/manifest.json
  3. 1 1
      src/pages.json

+ 83 - 0
src/composables/useListLoader.ts

@@ -0,0 +1,83 @@
1
+import { ref } from 'vue'
2
+import { onPageScroll, onReachBottom } from '@dcloudio/uni-app'
3
+import useMescroll from '@/uni_modules/mescroll-uni/hooks/useMescroll.js'
4
+
5
+interface UseListLoaderOptions {
6
+  apiFn: (params: EmptyObjectType, ...args: any[]) => Promise<any>
7
+  initialParams?: EmptyObjectType
8
+  getApiParams?: () => any[]
9
+}
10
+
11
+interface UseListLoaderReturn {
12
+  listData: any
13
+  params: EmptyObjectType
14
+  totalCount: EmptyObjectType
15
+  extend: any
16
+  handleSearch: () => void
17
+  upCallback: (mescroll: any) => Promise<void>
18
+  downCallback: () => void
19
+  mescrollInit: (mescroll: any) => void
20
+  getMescroll: () => any
21
+  updateApiParams: (callback: () => any[]) => void
22
+}
23
+
24
+export const useListLoader = ({ apiFn, initialParams = {}, getApiParams = () => [] }: UseListLoaderOptions): UseListLoaderReturn => {
25
+  const { mescrollInit, downCallback, getMescroll } = useMescroll(onPageScroll, onReachBottom)
26
+
27
+  const listData = ref<EmptyObjectType[]>([])
28
+  const params = ref(initialParams)
29
+  const apiParamsGetter = ref(getApiParams)
30
+  const totalCount = ref(0)
31
+  const extend = ref<any>({})
32
+
33
+  // 获取列表数据
34
+  const upCallback = async (mescroll: { num: number; size: number; endErr: () => void; endBySize: (length: number, total: number) => void }) => {
35
+    const query: PageQuery = {
36
+      pageNum: mescroll.num,
37
+      pageSize: mescroll.size,
38
+      ...params.value
39
+    }
40
+
41
+    try {
42
+      // 调用getter函数获取最新的参数
43
+      const apiParams = apiParamsGetter.value()
44
+      const res = await apiFn(query, ...apiParams)
45
+      const curPageData: any = res.rows || []
46
+
47
+      if (mescroll.num === 1) {
48
+        listData.value = []
49
+      }
50
+
51
+      listData.value = listData.value.concat(curPageData)
52
+      totalCount.value = res.total
53
+      extend.value = res.extend
54
+      mescroll.endBySize(curPageData.length, res.total)
55
+    } catch {
56
+      mescroll.endErr() // 请求失败, 结束加载
57
+    }
58
+  }
59
+
60
+  // 查询/重置搜索
61
+  const handleSearch = () => {
62
+    listData.value = []
63
+    getMescroll().resetUpScroll()
64
+  }
65
+
66
+  // 更新apiParams的getter函数
67
+  const updateApiParams = (callback: () => any[]) => {
68
+    apiParamsGetter.value = callback
69
+  }
70
+
71
+  return {
72
+    listData,
73
+    extend,
74
+    params,
75
+    totalCount,
76
+    handleSearch,
77
+    upCallback,
78
+    downCallback,
79
+    mescrollInit,
80
+    getMescroll,
81
+    updateApiParams
82
+  }
83
+}

+ 1 - 1
src/manifest.json

@@ -54,7 +54,7 @@
54 54
     "quickapp" : {},
55 55
     "h5" : {
56 56
         "router" : {
57
-            "base" : "./",
57
+            "base" : "/",
58 58
             "mode" : "history"
59 59
         }
60 60
     },

+ 1 - 1
src/pages.json

@@ -30,7 +30,7 @@
30 30
         "enablePullDownRefresh": false,
31 31
         "navigationStyle": "custom"
32 32
       }
33
-    },
33
+    }
34 34
   ],
35 35
   "subPackages": [
36 36
     {