Blame view

src/routes/layout.tsx 1.69 KB
a7a82dbf   张志伟   🚚 chore(*): 更新依赖
1
2
  import { useCallback, useEffect, useLayoutEffect, useRef } from 'react';
  import { Outlet, useNavigate } from '@modern-js/runtime/router';
5d134410   张志伟   🐱 feat(*): init
3
  import dayjs from 'dayjs';
a7a82dbf   张志伟   🚚 chore(*): 更新依赖
4
5
  import { ONE_SECOND, helper, localdb } from '@feewee/h5app-common';
  import { getServerTime } from './service';
5d134410   张志伟   🐱 feat(*): init
6
7
8
9
  import 'dayjs/locale/zh-cn';
  import '@/style/global.scss';
  
  dayjs.locale('zh-cn');
36821dd7   张志伟   feat: init
10
  
7217d10e   张志伟   🐹 feat(*): 更新脚手架
11
12
13
14
15
16
17
18
19
20
  (() => {
    const { error } = console;
    console.error = (...args: any) => {
      if (/defaultProps/.test(args[0])) {
        return;
      }
      error(...args);
    };
  })();
  
36821dd7   张志伟   feat: init
21
  export default function Layout() {
a7a82dbf   张志伟   🚚 chore(*): 更新依赖
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    localdb.init('fw_h5_template', 'fw_h5_template', 'fw_h5_template_del');
    const timer = useRef<any>();
    const navigate = useNavigate();
  
    const saveServerTimeDif = useCallback(async () => {
      const idt = (await localdb.query<number>('i_dt')) || 0;
      if (Math.abs(idt - Date.now()) < 1000 * 60 * 10) {
        return;
      }
      const ser_time = await getServerTime();
      localdb.save<number>(process.env.MODERN_CALKEY ?? 'i_dif', ser_time ? ser_time - Date.now() : 0);
      localdb.save<number>('i_dt', Date.now());
    }, []);
  
    const lisFn = useCallback((e: any) => {
      if ('hardwareBackPress' === e.data) {
        helper.checkBack(() => navigate(-1));
      }
    }, []);
  
    useLayoutEffect(() => {
      localdb.clearExpireData();
      saveServerTimeDif();
  
      timer.current = setInterval(() => localdb.clearExpireData(), ONE_SECOND * 5);
      return () => {
        if (timer.current) {
          clearInterval(timer.current);
        }
      };
    }, [saveServerTimeDif]);
  
    useEffect(() => {
      window.document.addEventListener('message', lisFn);
  
      return () => {
        window.document.removeEventListener('message', lisFn);
      };
    }, []);
  
    return <Outlet />;
36821dd7   张志伟   feat: init
63
  }