Yansen 2 years ago
parent
commit
7df1e5cf1a

+ 2
- 2
package.json View File

@@ -48,11 +48,11 @@
48 48
     "@tarojs/taro": "3.5.7",
49 49
     "@tarojs/taro-h5": "3.5.7",
50 50
     "@zjxpcyc/react-tiny-store": "^2.0.1",
51
+    "echarts": "5.4.1",
51 52
     "echarts-for-weixin": "^1.0.2",
52 53
     "md5": "^2.3.0",
53 54
     "react": "^18.0.0",
54
-    "react-dom": "^18.0.0",
55
-    "echarts": "5.4.1"
55
+    "react-dom": "^18.0.0"
56 56
   },
57 57
   "devDependencies": {
58 58
     "@babel/core": "^7.8.0",

+ 20
- 13
src/components/PowerList/index.jsx View File

@@ -2,9 +2,10 @@ import React from 'react';
2 2
 import Taro from '@tarojs/taro';
3 3
 import { View } from '@tarojs/components';
4 4
 import { PowerScrollView } from '@antmjs/vantui';
5
+import useShow from '@/layouts/hooks/useShow';
5 6
 
6 7
 export default React.forwardRef((props, ref) => {
7
-  const { request, params, renderItem, onLoadingChange } = props;
8
+  const { request, params, renderItem, onLoadingChange, onDataChange } = props;
8 9
 
9 10
   const pageSize = 20;
10 11
   const pageNumRef = React.useRef(1);
@@ -31,11 +32,11 @@ export default React.forwardRef((props, ref) => {
31 32
       }).then((res) => {
32 33
         const { records, current, pages } = res;
33 34
 
34
-        if (current == 1) {
35
-          setList(records || []);
36
-        } else {
37
-          setList(listRef.current.concat(records || []));
35
+        let dataset = current == 1 ? records || [] : listRef.current.concat(records || [])
36
+        if (onDataChange) {
37
+          dataset = onDataChange(dataset)
38 38
         }
39
+        setList(dataset);
39 40
 
40 41
         setFinished(current >= pages);
41 42
         changeLoading(false);
@@ -47,22 +48,28 @@ export default React.forwardRef((props, ref) => {
47 48
     });
48 49
   }, [request, params]);
49 50
 
51
+  const refresh = React.useCallback(() => {
52
+    pageNumRef.current = 1;
53
+    queryData({pageNum : pageNumRef.current});
54
+  }, [queryData]);
55
+
50 56
   const onScrollToLower = React.useCallback((event = 0, isRefresh = false) => {
51 57
     pageNumRef.current += 1;
52 58
     return queryData({pageNum : pageNumRef.current});
53 59
   }, [queryData]);
54 60
 
55
-  React.useEffect(() => {
56
-    pageNumRef.current = 1;
57
-    queryData({pageNum : pageNumRef.current});
58
-  }, [queryData]);
61
+  // React.useEffect(() => {
62
+  //   refresh();
63
+  // }, [refresh]);
59 64
 
60 65
   React.useImperativeHandle(ref, () => ({
61
-    refresh: () => {
62
-      pageNumRef.current = 1;
63
-      queryData({pageNum : pageNumRef.current});
64
-    }
66
+    refresh,
67
+    updateData: (dt) => setList(dt),
65 68
   }));
69
+
70
+  useShow(() => {
71
+    refresh();
72
+  }, [refresh]);
66 73
   
67 74
   return (
68 75
     <PowerScrollView

+ 20
- 0
src/layouts/hooks/useShow.js View File

@@ -0,0 +1,20 @@
1
+import React from "react";
2
+import { useModel } from '@/store';
3
+
4
+/**
5
+ * 页面显示, 隐藏 hook, 可以在 component 中使用
6
+ */
7
+export default (fn, deps = []) => {
8
+  const fnRef = React.useRef();
9
+  fnRef.current = fn;
10
+
11
+  const { getPageCtx } = useModel('page');
12
+  const { show } = React.useContext(getPageCtx());
13
+
14
+  React.useEffect(() => {
15
+    if (show) {
16
+      fnRef.current();
17
+    }
18
+  // eslint-disable-next-line react-hooks/exhaustive-deps
19
+  }, [show, ...deps]);
20
+}

+ 48
- 29
src/layouts/index.jsx View File

@@ -8,12 +8,18 @@ import Auth from '@/components/Auth';
8 8
 import TabBar from './TabBar';
9 9
 import laySty from './layout.module.less';
10 10
 
11
-const f = x => x;
12
-
13 11
 export default (props) => {
14
-  const { className, style, roles, tabBar = false, loading, onShow = f, onHide = f } = props;
12
+  const { className, style, roles, tabBar = false, loading } = props;
15 13
 
16 14
   const { person, user, duty } = useModel('user');
15
+  const { setPageCtx } = useModel('page');
16
+  const [pageShow, setPageShow] = React.useState(false);
17
+  const PageContext = React.useMemo(() => {
18
+    const ctx = React.createContext();
19
+    setPageCtx(ctx);
20
+
21
+    return ctx;
22
+  }, []);
17 23
 
18 24
   const containerClass = `${laySty['page-conatiner']} ${tabBar ? laySty['with-tabbar'] : ''} ${className}`;
19 25
 
@@ -34,37 +40,50 @@ export default (props) => {
34 40
     };
35 41
   });
36 42
 
37
-  Taro.useDidShow(onShow);
38
-  Taro.useDidHide(onHide);
43
+  Taro.useDidShow(() => {
44
+    // useDidShow 比 React Hook 要早
45
+    // 所以使用 nextTick
46
+    Taro.nextTick(() => {
47
+      setPageShow(true);
48
+    });
49
+  });
50
+
51
+  Taro.useDidHide(() => {
52
+    Taro.nextTick(() => {
53
+      setPageShow(false);
54
+    });
55
+  })
39 56
 
40 57
   return (
41
-    <View className={laySty['page-wrapper']}>
42
-      <Notify id="vanNotify" />
43
-      <Dialog id="vanDialog" />
44
-      <NavLoading loading={loading} />
45
-      <View className={containerClass} style={style}>
46
-        {
47
-          !person && (
48
-            <View className={laySty.loading}>
49
-              <Loading size="32px" vertical>
50
-                加载中...
51
-              </Loading>
52
-            </View>
53
-          )
54
-        }
55
-        <Auth roles={roles}>
56
-          {props.children}
57
-        </Auth>
58
+    <PageContext.Provider value={{show: pageShow}}>
59
+      <View className={laySty['page-wrapper']}>
60
+        <Notify id="vanNotify" />
61
+        <Dialog id="vanDialog" />
62
+        <NavLoading loading={loading} />
63
+        <View className={containerClass} style={style}>
64
+          {
65
+            !person && (
66
+              <View className={laySty.loading}>
67
+                <Loading size="32px" vertical>
68
+                  加载中...
69
+                </Loading>
70
+              </View>
71
+            )
72
+          }
73
+          <Auth roles={roles}>
74
+            {props.children}
75
+          </Auth>
58 76
 
77
+          {
78
+            !tabBar && (
79
+              <View className={laySty['pdm-space']}></View>
80
+            )
81
+          }
82
+        </View>
59 83
         {
60
-          !tabBar && (
61
-            <View className={laySty['pdm-space']}></View>
62
-          )
84
+          tabBar && <TabBar className={laySty['page-tabbar']} active={tabBar} duty={duty} />
63 85
         }
64 86
       </View>
65
-      {
66
-        tabBar && <TabBar className={laySty['page-tabbar']} active={tabBar} duty={duty} />
67
-      }
68
-    </View>
87
+    </PageContext.Provider>
69 88
   )
70 89
 }

+ 12
- 5
src/pages/home/components/StatCard.jsx View File

@@ -2,6 +2,7 @@ import React from 'react';
2 2
 import { View } from '@tarojs/components';
3 3
 import { ROLE_CITIZEN } from '@/utils/user';
4 4
 import { getIndexData } from '@/services/stat';
5
+import useShow from '@/layouts/hooks/useShow';
5 6
 import style from './stat-card.module.less';
6 7
 
7 8
 export default React.forwardRef((props, ref) => {
@@ -16,11 +17,11 @@ export default React.forwardRef((props, ref) => {
16 17
     ].filter(Boolean).join(' ');
17 18
   }, [duty])
18 19
 
19
-  React.useEffect(() => {
20
-    if (duty) {
21
-      getIndexData(duty).then(setList);
22
-    }
23
-  }, [duty]);
20
+  // React.useEffect(() => {
21
+  //   if (duty) {
22
+  //     getIndexData(duty).then(setList);
23
+  //   }
24
+  // }, [duty]);
24 25
 
25 26
   React.useImperativeHandle(ref, () => ({
26 27
     refresh: () => {
@@ -30,6 +31,12 @@ export default React.forwardRef((props, ref) => {
30 31
     }
31 32
   }), [duty]);
32 33
 
34
+  useShow(() => {
35
+    if (duty) {
36
+      getIndexData(duty).then(setList);
37
+    }
38
+  }, [duty]);
39
+
33 40
   return (
34 41
     <View className={classNames}>
35 42
       <View className={style['stat-card']}>

+ 2
- 8
src/pages/home/index.jsx View File

@@ -73,8 +73,6 @@ const menus = {
73 73
 }
74 74
 
75 75
 export default (props) => {
76
-
77
-  const statRef = React.useRef();
78 76
   const userModel = useModel('user');
79 77
   const { user, duty, signOut, changePwd } = userModel || {};
80 78
 
@@ -84,19 +82,15 @@ export default (props) => {
84 82
     return menus[duty];
85 83
   }, [duty])
86 84
 
87
-  const onShow = () => {
88
-    statRef.current?.refresh();
89
-  }
90
-
91 85
   return (
92
-    <Page tabBar="home" className="home-page" onShow={onShow}>
86
+    <Page tabBar="home" className="home-page">
93 87
       <Head
94 88
         userModel={userModel}
95 89
         onChangePwd={changePwd}
96 90
         onExit={signOut}
97 91
       />
98 92
       <Banner duty={duty} />
99
-      <StatCard duty={duty} ref={statRef} />
93
+      <StatCard duty={duty} />
100 94
 
101 95
       <View className="menu-icons">
102 96
         {

+ 0
- 1
src/pages/issue/list/index.jsx View File

@@ -7,7 +7,6 @@ import PowerList from '@/components/PowerList';
7 7
 import Card from '@/components/IssueCard';
8 8
 import { getTaIssue } from '@/services/taissue';
9 9
 import { ROLE_INSPECTOR, ROLE_MANAGER } from '@/utils/user';
10
-import { useModel } from '@/store';
11 10
 import { getIssueStatus, PROCESS_ASSIGNED, PROCESS_END, PROCESS_START } from '@/utils/biz';
12 11
 
13 12
 // 只有督查员能看到当前页面

+ 3
- 0
src/store/index.js View File

@@ -1,13 +1,16 @@
1 1
 import { createStore } from '@zjxpcyc/react-tiny-store';
2 2
 import useUser from './user';
3
+import usePage from './page';
3 4
 
4 5
 const models = {
5 6
   'user': useUser,
7
+  'page': usePage,
6 8
 };
7 9
 
8 10
 const store = createStore(models);
9 11
 
10 12
 export default store;
13
+export const getState = store.getState;
11 14
 export const useModel = store.useModel;
12 15
 export const Provider = store.Provider;
13 16
 

+ 17
- 0
src/store/page.js View File

@@ -0,0 +1,17 @@
1
+import React from "react";
2
+
3
+export default () => {
4
+  const pageCtxRef = React.useRef();
5
+
6
+  const setPageCtx = (pg) => {
7
+    pageCtxRef.current = pg;
8
+  }
9
+  const getPageCtx = () => {
10
+    return pageCtxRef.current;
11
+  }
12
+
13
+  return {
14
+    setPageCtx,
15
+    getPageCtx,
16
+  }
17
+}

+ 25
- 0
yarn.lock View File

@@ -5013,6 +5013,19 @@ ecc-jsbn@~0.1.1:
5013 5013
     jsbn "~0.1.0"
5014 5014
     safer-buffer "^2.1.0"
5015 5015
 
5016
+echarts-for-weixin@^1.0.2:
5017
+  version "1.0.2"
5018
+  resolved "https://registry.npmmirror.com/echarts-for-weixin/-/echarts-for-weixin-1.0.2.tgz#f32f46dc40fdc60d9a97637e5694df01b8a060ed"
5019
+  integrity sha512-cEk4yFMCJDQpvAgVRKbGvMjXvuW965iEUootJffRFHwJwCq0/GIeoRTTAEuj50PKMMtLTW4DmBPmvPja3sH2jQ==
5020
+
5021
+echarts@5.4.1:
5022
+  version "5.4.1"
5023
+  resolved "https://registry.npmmirror.com/echarts/-/echarts-5.4.1.tgz#d7f65a584d78beff62568d878b16151b3381811c"
5024
+  integrity sha512-9ltS3M2JB0w2EhcYjCdmtrJ+6haZcW6acBolMGIuf01Hql1yrIV01L1aRj7jsaaIULJslEP9Z3vKlEmnJaWJVQ==
5025
+  dependencies:
5026
+    tslib "2.3.0"
5027
+    zrender "5.4.1"
5028
+
5016 5029
 ee-first@1.1.1:
5017 5030
   version "1.1.1"
5018 5031
   resolved "https://registry.npmmirror.com/ee-first/-/ee-first-1.1.1.tgz#590c61156b0ae2f4f0255732a158b266bc56b21d"
@@ -11938,6 +11951,11 @@ tsconfig-paths@^3.14.1:
11938 11951
     minimist "^1.2.6"
11939 11952
     strip-bom "^3.0.0"
11940 11953
 
11954
+tslib@2.3.0:
11955
+  version "2.3.0"
11956
+  resolved "https://registry.npmmirror.com/tslib/-/tslib-2.3.0.tgz#803b8cdab3e12ba581a4ca41c8839bbb0dacb09e"
11957
+  integrity sha512-N82ooyxVNm6h1riLCoyS9e3fuJ3AMG2zIZs2Gd1ATcSFjSA23Q0fzjjZeh0jbJvWVDZ0cJT8yaNNaaXHzueNjg==
11958
+
11941 11959
 tslib@^1.10.0, tslib@^1.8.1, tslib@^1.9.0:
11942 11960
   version "1.14.1"
11943 11961
   resolved "https://registry.npmmirror.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00"
@@ -12847,3 +12865,10 @@ yocto-queue@^0.1.0:
12847 12865
   version "0.1.0"
12848 12866
   resolved "https://registry.npmmirror.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"
12849 12867
   integrity sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q==
12868
+
12869
+zrender@5.4.1:
12870
+  version "5.4.1"
12871
+  resolved "https://registry.npmmirror.com/zrender/-/zrender-5.4.1.tgz#892f864b885c71e1dc25dcb3c7a4ba42678d3f11"
12872
+  integrity sha512-M4Z05BHWtajY2241EmMPHglDQAJ1UyHQcYsxDNzD9XLSkPDqMq4bB28v9Pb4mvHnVQ0GxyTklZ/69xCFP6RXBA==
12873
+  dependencies:
12874
+    tslib "2.3.0"