Yansen 2 年前
父节点
当前提交
51437508ff

+ 1
- 0
package.json 查看文件

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

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

58
     return queryData({pageNum : pageNumRef.current});
58
     return queryData({pageNum : pageNumRef.current});
59
   }, [queryData]);
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
   React.useImperativeHandle(ref, () => ({
69
   React.useImperativeHandle(ref, () => ({
66
     refresh,
70
     refresh,
67
     updateData: (dt) => setList(dt),
71
     updateData: (dt) => setList(dt),
68
   }));
72
   }));
69
-
70
-  useShow(() => {
71
-    refresh();
72
-  }, [refresh]);
73
   
73
   
74
   return (
74
   return (
75
     <PowerScrollView
75
     <PowerScrollView

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

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
 import React from "react";
1
 import React from "react";
2
-import { useModel } from '@/store';
2
+import evtBus from '@/utils/event_bus';
3
 
3
 
4
 /**
4
 /**
5
  * 页面显示, 隐藏 hook, 可以在 component 中使用
5
  * 页面显示, 隐藏 hook, 可以在 component 中使用
6
  */
6
  */
7
-export default (fn, deps = []) => {
7
+export default (fn, deps) => {
8
   const fnRef = React.useRef();
8
   const fnRef = React.useRef();
9
   fnRef.current = fn;
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
   React.useEffect(() => {
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
     // eslint-disable-next-line react-hooks/exhaustive-deps
29
     // eslint-disable-next-line react-hooks/exhaustive-deps
21
-  }, [show, ...deps]);
30
+  }, deps);
22
 }
31
 }

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

5
 import { Loading, Notify, Dialog } from '@antmjs/vantui';
5
 import { Loading, Notify, Dialog } from '@antmjs/vantui';
6
 import NavLoading from '@/components/NavLoading';
6
 import NavLoading from '@/components/NavLoading';
7
 import Auth from '@/components/Auth';
7
 import Auth from '@/components/Auth';
8
+import evtBus from '@/utils/event_bus';
8
 import TabBar from './TabBar';
9
 import TabBar from './TabBar';
9
 import laySty from './layout.module.less';
10
 import laySty from './layout.module.less';
10
 
11
 
12
   const { className, style, roles, tabBar = false, loading } = props;
13
   const { className, style, roles, tabBar = false, loading } = props;
13
 
14
 
14
   const { person, user, duty } = useModel('user');
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
   React.useEffect(() => {
22
   React.useEffect(() => {
28
     if (person && !user) {
23
     if (person && !user) {
29
       const currentPage = Taro.getCurrentPages().slice().pop();
24
       const currentPage = Taro.getCurrentPages().slice().pop();
45
     // useDidShow 比 React Hook 要早
40
     // useDidShow 比 React Hook 要早
46
     // 所以使用 nextTick
41
     // 所以使用 nextTick
47
     Taro.nextTick(() => {
42
     Taro.nextTick(() => {
48
-      setPageShow(true);
43
+      evt.dispatchEvent('show');
49
     });
44
     });
50
   });
45
   });
51
 
46
 
52
   Taro.useDidHide(() => {
47
   Taro.useDidHide(() => {
53
     Taro.nextTick(() => {
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
   return (
59
   return (
59
-    <PageContext.Provider value={{ show: pageShow }}>
60
       <View className={laySty['page-wrapper']}>
60
       <View className={laySty['page-wrapper']}>
61
         <Notify id="vanNotify" />
61
         <Notify id="vanNotify" />
62
         <Dialog id="vanDialog" />
62
         <Dialog id="vanDialog" />
85
           tabBar && <TabBar className={laySty['page-tabbar']} active={tabBar} duty={duty} />
85
           tabBar && <TabBar className={laySty['page-tabbar']} active={tabBar} duty={duty} />
86
         }
86
         }
87
       </View>
87
       </View>
88
-    </PageContext.Provider>
89
   )
88
   )
90
 }
89
 }

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

17
     ].filter(Boolean).join(' ');
17
     ].filter(Boolean).join(' ');
18
   }, [duty])
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
   React.useImperativeHandle(ref, () => ({
26
   React.useImperativeHandle(ref, () => ({
27
     refresh: () => {
27
     refresh: () => {

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

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

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

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
   resolved "https://registry.npmmirror.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
2255
   resolved "https://registry.npmmirror.com/@xtuc/long/-/long-4.2.2.tgz#d291c6a4e97989b5c61d9acf396ae4fe133a718d"
2256
   integrity sha512-NuHqBY1PB/D8xU6s/thBgOAiAP7HOYDQ32+BFZILJ8ivkUkAHQnWfn6WhL79Owj1qmUnoN/YPhktdIoucipkAQ==
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
 "@zjxpcyc/react-tiny-store@^2.0.1":
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
 abab@^2.0.3, abab@^2.0.5:
2268
 abab@^2.0.3, abab@^2.0.5:
2264
   version "2.0.6"
2269
   version "2.0.6"