修改app扫码问题

master
denghaohaoya 2026-04-02 22:45:01 +08:00
parent 2f98ab970f
commit c401d817d5
1 changed files with 40 additions and 2 deletions

View File

@ -155,6 +155,37 @@
onLoad() { onLoad() {
this.storeId = getStoreId() 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: { methods: {
/* ========== 通用:处理识别结果 ========== */ /* ========== 通用:处理识别结果 ========== */
async handleScanResult(type, result) { async handleScanResult(type, result) {
@ -386,7 +417,8 @@
width: rect.width + 'px', width: rect.width + 'px',
height: rect.height + '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', frameColor: '#FFFFFF',
scanbarColor: '#FFFFFF' scanbarColor: '#FFFFFF'
}) })
@ -398,7 +430,13 @@
this.handleScanResult(type, result) this.handleScanResult(type, result)
} }
// 启动识别 // 启动识别
barcode.start() if (!this.paused) {
barcode.start()
}
// 如果手电筒原本是开启的,恢复状态
if (this.flashOn && typeof barcode.setFlash === 'function') {
barcode.setFlash(true)
}
this.barcodeView = barcode this.barcodeView = barcode
}) })
.exec() .exec()