Yansen 2 年之前
父節點
當前提交
51437508ff

+ 1
- 0
package.json 查看文件

@@ -47,6 +47,7 @@
47 47
     "@tarojs/shared": "3.5.7",
48 48
     "@tarojs/taro": "3.5.7",
49 49
     "@tarojs/taro-h5": "3.5.7",
50
+    "@zjxpcyc/js_event": "^1.0.0",
50 51
     "@zjxpcyc/react-tiny-store": "^2.0.1",
51 52
     "echarts": "5.4.1",
52 53
     "echarts-for-weixin": "^1.0.2",

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

@@ -58,18 +58,18 @@ export default React.forwardRef((props, ref) => {
58 58
     return queryData({pageNum : pageNumRef.current});
59 59
   }, [queryData]);
60 60
 
61
-  // React.useEffect(() => {
62
-  //   refresh();
63
-  // }, [refresh]);
61
+  useShow(() => {
62
+    refresh();
63
+  })
64
+
65
+  React.useEffect(() => {
66
+    refresh();
67
+  }, [refresh]);
64 68
 
65 69
   React.useImperativeHandle(ref, () => ({
66 70
     refresh,
67 71
     updateData: (dt) => setList(dt),
68 72
   }));
69
-
70
-  useShow(() => {
71
-    refresh();
72
-  }, [refresh]);
73 73
   
74 74
   return (
75 75
     <PowerScrollView

+ 34
- 0
src/layouts/hooks/useHide.js 查看文件

@@ -0,0 +1,34 @@
1
+import React from "react";
2
+import evtBus from '@/utils/event_bus';
3
+
4
+/**
5
+ * 页面显示, 隐藏 hook, 可以在 component 中使用
6
+ */
7
+export default (fn, deps) => {
8
+  const fnRef = React.useRef();
9
+  fnRef.current = fn;
10
+
11
+  const [evt, func] = React.useMemo(() => {
12
+    const evt1 = evtBus.current();
13
+    const func1 = () => fnRef.current();
14
+
15
+    evt1.addEventListener('hide', func1);
16
+    evt1.addEventListener('unload', func1);
17
+
18
+    return [
19
+      evt1,
20
+      func1,
21
+    ];
22
+  }, []);
23
+
24
+  React.useEffect(() => {
25
+    evt.addEventListener('hide', func);
26
+    evt.addEventListener('unload', func);
27
+
28
+    return () => {
29
+      evt.removeEventListener('hide', func);
30
+      evt.removeEventListener('unload', func);
31
+    };
32
+    // eslint-disable-next-line react-hooks/exhaustive-deps
33
+  }, deps);
34
+}

+ 19
- 10
src/layouts/hooks/useShow.js 查看文件

@@ -1,22 +1,31 @@
1 1
 import React from "react";
2
-import { useModel } from '@/store';
2
+import evtBus from '@/utils/event_bus';
3 3
 
4 4
 /**
5 5
  * 页面显示, 隐藏 hook, 可以在 component 中使用
6 6
  */
7
-export default (fn, deps = []) => {
7
+export default (fn, deps) => {
8 8
   const fnRef = React.useRef();
9 9
   fnRef.current = fn;
10 10
 
11
-  const { getPageCtx } = useModel('page');
12
-  // eslint-disable-next-line react-hooks/exhaustive-deps
13
-  const ctx = React.useMemo(getPageCtx, []);
14
-  const { show } = React.useContext(ctx);
11
+  const [evt, func] = React.useMemo(() => {
12
+    const evt1 = evtBus.current();
13
+    const func1 = () => console.log('--show-->') || fnRef.current();
14
+
15
+    evt1.addEventListener('show', func1);
16
+
17
+    return [
18
+      evt1,
19
+      func1,
20
+    ];
21
+  }, []);
15 22
 
16 23
   React.useEffect(() => {
17
-    if (show) {
18
-      fnRef.current();
19
-    }
24
+    evt.addEventListener('show', func);
25
+
26
+    return () => {
27
+      evt.removeEventListener('show', func);
28
+    };
20 29
     // eslint-disable-next-line react-hooks/exhaustive-deps
21
-  }, [show, ...deps]);
30
+  }, deps);
22 31
 }

+ 12
- 13
src/layouts/index.jsx 查看文件

@@ -5,6 +5,7 @@ import { useModel } from '@/store';
5 5
 import { Loading, Notify, Dialog } from '@antmjs/vantui';
6 6
 import NavLoading from '@/components/NavLoading';
7 7
 import Auth from '@/components/Auth';
8
+import evtBus from '@/utils/event_bus';
8 9
 import TabBar from './TabBar';
9 10
 import laySty from './layout.module.less';
10 11
 
@@ -12,18 +13,12 @@ export default (props) => {
12 13
   const { className, style, roles, tabBar = false, loading } = props;
13 14
 
14 15
   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({show: false});
19
-    setPageCtx(ctx);
16
+  const containerClass = `${laySty['page-conatiner']} ${tabBar ? laySty['with-tabbar'] : ''} ${className}`;
20 17
 
21
-    return ctx;
22
-  // eslint-disable-next-line react-hooks/exhaustive-deps
18
+  const evt = React.useMemo(() => {
19
+    return evtBus.current();
23 20
   }, []);
24 21
 
25
-  const containerClass = `${laySty['page-conatiner']} ${tabBar ? laySty['with-tabbar'] : ''} ${className}`;
26
-
27 22
   React.useEffect(() => {
28 23
     if (person && !user) {
29 24
       const currentPage = Taro.getCurrentPages().slice().pop();
@@ -45,18 +40,23 @@ export default (props) => {
45 40
     // useDidShow 比 React Hook 要早
46 41
     // 所以使用 nextTick
47 42
     Taro.nextTick(() => {
48
-      setPageShow(true);
43
+      evt.dispatchEvent('show');
49 44
     });
50 45
   });
51 46
 
52 47
   Taro.useDidHide(() => {
53 48
     Taro.nextTick(() => {
54
-      setPageShow(false);
49
+      evt.dispatchEvent('hide');
50
+    });
51
+  })
52
+
53
+  Taro.useUnload(() => {
54
+    Taro.nextTick(() => {
55
+      evt.dispatchEvent('unload');
55 56
     });
56 57
   })
57 58
 
58 59
   return (
59
-    <PageContext.Provider value={{ show: pageShow }}>
60 60
       <View className={laySty['page-wrapper']}>
61 61
         <Notify id="vanNotify" />
62 62
         <Dialog id="vanDialog" />
@@ -85,6 +85,5 @@ export default (props) => {
85 85
           tabBar && <TabBar className={laySty['page-tabbar']} active={tabBar} duty={duty} />
86 86
         }
87 87
       </View>
88
-    </PageContext.Provider>
89 88
   )
90 89
 }

+ 5
- 5
src/pages/home/components/StatCard.jsx 查看文件

@@ -17,11 +17,11 @@ export default React.forwardRef((props, ref) => {
17 17
     ].filter(Boolean).join(' ');
18 18
   }, [duty])
19 19
 
20
-  // React.useEffect(() => {
21
-  //   if (duty) {
22
-  //     getIndexData(duty).then(setList);
23
-  //   }
24
-  // }, [duty]);
20
+  React.useEffect(() => {
21
+    if (duty) {
22
+      getIndexData(duty).then(setList);
23
+    }
24
+  }, [duty]);
25 25
 
26 26
   React.useImperativeHandle(ref, () => ({
27 27
     refresh: () => {

+ 27
- 8
src/pages/issue/list/index.jsx 查看文件

@@ -22,25 +22,35 @@ export default (props) => {
22 22
   const router = Taro.useRouter();
23 23
   const { mine = '' } = router.params;
24 24
 
25
+  const [active, setActive] = React.useState(0);
26
+
25 27
   const onClick = (item) => {
26 28
     Taro.navigateTo({
27 29
       url: `/pages/issue/edit/index?id=${item.issueId}`
28 30
     })
29 31
   }
30 32
 
33
+  const onTabChange = (e) => {
34
+    const { index } = e.detail;
35
+    setActive(index)
36
+  }
37
+
31 38
   return (
32 39
     <Page roles={[ROLE_INSPECTOR]}>
33
-      <Tabs sticky>
40
+      <Tabs sticky onChange={onTabChange}>
34 41
         <Tab title="全部" style={tabStyle}>
35
-          <PowerList
36
-            request={getTaIssue}
37
-            params={{ mine }}
38
-            renderItem={(item) => (
39
-              <Card key={item.issueId} issue={item} onClick={() => onClick(item)} />
40
-            )}
41
-          />
42
+          {active == 0 && (
43
+            <PowerList
44
+              request={getTaIssue}
45
+              params={{ mine }}
46
+              renderItem={(item) => (
47
+                <Card key={item.issueId} issue={item} onClick={() => onClick(item)} />
48
+              )}
49
+            />
50
+          )}
42 51
         </Tab>
43 52
         <Tab title="未交办" style={tabStyle}>
53
+        {active == 1 && (
44 54
           <PowerList
45 55
             request={getTaIssue}
46 56
             params={{ mine, bizStatus: PROCESS_START }}
@@ -48,8 +58,10 @@ export default (props) => {
48 58
               <Card key={item.issueId} issue={item} onClick={() => onClick(item)} />
49 59
             )}
50 60
           />
61
+          )}
51 62
         </Tab>
52 63
         <Tab title="已交办" style={tabStyle}>
64
+          {active == 2 && (
53 65
           <PowerList
54 66
             request={getTaIssue}
55 67
             params={{ mine, bizStatus: PROCESS_ASSIGNED }}
@@ -57,8 +69,10 @@ export default (props) => {
57 69
               <Card key={item.issueId} issue={item} onClick={() => onClick(item)} />
58 70
             )}
59 71
           />
72
+          )}
60 73
         </Tab>
61 74
         <Tab title="已办结" style={tabStyle}>
75
+          {active == 3 && (
62 76
           <PowerList
63 77
             request={getTaIssue}
64 78
             params={{ mine, bizStatus: PROCESS_END }}
@@ -66,8 +80,10 @@ export default (props) => {
66 80
               <Card key={item.issueId} issue={item} onClick={() => onClick(item)} />
67 81
             )}
68 82
           />
83
+          )}
69 84
         </Tab>
70 85
         <Tab title="已逾期" style={tabStyle}>
86
+          {active == 4 && (
71 87
           <PowerList
72 88
             request={getTaIssue}
73 89
             params={{ mine, bizStatus: 'expired' }}
@@ -75,8 +91,10 @@ export default (props) => {
75 91
               <Card key={item.issueId} issue={item} onClick={() => onClick(item)} />
76 92
             )}
77 93
           />
94
+          )}
78 95
         </Tab>
79 96
         <Tab title="已打回" style={tabStyle}>
97
+          {active == 5 && (
80 98
           <PowerList
81 99
             request={getTaIssue}
82 100
             params={{ mine, bizStatus: 'reject' }}
@@ -84,6 +102,7 @@ export default (props) => {
84 102
               <Card key={item.issueId} issue={item} onClick={() => onClick(item)} />
85 103
             )}
86 104
           />
105
+          )}
87 106
         </Tab>
88 107
       </Tabs>
89 108
     </Page>

+ 38
- 0
src/utils/event_bus.js 查看文件

@@ -0,0 +1,38 @@
1
+import Taro from '@tarojs/taro';
2
+import createEvent from "@zjxpcyc/js_event";
3
+
4
+function evtBus() {
5
+  const bus = {};
6
+
7
+  const currentPath = () => {
8
+    const currentPage = Taro.getCurrentPages().slice().pop();
9
+    return currentPage.route;
10
+  }
11
+
12
+  // 没有则新建一个
13
+  const current = () => {
14
+    const path = currentPath();
15
+    const target = bus[path];
16
+    
17
+    if (target) {
18
+      return target;
19
+    }
20
+
21
+    const evt = createEvent();
22
+    bus[path] = evt;
23
+    return evt;
24
+  }
25
+
26
+  const remove = () => {
27
+    const path = currentPath();
28
+    delete bus[path];
29
+  }
30
+
31
+  return {
32
+    remove,
33
+    current,
34
+  }
35
+}
36
+
37
+const bus = evtBus();
38
+export default bus;

+ 8
- 3
yarn.lock 查看文件

@@ -2255,10 +2255,15 @@
2255 2255
   resolved "https://registry.npmmirror.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
2256 2256
   integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
2257 2257
 
2258
+"@zjxpcyc/js_event@^1.0.0":
2259
+  version "1.0.0"
2260
+  resolved "https://registry.npmmirror.com/@zjxpcyc/js_event/-/js_event-1.0.0.tgz#25688d9ac263700a8ed8d702c1517bcd827934d1"
2261
+  integrity sha512-Nz9bHp0DcdWbgNQQ54uFXoAONvOcrzzqbNUxMp6GTOWP4y14jOZowsLwb1mYNcVxBlkar4Ds0WTIoRyx8sOpRw==
2262
+
2258 2263
 "@zjxpcyc/react-tiny-store@^2.0.1":
2259
-  version "2.0.1"
2260
-  resolved "https://registry.npmmirror.com/@zjxpcyc/react-tiny-store/-/react-tiny-store-2.0.1.tgz#a83813d3ebd90050eeb0238af16fa355c034ab40"
2261
-  integrity sha512-+MTTcQ9aU8OMl7YrmX8UVnM5qUDktdHl0wpxmI5dVIwZxvHsKRKZuQn+ExOeCvJAVC2l+ubS7afT9rzTHPuPrA==
2264
+  version "2.0.3"
2265
+  resolved "https://registry.npmmirror.com/@zjxpcyc/react-tiny-store/-/react-tiny-store-2.0.3.tgz#99cc712c2bc1cecc2b5bdaf2bb15cf3a60a4fbbe"
2266
+  integrity sha512-YL0s2wlxfF3QrGy4J7PlU2HUtVUOZMMUeTWx2gEKoEtKjLVtRxq4+8gGZA4K4ZYvbAyZ9PHqc1hi8gv7gQ8k5Q==
2262 2267
 
2263 2268
 abab@^2.0.3, abab@^2.0.5:
2264 2269
   version "2.0.6"