/** * iData 扫码功能 Mixin(APP + 小程序双端通用) * 提供可复用的跨端扫码能力 */ // #ifdef APP-PLUS import scan from '@/utils/scan.js' // #endif export default { data() { return { scanStatus: '' } }, // #ifdef APP-PLUS onLoad() { this.initScanner() }, onUnload() { this.cleanupScanner() }, // #endif methods: { // #ifdef APP-PLUS /** * APP端:初始化扫码模块 */ initScanner() { try { const mode = (this.$store && this.$store.state && this.$store.state.scan && this.$store.state.scan.scanMode) || 'idata' scan.setMode(mode) scan.init() scan.register((codeStr) => { this.onBarcodeScanned(codeStr) }) } catch (e) { console.error('[BarcodeMixin] 扫码初始化失败:', e) } }, /** * APP端:清理扫码资源 */ cleanupScanner() { scan.unregister() }, /** * APP端扫码:点一次开始扫描,再点一次停止扫描 */ scanCode() { if (scan.isScanning()) { scan.stopScan() } else { scan.startScan() } }, // #endif // #ifdef MP /** * 小程序端扫码:调用 uni 跨平台扫码 API */ scanCode() { uni.scanCode({ success: (res) => { this.onBarcodeScanned(res.result) }, fail: (err) => { console.error('[BarcodeMixin] 小程序扫码失败:', err) if (err.errMsg && err.errMsg.indexOf('cancel') === -1) { uni.showToast({ title: '扫码失败', icon: 'none' }) } } }) }, // #endif /** * 扫码成功后的默认处理(页面可覆盖此方法自定义处理逻辑) * @param {string} barcode - 扫码结果 */ onBarcodeScanned(barcode) { console.log('[BarcodeMixin] 收到条码:', barcode) } } }