Files
2026-06-02 15:28:25 +08:00

65 lines
1.2 KiB
Vue
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<view>
<view class="uni-section">
<text>扫描测试统一扫码模块</text>
</view>
<view class="uni-form-item uni-column">
<button class="btn btn-primary" @click="startScan">开始扫描</button>
<button class="btn" @click="stopScan">停止扫描</button>
</view>
<view v-if="scanResult">
<text>最近扫描结果: {{ scanResult }}</text>
</view>
</view>
</template>
<script>
import scan from '@/utils/scan.js'
export default {
data() {
return {
scanResult: ''
}
},
onLoad() {
// 初始化(默认自动检测模式,也可手动指定)
// scan.setMode('idata') // 强制 iData 模式
scan.setMode('android') // 强制 Android 系统模式
scan.init()
// 注册扫码结果回调
scan.register(this.onScanResult)
},
onUnload() {
scan.unregister()
},
methods: {
/**
* 扫码结果回调
* @param {string} codeStr - 解码后的扫描字符串
*/
onScanResult(codeStr) {
console.log('codeStr:', codeStr)
this.scanResult = codeStr
uni.showToast({
title: codeStr,
duration: 2000
})
},
startScan() {
scan.startScan()
},
stopScan() {
scan.stopScan()
}
}
}
</script>
<style>
</style>