From c401d817d563d732ce2822044901f2d87604542e Mon Sep 17 00:00:00 2001 From: denghaohaoya <1725652099@qq.com> Date: Thu, 2 Apr 2026 22:45:01 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9app=E6=89=AB=E7=A0=81?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package_a/translation/translation.vue | 42 +++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/package_a/translation/translation.vue b/package_a/translation/translation.vue index 17861dd..3e21500 100644 --- a/package_a/translation/translation.vue +++ b/package_a/translation/translation.vue @@ -155,6 +155,37 @@ onLoad() { this.storeId = getStoreId() }, + onShow() { + // 从其他页面返回时,如果未处于暂停状态,重新启动扫码 + this.lastScanValue = '' + this.lastScanAt = 0 + // #ifdef APP-PLUS + if (this._isFirstShow === undefined) { + // 首次进入时由 onReady 初始化,这里跳过 + this._isFirstShow = false + } else { + // 从其他页面返回时 + if (!this.barcodeView) { + // 延迟重建,确保 DOM 已经就绪 + setTimeout(() => { + this.initAppScanner() + }, 200) + } else if (!this.paused) { + try { + this.barcodeView.start && this.barcodeView.start() + } catch (e) {} + } + } + // #endif + }, + onHide() { + // 页面隐藏时暂停扫码,节省性能 + // #ifdef APP-PLUS + // 为避免与其他页面的系统扫码(如 addProduct 的 uni.scanCode)产生布局冲突 + // 页面隐藏时彻底销毁原生组件,onShow 时再重新创建 + this.destroyAppScanner() + // #endif + }, methods: { /* ========== 通用:处理识别结果 ========== */ async handleScanResult(type, result) { @@ -386,7 +417,8 @@ width: rect.width + 'px', height: rect.height + 'px' } - const barcode = plus.barcode.create('scan-barcode', types, { + const barcodeId = 'scan-barcode-' + Date.now() + const barcode = plus.barcode.create(barcodeId, types, { frameColor: '#FFFFFF', scanbarColor: '#FFFFFF' }) @@ -398,7 +430,13 @@ this.handleScanResult(type, result) } // 启动识别 - barcode.start() + if (!this.paused) { + barcode.start() + } + // 如果手电筒原本是开启的,恢复状态 + if (this.flashOn && typeof barcode.setFlash === 'function') { + barcode.setFlash(true) + } this.barcodeView = barcode }) .exec()