65 lines
1.2 KiB
Vue
65 lines
1.2 KiB
Vue
<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>
|