Blame view

src/routes/layout.tsx 1.81 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
  import { ONE_SECOND, helper, localdb } from '@feewee/h5app-common';
30de39f3   王强   fix(*): 兼容低版本浏览器
5
  import { StyleProvider } from '@ant-design/cssinjs';
a7a82dbf   张志伟   🚚 chore(*): 更新依赖
6
  import { getServerTime } from './service';
5d134410   张志伟   🐱 feat(*): init
7
8
9
10
  import 'dayjs/locale/zh-cn';
  import '@/style/global.scss';
  
  dayjs.locale('zh-cn');
36821dd7   张志伟   feat: init
11
  
7217d10e   张志伟   🐹 feat(*): 更新脚手架
12
13
14
15
16
17
18
19
20
21
  (() => {
    const { error } = console;
    console.error = (...args: any) => {
      if (/defaultProps/.test(args[0])) {
        return;
      }
      error(...args);
    };
  })();
  
36821dd7   张志伟   feat: init
22
  export default function Layout() {
a7a82dbf   张志伟   🚚 chore(*): 更新依赖
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);
      };
    }, []);
  
30de39f3   王强   fix(*): 兼容低版本浏览器
63
64
65
66
67
    return (
      <StyleProvider hashPriority="high">
        <Outlet />
      </StyleProvider>
    );
36821dd7   张志伟   feat: init
68
  }