zjk
2026-09-16 d41fd9347b2ec9a40286a7921f4909ac0b0f4ecc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<script>
import { initDB } from './utils/db';
import { startSyncQueue, runDailyCleanup } from './utils/syncEngine';
import { HardwareAPI } from './utils/visitorDevice';
import { getDeviceActivationStorage, hasActivatedDevice } from './utils/request';
 
export default {
    onLaunch() {
        console.log('App Launch');
 
        HardwareAPI.init().then(async () => {
            console.log('硬件插件全局初始化完成');
            await this.restoreOfflineActivation();
 
            // 全局开启扫码和读卡监听,避免多线程干扰
            HardwareAPI.startScan((res) => {
                uni.$emit('Hardware-Scan', res);
            });
            HardwareAPI.startReadCard((res) => {
                uni.$emit('Hardware-ReadCard', res);
            });
        }).catch((err) => {
            console.error('硬件插件全局初始化失败:', err);
        });
 
        initDB().then(() => {
            console.log('数据库初始化完毕');
            startSyncQueue();
        }).catch((err) => {
            console.error('数据库初始化失败:', err);
        });
 
        uni.onNetworkStatusChange((res) => {
            if (res.isConnected && res.networkType !== 'none') {
                console.log('[App] 监听到网络恢复,触发同步队列');
                startSyncQueue();
            }
        });
 
        runDailyCleanup();
 
        // setInterval(() => {
        //     startSyncQueue();
        // }, 5 * 60 * 1000);
    },
    onShow() {
        console.log('App Show');
    },
    onHide() {
        console.log('App Hide');
        // 扫码硬件只在应用退到后台/关闭时统一停止,页面切换过程中不主动 stop
        // HardwareAPI.stopScan();
    },
    methods: {
        async restoreOfflineActivation() {
            // 已激活设备在每次 App 启动时都补调一次离线激活,确保原生插件状态恢复
            if (!hasActivatedDevice()) {
                return;
            }
 
            const { deviceFingerprint } = getDeviceActivationStorage();
            if (!deviceFingerprint) {
                return;
            }
 
            try {
                await HardwareAPI.offlineActivateDevice({
                    code: deviceFingerprint,
                });
                console.log('已激活设备启动时离线激活成功');
            } catch (error) {
                console.error('已激活设备启动时离线激活失败:', error);
            }
        }
    },
};
</script>
 
<style>
page {
    width: 100%;
    height: 100%;
    font-family: Inter, "PingFang SC", "Microsoft YaHei", sans-serif;
    background: #f4f7fc;
}
 
view,
text {
    box-sizing: border-box;
}
</style>