Forráskód Böngészése

切换项目更新

ZaiZai 11 hónapja
szülő
commit
cb2e1694d4
1 módosított fájl, 46 hozzáadás és 2 törlés
  1. 46 2
      src/views/tentative/material/sampling.vue

+ 46 - 2
src/views/tentative/material/sampling.vue

@@ -161,7 +161,7 @@
                     </el-form-item>
                 </div>
                 <el-form-item v-if="addEditFormModel.isRfid === 1" label="RFID编号">
-                    <el-input v-model="addEditFormModel.rfid">
+                    <el-input v-model="addEditFormModel.rfid" disabled clearable>
                         <template #append>
                             <el-button>选择RFID</el-button>
                         </template>
@@ -472,6 +472,7 @@ const addFormModalClick = () => {
         userId: userInfo.value.user_id,
     }
     addEditFormModal.value = true
+    requestDevice()
 }
 //编辑
 const editFormModalClick = () => {
@@ -482,13 +483,56 @@ const editFormModalClick = () => {
         addEditFormModel.value.representativeCount = addEditFormModel.value.representativeCount === -1 ? '' : addEditFormModel.value.representativeCount
         addEditFormModel.value.materialCount = addEditFormModel.value.materialCount === -1 ? '' : addEditFormModel.value.materialCount
         addEditFormModal.value = true
+        requestDevice()
     } else if (keys.length > 1) {
         window?.$message?.warning('只能选择一条数据编辑')
     }
 }
-const addEditFormModalClose = () => {
+
+//获取读卡器设备
+let device
+const deviceTable = ref([])
+const requestDevice = async () => {
+    if (isNullES(device)) {
+        const devices = await navigator.hid.requestDevice({
+            filters: [{ vendorId: 1240, productId: 831 }],
+        })
+        if (devices.length <= 0) return
+        device = devices[0]
+    }
+    // 检查设备是否打开
+    if (!device.opened) {
+        await device.open() // 打开设备
+    }
+    // 电脑接收到来自设备的消息回调
+    device.oninputreport = (event) => {
+        const { isRfid } = addEditFormModel.value
+        if (isRfid !== 1 ) return
+        const array = new Uint8Array(event.data.buffer)
+        const data = array.slice(10, array[0] - 1)
+        let epc = ''
+        const res = new Uint8Array(data)
+        for (const data of res) {
+            // 将字节数据转换成(XX )形式字符串
+            epc += (Array(2).join(0) + data.toString(16).toUpperCase()).slice(-2) + ''
+        }
+        addEditFormModel.value.rfid = epc
+        const index = arrIndex(deviceTable.value, 'epc', epc)
+        if (index === -1) {
+            deviceTable.value.push({ epc: epc })
+        }
+    }
+}
+
+//弹窗关闭
+const addEditFormModalClose = async () => {
     addEditFormModal.value = false
     addEditFormModel.value = {}
+    if (!isNullES(device)) {
+        await device.close() // 关闭设备
+        await device.forget() // 遗忘设备
+        device = null
+    }
 }
 
 //新增/编辑 表单