浏览代码

feat: list page cache

Your Name 2 年前
父节点
当前提交
56de856b38

+ 2
- 2
config/dev.js 查看文件

@@ -3,8 +3,8 @@ module.exports = {
3 3
     NODE_ENV: '"development"',
4 4
   },
5 5
   defineConstants: {
6
-    // HOST: '"http://127.0.0.1:9087"',
7
-    HOST: '"http://192.168.89.147:9087"',
6
+    HOST: '"http://127.0.0.1:9087"',
7
+    // HOST: '"http://192.168.89.147:9087"',
8 8
     AD_IMAGE: '"https://h5.njyunzhi.com/images/citizen_banner.png"',
9 9
     DEFAULT_POS: '"116.3476917447715,31.409912844296578"', // 霍山县人民政府 gcj02
10 10
     VERSION: '"1.1.13-20230325"',

+ 60
- 29
src/components/PowerList/index.jsx 查看文件

@@ -3,19 +3,20 @@ import Taro from "@tarojs/taro";
3 3
 import { View } from "@tarojs/components";
4 4
 import { PowerScrollView } from "@antmjs/vantui";
5 5
 import useShow from "@/layouts/hooks/useShow";
6
+import useHide from "@/layouts/hooks/useHide";
7
+import { throttle } from "@/utils/tools";
6 8
 
7 9
 export default React.forwardRef((props, ref) => {
8
-  const { request, params, renderItem, onLoadingChange, onDataChange } = props;
10
+  const { sid, request, params, renderItem, onLoadingChange, onDataChange } = props;
9 11
 
10 12
   const pageSize = 5;
11
-  const pageNumRef = React.useRef(0);
13
+  const pageShowRef = React.useRef(false);
14
+  const inited = React.useRef(false);
12 15
   const [loading, setLoading] = React.useState(false);
13 16
   const [list, setList] = React.useState([]);
14 17
   const [finished, setFinished] = React.useState(true);
15 18
   const listRef = React.useRef([]);
16
-  const paramsRef = React.useRef();
17 19
   listRef.current = list;
18
-  paramsRef.current = params || {};
19 20
 
20 21
   const changeLoading = (val) => {
21 22
     setLoading(val);
@@ -24,17 +25,19 @@ export default React.forwardRef((props, ref) => {
24 25
     }
25 26
   };
26 27
 
27
-  const queryData = React.useCallback(
28
+  const queryData = React.useCallback(throttle(
28 29
     (options = {}) => {
29 30
       return new Promise((resolve, reject) => {
30 31
         changeLoading(true);
31
-        request({
32
+
33
+        const queryParams = {
32 34
           pageSize,
33
-          ...paramsRef.current,
34 35
           ...options,
35
-        })
36
+        }
37
+
38
+        request(queryParams)
36 39
           .then((res) => {
37
-            const { records, current, pages } = res;
40
+            const { records, current, total } = res;
38 41
 
39 42
             let dataset =
40 43
               current == 1
@@ -45,51 +48,79 @@ export default React.forwardRef((props, ref) => {
45 48
             }
46 49
             setList(dataset);
47 50
 
48
-            setFinished(current >= pages);
51
+            setFinished(dataset.length >= total);
49 52
             changeLoading(false);
50 53
             resolve();
54
+
55
+            // 写入缓存
56
+            Taro.setStorage({
57
+              key: `list-${sid}`,
58
+              data: JSON.stringify({
59
+                params: queryParams,
60
+              }),
61
+            })
62
+
51 63
           })
52 64
           .catch((err) => {
53 65
             changeLoading(false);
54 66
             reject(err);
55 67
           });
56 68
       });
57
-    },
69
+    }),
58 70
     []
59 71
   );
60 72
 
61
-  const refresh = React.useCallback(() => {
62
-    console.log(pageNumRef);
63
-    pageNumRef.current = 1;
64
-
65
-    queryData({ pageNum: pageNumRef.current });
66
-    console.log("refresh" + pageNumRef.current);
67
-  }, [queryData]);
73
+  const refresh = React.useCallback((options = {}) => {
74
+    queryData({ ...options, pageNum: 1 });
75
+  }, []);
68 76
 
69 77
   const onScrollToLower = React.useCallback(
70
-    (event = 0, isRefresh = false) => {
71
-      console.log("onScrollToLower" + pageNumRef.current);
72
-      pageNumRef.current += 1;
73
-      queryData({ pageNum: pageNumRef.current });
78
+    (page) => {
79
+      const currentPage = Math.ceil(page / pageSize);
80
+      queryData({ pageNum: currentPage + 1, ...(params || {}) });
74 81
     },
75
-    []
82
+    [params]
76 83
   );
77 84
 
78 85
   useShow(() => {
79
-    refresh();
86
+    pageShowRef.current = true;
87
+
88
+    const paramStr = Taro.getStorageSync(`list-${sid}`);
89
+    const finalParams = paramStr ? JSON.parse(paramStr).params : { ...params || {} };
90
+
91
+    // 如果已经加载多页数据
92
+    // 那么需要重新加载
93
+    if (finalParams.pageNum && finalParams.pageNum > 1) {
94
+      finalParams.pageSize = finalParams.pageNum * finalParams.pageSize;
95
+      finalParams.pageNum = 1;
96
+    }
97
+
98
+    refresh(finalParams);
80 99
   });
81 100
 
101
+  useHide(() => {
102
+    pageShowRef.current = false;
103
+  });
82 104
   
105
+  // 页面隐藏之后, 还会执行一次 useEffect
106
+  // 不知道是否是 Taro 的 bug
83 107
   React.useEffect(() => {
84
-    refresh();
85
-  }, [refresh, params]);
108
+    if (!inited.current) {
109
+      // 首次进入, 清理缓存
110
+      Taro.removeStorageSync(`list-${sid}`);
111
+      inited.current = true;
112
+    }
113
+
114
+    if (pageShowRef.current) {
115
+      refresh(params);
116
+    }
117
+  }, [params]);
86 118
 
87 119
   React.useImperativeHandle(ref, () => ({
88
-    refresh,
120
+    refresh: () => refresh(params),
89 121
     updateData: (dt) => setList(dt),
90
-  }));
122
+  }), [params]);
91 123
  
92
-
93 124
   return (
94 125
     <PowerScrollView
95 126
       scrollY

+ 5
- 0
src/pages/apply/list/index.jsx 查看文件

@@ -14,6 +14,9 @@ const tabStyle = {
14 14
   overflow: "hidden",
15 15
 };
16 16
 
17
+const sid1 = Math.random().toString(36).substring(2, 8);
18
+const sid2 = Math.random().toString(36).substring(2, 8);
19
+
17 20
 export default (props) => {
18 21
   const router = Taro.useRouter();
19 22
   const { title, applyType = "" } = router.params;
@@ -66,6 +69,7 @@ export default (props) => {
66 69
       <Tabs sticky>
67 70
         <Tab title="督查员" style={tabStyle}>
68 71
           <PowerList
72
+            sid={sid1}
69 73
             request={getTaIssueApply}
70 74
             params={{ applyType, sourceType: "inspector", duty, issueId }}
71 75
             renderItem={(item) => (
@@ -80,6 +84,7 @@ export default (props) => {
80 84
         </Tab>
81 85
         <Tab title="市民" style={tabStyle}>
82 86
           <PowerList
87
+            sid={sid2}
83 88
             request={getTaIssueApply}
84 89
             params={{ applyType, sourceType: "feedback", duty }}
85 90
             renderItem={(item) => (

+ 3
- 0
src/pages/check/list/index.jsx 查看文件

@@ -9,6 +9,8 @@ import { getTaCheck } from '@/services/tacheck';
9 9
 import Card from './components/Card';
10 10
 
11 11
 const queryParams = {isValid: true};
12
+const sid1 = Math.random().toString(36).substring(2, 8);
13
+
12 14
 export default (props) => {
13 15
 
14 16
   const onClick = (typ, item) => {
@@ -26,6 +28,7 @@ export default (props) => {
26 28
   return (
27 29
     <Page roles={[ROLE_INSPECTOR]}>
28 30
       <PowerList
31
+        sid={sid1}
29 32
         request={getTaCheck}
30 33
         params={queryParams}
31 34
         renderItem={item => (

+ 3
- 0
src/pages/check/loc/list/index.jsx 查看文件

@@ -7,6 +7,8 @@ import PowerList from '@/components/PowerList';
7 7
 import { getTaCheckItem } from '@/services/tacheckitem';
8 8
 import { ROLE_INSPECTOR } from '@/utils/user';
9 9
 
10
+const sid1 = Math.random().toString(36).substring(2, 8);
11
+
10 12
 export default (props) => {
11 13
 
12 14
   const router = Taro.useRouter();
@@ -38,6 +40,7 @@ export default (props) => {
38 40
   return (
39 41
     <Page loading={loading} roles={[ROLE_INSPECTOR]}>
40 42
       <PowerList
43
+        sid={sid1}
41 44
         request={getTaCheckItem}
42 45
         params={params}
43 46
         renderItem={item => {

+ 3
- 0
src/pages/checkStand/list/index.jsx 查看文件

@@ -7,6 +7,8 @@ import PowerList from "@/components/PowerList";
7 7
 import { getTaCheckStand } from "@/services/tacheckstand";
8 8
 import { ROLE_INSPECTOR } from "@/utils/user";
9 9
 
10
+const sid1 = Math.random().toString(36).substring(2, 8);
11
+
10 12
 export default (props) => {
11 13
   const router = Taro.useRouter();
12 14
   const { checkId } = router.params;
@@ -40,6 +42,7 @@ export default (props) => {
40 42
   return (
41 43
     <Page tabBar="check" loading={loading}>
42 44
       <PowerList
45
+        sid={sid1}
43 46
         request={getTaCheckStand}
44 47
         params={params}
45 48
         renderItem={(item) => {

+ 3
- 0
src/pages/feedback/issuelist/index.jsx 查看文件

@@ -7,6 +7,8 @@ import Card from '@/components/IssueCard';
7 7
 import { getTaFeedback } from '@/services/tafeedback';
8 8
 import { getIssueStatus } from '@/utils/biz';
9 9
 
10
+const sid1 = Math.random().toString(36).substring(2, 8);
11
+
10 12
 export default (props) => {
11 13
 
12 14
   const router = Taro.useRouter();
@@ -29,6 +31,7 @@ export default (props) => {
29 31
   return (
30 32
     <Page>
31 33
       <PowerList
34
+        sid={sid1}
32 35
         request={getTaFeedback}
33 36
         params={{bizStatus, isMine: true}}
34 37
         renderItem={(item) => (

+ 13
- 0
src/pages/issue/list/index.jsx 查看文件

@@ -15,6 +15,13 @@ const tabStyle = {
15 15
   overflow: 'hidden',
16 16
 }
17 17
 
18
+const sid1 = Math.random().toString(36).substring(2, 8);
19
+const sid2 = Math.random().toString(36).substring(2, 8);
20
+const sid3 = Math.random().toString(36).substring(2, 8);
21
+const sid4 = Math.random().toString(36).substring(2, 8);
22
+const sid5 = Math.random().toString(36).substring(2, 8);
23
+const sid6 = Math.random().toString(36).substring(2, 8);
24
+
18 25
 // 只有督查员能看到当前页面
19 26
 export default (props) => {
20 27
   // const { user } = useModel('user');
@@ -41,6 +48,7 @@ export default (props) => {
41 48
         <Tab title="全部" style={tabStyle}>
42 49
           {active == 0 && (
43 50
             <PowerList
51
+              sid={sid1}
44 52
               request={getTaIssue}
45 53
               params={{ mine }}
46 54
               renderItem={(item) => (
@@ -52,6 +60,7 @@ export default (props) => {
52 60
         <Tab title="未交办" style={tabStyle}>
53 61
         {active == 1 && (
54 62
           <PowerList
63
+            sid={sid2}
55 64
             request={getTaIssue}
56 65
             params={{ mine, bizStatus: PROCESS_START }}
57 66
             renderItem={(item) => (
@@ -63,6 +72,7 @@ export default (props) => {
63 72
         <Tab title="已交办" style={tabStyle}>
64 73
           {active == 2 && (
65 74
           <PowerList
75
+            sid={sid3}
66 76
             request={getTaIssue}
67 77
             params={{ mine, bizStatus: PROCESS_ASSIGNED }}
68 78
             renderItem={(item) => (
@@ -74,6 +84,7 @@ export default (props) => {
74 84
         <Tab title="已办结" style={tabStyle}>
75 85
           {active == 3 && (
76 86
           <PowerList
87
+            sid={sid4}
77 88
             request={getTaIssue}
78 89
             params={{ mine, bizStatus: PROCESS_END }}
79 90
             renderItem={(item) => (
@@ -85,6 +96,7 @@ export default (props) => {
85 96
         <Tab title="已逾期" style={tabStyle}>
86 97
           {active == 4 && (
87 98
           <PowerList
99
+            sid={sid5}
88 100
             request={getTaIssue}
89 101
             params={{ mine, bizStatus: 'expired' }}
90 102
             renderItem={(item) => (
@@ -96,6 +108,7 @@ export default (props) => {
96 108
         <Tab title="已打回" style={tabStyle}>
97 109
           {active == 5 && (
98 110
           <PowerList
111
+            sid={sid6}
99 112
             request={getTaIssue}
100 113
             params={{ mine, bizStatus: 'reject' }}
101 114
             renderItem={(item) => (

+ 5
- 0
src/pages/issue/list2/index.jsx 查看文件

@@ -16,6 +16,9 @@ const tabStyle = {
16 16
   overflow: "hidden",
17 17
 };
18 18
 
19
+const sid1 = Math.random().toString(36).substring(2, 8);
20
+const sid2 = Math.random().toString(36).substring(2, 8);
21
+
19 22
 export default (props) => {
20 23
   const router = Taro.useRouter();
21 24
   const { title, bizStatus = "", mine = "" } = router.params;
@@ -51,6 +54,7 @@ export default (props) => {
51 54
       <Tabs sticky>
52 55
         <Tab title="督查员" style={tabStyle}>
53 56
           <PowerList
57
+            sid={sid1}
54 58
             request={getTaIssue}
55 59
             params={{ bizStatus, mine, sourceType: "inspector", issueId }}
56 60
             renderItem={(item) => (
@@ -64,6 +68,7 @@ export default (props) => {
64 68
         </Tab>
65 69
         <Tab title="市民" style={tabStyle}>
66 70
           <PowerList
71
+            sid={sid2}
67 72
             request={getTaIssue}
68 73
             params={{ bizStatus, mine, sourceType: "feedback" }}
69 74
             renderItem={(item) => (

+ 4
- 0
src/pages/message/list/index.jsx 查看文件

@@ -7,12 +7,16 @@ import { getTaMessage } from '@/services/tamessage';
7 7
 import { useModel } from '@/store';
8 8
 import Card from './components/Card';
9 9
 
10
+const sid1 = Math.random().toString(36).substring(2, 8);
11
+// const sid2 = Math.random().toString(36).substring(2, 8);
12
+
10 13
 export default (props) => {
11 14
   const { duty } = useModel('user');
12 15
   
13 16
   return (
14 17
     <Page>
15 18
       <PowerList
19
+        sid={sid1}
16 20
         request={getTaMessage}
17 21
         renderItem={(item) => (
18 22
           <Card key={item.msgId} item={item} duty={duty} />

+ 4
- 0
src/pages/notice/index.jsx 查看文件

@@ -4,6 +4,9 @@ import PowerList from '@/components/PowerList';
4 4
 import { getTaNotice } from '@/services/tanotice';
5 5
 import Card from './components/Card/index';
6 6
 
7
+const sid1 = Math.random().toString(36).substring(2, 8);
8
+// const sid2 = Math.random().toString(36).substring(2, 8);
9
+
7 10
 export default (props) => {
8 11
 
9 12
   const [loading, setLoading] = React.useState(false);
@@ -11,6 +14,7 @@ export default (props) => {
11 14
   return (
12 15
     <Page tabBar="notice">
13 16
       <PowerList
17
+        sid={sid1}
14 18
         request={getTaNotice}
15 19
         params={{status: 1}}
16 20
         renderItem={(item) => (

+ 5
- 0
src/pages/org/issue/list/index.jsx 查看文件

@@ -15,6 +15,9 @@ const tabStyle = {
15 15
   overflow: "hidden",
16 16
 };
17 17
 
18
+const sid1 = Math.random().toString(36).substring(2, 8);
19
+const sid2 = Math.random().toString(36).substring(2, 8);
20
+
18 21
 export default (props) => {
19 22
   const router = Taro.useRouter();
20 23
   const { title, bizStatus, color } = router.params;
@@ -55,6 +58,7 @@ export default (props) => {
55 58
       <Tabs sticky>
56 59
         <Tab title="督查员" style={tabStyle}>
57 60
           <PowerList
61
+            sid={sid1}
58 62
             request={getTaOrgIssue}
59 63
             params={{ bizStatus, sourceType: "inspector" ,issueId}}
60 64
             renderItem={(item) => (
@@ -70,6 +74,7 @@ export default (props) => {
70 74
         </Tab>
71 75
         <Tab title="市民" style={tabStyle}>
72 76
           <PowerList
77
+            sid={sid2}
73 78
             request={getTaOrgIssue}
74 79
             params={{ bizStatus, sourceType: "feedback" }}
75 80
             renderItem={(item) => (

+ 4
- 0
src/pages/user/list/index.jsx 查看文件

@@ -8,6 +8,9 @@ import PowerList from "@/components/PowerList";
8 8
 import { getSysUser, deleteSysUser } from "@/services/sysuser";
9 9
 import styles from "./index.module.less";
10 10
 
11
+const sid1 = Math.random().toString(36).substring(2, 8);
12
+// const sid2 = Math.random().toString(36).substring(2, 8);
13
+
11 14
 export default (props) => {
12 15
   const [loading, setLoading] = React.useState(false);
13 16
   const listRef = React.useRef();
@@ -52,6 +55,7 @@ export default (props) => {
52 55
       <view className={styles["user-warpper"]}>
53 56
         <view className={styles["user-warpper-list"]}>
54 57
           <PowerList
58
+            sid={sid1}
55 59
             ref={listRef}
56 60
             request={getSysUser}
57 61
             onLoadingChange={setLoading}

+ 19
- 0
src/utils/tools.js 查看文件

@@ -0,0 +1,19 @@
1
+
2
+// @fn 是对应请求数据
3
+// @ms 是用户多次触发事件的时间间隔 是一个毫秒数
4
+export function throttle(fn, ms = 800){
5
+  let flag = true
6
+
7
+  return (...args) => {
8
+    if(!flag) return;
9
+
10
+    flag = false;
11
+    
12
+    const t = setTimeout(()=>{
13
+      clearTimeout(t);
14
+      flag = true
15
+    }, ms);
16
+
17
+    return fn(...args);
18
+  }
19
+}