Przeglądaj źródła

Merge remote-tracking branch 'origin/master'

ZaiZai 9 miesięcy temu
rodzic
commit
f93a735b01
1 zmienionych plików z 129 dodań i 0 usunięć
  1. 129 0
      pages/index/index - 副本.vue

+ 129 - 0
pages/index/index - 副本.vue

@@ -0,0 +1,129 @@
+<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>