<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>
|