ZaiZai 11 mesi fa
parent
commit
a7bbbecda8
2 ha cambiato i file con 43 aggiunte e 18 eliminazioni
  1. 2 2
      pages.json
  2. 41 16
      pages/index/index.vue

+ 2 - 2
pages.json

@@ -1,9 +1,9 @@
 {
     "pages": [
-        { "path": "pages/index/index" }
+        {"path": "pages/index/index"}
     ],
     "globalStyle": {
-        "navigationStyle": "custom",
+        "navigationStyle": "default",
         "navigationBarTextStyle": "black",
         "navigationBarTitleText": "芯片扫描",
         "navigationBarBackgroundColor": "#FFFFFF",

+ 41 - 16
pages/index/index.vue

@@ -1,12 +1,20 @@
 <template>
     <view class="content">
-        111
+        <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");
@@ -19,7 +27,9 @@ onReady(() => {
 })
 
 //按键操作
-const rfidModuleInit = ({keyCode}) => {
+const isRfidInit = ref(true)
+const rfidModuleInit = () => {
+    isRfidInit.value = true
     uni.showLoading({
     	title: 'RFID模块加载中...',
         mask: true,
@@ -33,22 +43,24 @@ const rfidModuleInit = ({keyCode}) => {
     setTimeout(() => {
         // 使用模块前必须先初始化RDIF模块
         let { code } = rfidModule.init();
-        if (code == 0) {
+        if (code === 0) {
             uni.hideLoading();
-        } else if (code == -1) {
+        } else if (code === -1) {
             uni.hideLoading();
             uni.showToast({
                 title: 'RFID模块加载失败',
                 icon: 'error',
                 duration: 3000
             })
+        } else {
+            uni.hideLoading();
         }
+        isRfidInit.value = false
     }, 400);
 }
 
 //按键操作
 const keyListener = ({keyCode}) => {
-    console.log("按键:", keyCode)
     if (keyCode === 293 || keyCode === 312) {
         if (isScan.value) {
             stopScan()
@@ -60,20 +72,33 @@ const keyListener = ({keyCode}) => {
 
 //开始扫描
 const isScan = ref(false)
-const startScan = async () => {
+const startScan = () => {
+    if (isRfidInit.value) return
     isScan.value = true
-    const res = await startScanApi()
-    console.log(JSON.stringify(res))
-    console.log(res)
+    rfidModule.startScan((res) => {
+        if (res.code === 0) {
+            uni.showToast({
+                icon: "success",
+                title: '开启扫描成功'
+            })
+        } else if (res.code === 1) {
+            startScanData(res.data)
+        }
+    })
 }
 
-//扫描API
-const startScanApi = async () => {
-    return new Promise((resolve) => {
-        rfidModule.startScan((res) => {
-            resolve(res)
-        })
-    })
+//扫描结果处理
+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
 }
 
 //停止扫描