123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- <template>
- <view class="content">
- <uni-table border stripe>
- <uni-tr>
- <uni-th width="150" align="center">EPC</uni-th>
- </uni-tr>
- <uni-tr v-for="(item, index) in scanDatas" :key="index">
- <uni-td>{{ item }}</uni-td>
- </uni-tr>
- </uni-table>
- </view>
- </template>
- <script setup>
- import { ref } from "vue";
- import {onLoad, onReady, onUnload} from '@dcloudio/uni-app'
- import {getArrValue} from "js-fast-way";
- // 获取 module
- const rfidModule = uni.requireNativePlugin("DeviceModule_RFID");
- //渲染完成
- onReady(() => {
- // #ifdef APP-PLUS
- rfidModuleInit()
- // #endif
- })
- //按键操作
- const isRfidInit = ref(true)
- const rfidModuleInit = () => {
- isRfidInit.value = true
- uni.showLoading({
- title: 'RFID模块加载中...',
- mask: true,
- });
- /**
- 手机按键监听事件,可在此编写一些逻辑,如使用按键触发扫描,更多详细信息请查阅uni官方文档
- 需要注意:退出界面必须移除监听,否则再进入页面重复注册监听会出现多次触发、回调失效的问题
- */
- plus.key.addEventListener('keydown', keyListener);
- //初始化
- setTimeout(() => {
- // 使用模块前必须先初始化RDIF模块
- let { code } = rfidModule.init();
- if (code === 0) {
- uni.hideLoading();
- } else if (code === -1) {
- uni.hideLoading();
- uni.showToast({
- title: 'RFID模块加载失败',
- icon: 'error',
- duration: 3000
- })
- } else {
- uni.hideLoading();
- }
- isRfidInit.value = false
- }, 400);
- }
- //按键操作
- const keyListener = ({keyCode}) => {
- if (keyCode === 293 || keyCode === 312) {
- if (isScan.value) {
- stopScan()
- } else {
- startScan()
- }
- }
- }
- //开始扫描
- const isScan = ref(false)
- const startScan = () => {
- if (isRfidInit.value) return
- isScan.value = true
- rfidModule.startScan((res) => {
- if (res.code === 0) {
- uni.showToast({
- icon: "success",
- title: '开启扫描成功'
- })
- } else if (res.code === 1) {
- startScanData(res.data)
- }
- })
- }
- //扫描结果处理
- const scanDatas = ref([])
- const startScanData = async (data) => {
- const arr = getArrValue(data)
- let epcs = scanDatas.value
- for (let i = 0; i < arr.length; i++) {
- const epc = arr[i].epc
- if (epcs.indexOf(epc) === -1) {
- epcs.push(epc)
- }
- }
- scanDatas.value = epcs
- }
- //停止扫描
- const stopScan = () => {
- const { code } = rfidModule.stopScan()
- if (code === 0) {
- isScan.value = false
- uni.showToast({
- icon: "success",
- title: '关闭扫描成功'
- })
- } else {
- uni.showToast({
- icon: "error",
- title: res.message
- })
- }
- }
- //页面卸载
- onUnload(()=>{
- // #ifdef APP-PLUS
- plus.key.removeEventListener('keydown', keyListener)
- // 使用完毕必须释放RDIF模块
- rfidModule.free();
- // #endif
- })
- </script>
|