| | |
| | | |
| | | // #ifdef APP-PLUS |
| | | // 全局初始化RFID模块(应用启动时初始化,关闭app时关闭) |
| | | console.log('[App] 初始化RFID模块...'); |
| | | const uhfModel = uni.requireNativePlugin("iData-UHFPlugin-UHFModule"); |
| | | const globalEvent = uni.requireNativePlugin('globalEvent'); |
| | | const modal = uni.requireNativePlugin('modal'); |
| | | // 注意:RFID初始化采用异步方式,不阻塞页面加载 |
| | | // 即使RFID初始化失败,也不影响页面正常使用 |
| | | console.log('[App] 开始异步初始化RFID模块(不阻塞页面加载)...'); |
| | | |
| | | if (!uhfModel) { |
| | | console.error('[App] RFID插件未找到'); |
| | | } else { |
| | | // 监听上电事件 |
| | | let isPowerOn = false; |
| | | globalEvent.addEventListener('POWEREvent', function(e) { |
| | | console.log('[App] POWEREvent:', JSON.stringify(e)); |
| | | if (e && e.status === 'connected') { |
| | | isPowerOn = true; |
| | | console.log('[App] ✓✓✓ RFID模块已上电成功!'); |
| | | } else { |
| | | isPowerOn = false; |
| | | console.log('[App] ✗ RFID模块未上电'); |
| | | // 使用setTimeout确保不阻塞主线程 |
| | | setTimeout(() => { |
| | | try { |
| | | const uhfModel = uni.requireNativePlugin("iData-UHFPlugin-UHFModule"); |
| | | const globalEvent = uni.requireNativePlugin('globalEvent'); |
| | | const modal = uni.requireNativePlugin('modal'); |
| | | |
| | | if (!uhfModel) { |
| | | console.warn('[App] ⚠ RFID插件未找到,页面可正常使用(RFID功能不可用)'); |
| | | return; |
| | | } |
| | | }); |
| | | |
| | | // 初始化RFID模块(使用SLR模块,与测试页面一致) |
| | | uhfModel.initUHF(1, (ret) => { |
| | | console.log('[App] initUHF result:', ret); |
| | | if (ret && typeof ret === 'string' && ret.includes('success')) { |
| | | console.log('[App] ✓ RFID模块初始化成功'); |
| | | } else { |
| | | console.warn('[App] ⚠ RFID模块初始化失败:', ret); |
| | | |
| | | // 监听上电事件 |
| | | let isPowerOn = false; |
| | | const powerEventHandler = function(e) { |
| | | console.log('[App] POWEREvent:', JSON.stringify(e)); |
| | | if (e && e.status === 'connected') { |
| | | isPowerOn = true; |
| | | console.log('[App] ✓✓✓ RFID模块已上电成功!'); |
| | | } else { |
| | | isPowerOn = false; |
| | | console.log('[App] ✗ RFID模块未上电'); |
| | | } |
| | | }; |
| | | globalEvent.addEventListener('POWEREvent', powerEventHandler); |
| | | |
| | | // 保存事件处理器引用,用于清理 |
| | | this.powerEventHandler = powerEventHandler; |
| | | |
| | | // 设置超时机制:如果10秒内没有初始化成功,允许页面继续使用 |
| | | const initTimeout = setTimeout(() => { |
| | | console.warn('[App] ⚠ RFID模块初始化超时(10秒),页面可正常使用(RFID功能可能不可用)'); |
| | | if (modal) { |
| | | modal.toast({ |
| | | message: 'RFID初始化超时,页面可正常使用', |
| | | duration: 2 |
| | | }); |
| | | } |
| | | }, 10000); // 10秒超时 |
| | | |
| | | // 初始化RFID模块(使用SLR模块,与测试页面一致) |
| | | try { |
| | | uhfModel.initUHF(1, (ret) => { |
| | | clearTimeout(initTimeout); // 清除超时定时器 |
| | | console.log('[App] initUHF result:', ret); |
| | | if (ret && typeof ret === 'string' && ret.includes('success')) { |
| | | console.log('[App] ✓ RFID模块初始化成功'); |
| | | } else { |
| | | console.warn('[App] ⚠ RFID模块初始化失败:', ret); |
| | | if (modal) { |
| | | modal.toast({ |
| | | message: 'RFID初始化失败,页面可正常使用', |
| | | duration: 2 |
| | | }); |
| | | } |
| | | } |
| | | }); |
| | | } catch (initError) { |
| | | clearTimeout(initTimeout); // 清除超时定时器 |
| | | console.error('[App] ⚠ RFID模块初始化异常:', initError); |
| | | console.warn('[App] ⚠ 页面可正常使用(RFID功能不可用)'); |
| | | if (modal) { |
| | | modal.toast({ |
| | | message: 'RFID初始化异常,页面可正常使用', |
| | | duration: 2 |
| | | }); |
| | | } |
| | | } |
| | | }); |
| | | |
| | | // 保存引用,用于onExit时关闭 |
| | | this.uhfModel = uhfModel; |
| | | this.globalEvent = globalEvent; |
| | | } |
| | | |
| | | // 保存引用,用于onExit时关闭 |
| | | this.uhfModel = uhfModel; |
| | | this.globalEvent = globalEvent; |
| | | } catch (error) { |
| | | console.error('[App] ⚠ RFID模块初始化过程出错:', error); |
| | | console.warn('[App] ⚠ 页面可正常使用(RFID功能不可用)'); |
| | | } |
| | | }, 100); // 延迟100ms执行,确保不阻塞页面加载 |
| | | |
| | | // 启动全局RFID输入助手(用于自动填入输入框) |
| | | startRFIDInputHelper(); |
| | | // 使用try-catch确保即使RFID助手启动失败也不影响页面 |
| | | try { |
| | | startRFIDInputHelper(); |
| | | } catch (error) { |
| | | console.error('[App] ⚠ RFID输入助手启动失败:', error); |
| | | console.warn('[App] ⚠ 页面可正常使用(RFID扫描功能不可用)'); |
| | | } |
| | | // #endif |
| | | |
| | | // 注意:以下代码已废弃,改为上面的全局初始化方式 |
| | |
| | | onExit: function() { |
| | | console.log('App Exit') |
| | | // #ifdef APP-PLUS |
| | | // 停止全局RFID输入助手 |
| | | stopRFIDInputHelper(); |
| | | |
| | | // 关闭RFID模块 |
| | | if (this.uhfModel && this.uhfModel.closeUHF) { |
| | | this.uhfModel.closeUHF(); |
| | | console.log('[App] ✓ RFID模块已关闭'); |
| | | // 停止全局RFID输入助手(使用try-catch确保即使失败也不影响退出) |
| | | try { |
| | | stopRFIDInputHelper(); |
| | | } catch (e) { |
| | | console.warn('[App] ⚠ 停止RFID输入助手失败:', e); |
| | | } |
| | | if (this.globalEvent) { |
| | | this.globalEvent.removeEventListener('POWEREvent'); |
| | | |
| | | // 关闭RFID模块(使用try-catch确保即使失败也不影响退出) |
| | | try { |
| | | if (this.uhfModel && this.uhfModel.closeUHF) { |
| | | this.uhfModel.closeUHF(); |
| | | console.log('[App] ✓ RFID模块已关闭'); |
| | | } |
| | | } catch (e) { |
| | | console.warn('[App] ⚠ 关闭RFID模块失败:', e); |
| | | } |
| | | |
| | | try { |
| | | if (this.globalEvent && this.powerEventHandler) { |
| | | this.globalEvent.removeEventListener('POWEREvent', this.powerEventHandler); |
| | | } |
| | | } catch (e) { |
| | | console.warn('[App] ⚠ 移除POWEREvent监听失败:', e); |
| | | } |
| | | // #endif |
| | | } |