chen.llin
8 天以前 4ca2900c4e3e9400d9a94567b8eb01b3948648cc
common/rfid-input-helper.js
@@ -107,32 +107,50 @@
    * len:读取的数据长度(Word类型,大于0)
    * pwd:访问密码,默认密码为:00000000
    * 注意:Word类型,一个长度表示标签存储4位字符
    * len=1表示读取4位,len=2表示读取8位,len=8表示读取32位
    * 使用len=1读取4位数据(如果标签是4位)
    */
   uhfModel.readTagWithoutFilter(1, 2, 6, "00000000", (ret) => {
   const readLen = 1; // 读取长度:1个Word = 4个字符
   uhfModel.readTagWithoutFilter(1, 2, readLen, "00000000", (ret) => {
      isScanning = false;
      console.log('[RFIDInputHelper] readTagWithoutFilter result:', ret);
      
      if (ret && typeof ret === 'string') {
         // 解析返回结果,提取EPC
         // 解析返回结果,提取EPC(不追加,不截断,使用实际长度)
         // 返回格式可能是:success EPC:XXXXXXXX 或类似格式
         let epc = '';
         if (ret.includes('success') || ret.includes('EPC')) {
            // 尝试提取EPC值
            // 尝试提取EPC值(提取完整的16进制字符串,不限制长度)
            const epcMatch = ret.match(/EPC[:\s]+([A-F0-9]+)/i);
            if (epcMatch && epcMatch[1]) {
               epc = epcMatch[1];
               epc = epcMatch[1]; // 使用实际长度,不追加
            } else {
               // 如果没有匹配到,尝试提取16进制字符串
               const hexMatch = ret.match(/([A-F0-9]{16,})/i);
               // 如果没有匹配到,尝试提取16进制字符串(不限制最小长度)
               const hexMatch = ret.match(/([A-F0-9]+)/i);
               if (hexMatch && hexMatch[1]) {
                  epc = hexMatch[1];
                  epc = hexMatch[1]; // 使用实际长度,不追加
               }
            }
         }
         
         if (epc) {
            console.log('[RFIDInputHelper] ✓ 读取到标签EPC:', epc);
            // 自动填入当前焦点输入框
            // 根据读取长度截取数据(len=1表示4位,len=2表示8位)
            const maxLength = readLen * 4; // Word长度 × 4 = 字符数
            if (epc.length > maxLength) {
               console.log(`[RFIDInputHelper] 数据过长(${epc.length}位),截取前${maxLength}位`);
               epc = epc.substring(0, maxLength);
            }
            // 去除前导0(如果插件返回了固定长度的数据,前面可能有0填充)
            // 但保留至少1位(如果全部是0,保留一个0)
            const originalLength = epc.length;
            epc = epc.replace(/^0+/, '') || '0';
            if (originalLength !== epc.length) {
               console.log('[RFIDInputHelper] 去除前导0: 原始长度=' + originalLength + ', 去除后长度=' + epc.length);
            }
            console.log('[RFIDInputHelper] ✓ 读取到标签EPC (长度' + epc.length + '位):', epc);
            // 自动填入当前焦点输入框(不追加,使用实际长度)
            fillCurrentInput(epc);
            modal.toast({
               message: '已读取标签',
@@ -147,10 +165,18 @@
                  duration: 1.5
               });
            } else {
               // 尝试直接使用返回的字符串作为EPC
               // 尝试直接使用返回的字符串作为EPC(不限制最小长度)
               const cleanRet = ret.trim();
               if (cleanRet.length >= 8) {
                  fillCurrentInput(cleanRet);
               // 移除可能的"success"等前缀,只保留16进制字符
               let hexOnly = cleanRet.replace(/[^A-F0-9]/gi, '');
               if (hexOnly.length > 0) {
                  // 去除前导0(如果插件返回了固定长度的数据,前面可能有0填充)
                  const originalLength = hexOnly.length;
                  hexOnly = hexOnly.replace(/^0+/, '') || '0';
                  if (originalLength !== hexOnly.length) {
                     console.log('[RFIDInputHelper] 去除前导0: 原始长度=' + originalLength + ', 去除后长度=' + hexOnly.length);
                  }
                  fillCurrentInput(hexOnly); // 使用实际长度,不追加
                  modal.toast({
                     message: '已读取标签',
                     duration: 1.5
@@ -166,9 +192,34 @@
      } else if (ret && typeof ret === 'object') {
         // 如果返回的是对象格式
         if (ret.code === 'success' && ret.data) {
            const epc = ret.data.epc || ret.data;
            let epc = '';
            if (typeof ret.data === 'string') {
               // 如果是字符串,提取16进制部分(不追加,不截断)
               epc = ret.data.trim().replace(/[^A-F0-9]/gi, '');
            } else if (ret.data.epc) {
               epc = String(ret.data.epc).trim().replace(/[^A-F0-9]/gi, '');
            } else {
               epc = String(ret.data).trim().replace(/[^A-F0-9]/gi, '');
            }
            // 根据读取长度截取数据(len=1表示4位,len=2表示8位)
            if (epc) {
               fillCurrentInput(epc);
               const maxLength = readLen * 4; // Word长度 × 4 = 字符数
               if (epc.length > maxLength) {
                  console.log(`[RFIDInputHelper] 数据过长(${epc.length}位),截取前${maxLength}位`);
                  epc = epc.substring(0, maxLength);
               }
               // 去除前导0(如果插件返回了固定长度的数据,前面可能有0填充)
               // 但保留至少1位(如果全部是0,保留一个0)
               const originalLength = epc.length;
               epc = epc.replace(/^0+/, '') || '0';
               if (originalLength !== epc.length) {
                  console.log('[RFIDInputHelper] 去除前导0: 原始长度=' + originalLength + ', 去除后长度=' + epc.length);
               }
               console.log('[RFIDInputHelper] ✓ 读取到标签EPC (长度' + epc.length + '位):', epc);
               fillCurrentInput(epc); // 使用实际长度,不追加
               modal.toast({
                  message: '已读取标签',
                  duration: 1.5
@@ -229,14 +280,25 @@
         { field: 'locNo', focusField: 'locNoFocus' }
      ];
      
      // 调试:打印所有输入框的焦点状态
      console.log('[RFIDInputHelper] ========== 检查输入框焦点状态 ==========');
      for (let item of inputFieldMap) {
         if (vm[item.field] !== undefined) {
            const focusValue = vm[item.focusField];
            console.log(`[RFIDInputHelper]   ${item.field}: focus=${focusValue}, value="${vm[item.field]}"`);
         }
      }
      // 只查找有焦点的输入框(光标所在的输入框)
      let focusedField = null;
      for (let item of inputFieldMap) {
         if (vm[item.field] !== undefined) {
            // 检查是否有焦点状态字段,并且焦点为true
            if (vm[item.focusField] === true) {
            const focusValue = vm[item.focusField];
            console.log(`[RFIDInputHelper] 检查 ${item.field}: focusField=${item.focusField}, focusValue=${focusValue}, type=${typeof focusValue}`);
            if (focusValue === true) {
               focusedField = item.field;
               console.log(`[RFIDInputHelper] ✓ 找到有焦点的输入框: ${item.field}`);
               console.log(`[RFIDInputHelper] ✓✓✓ 找到有焦点的输入框: ${item.field} (focus=${focusValue})`);
               break;
            }
         }
@@ -245,20 +307,35 @@
      // 只填入有焦点的输入框,如果没有焦点则不填入
      if (focusedField) {
         console.log(`[RFIDInputHelper] 填入有焦点的输入框 ${focusedField}:`, epc);
         vm[focusedField] = epc;
         // 触发input事件,确保页面逻辑能响应
         console.log(`[RFIDInputHelper] 输入框当前值: "${vm[focusedField]}"`);
         // 先清空输入框,再填入新值(避免追加)
         vm[focusedField] = '';
         // 使用 $nextTick 确保清空操作完成后再填入新值
         if (vm.$nextTick) {
            vm.$nextTick(() => {
               // 如果页面有对应的input处理方法,可以手动触发
               if (focusedField === 'barcode' && typeof vm.barcodeInput === 'function') {
                  vm.barcodeInput();
               } else if (focusedField === 'matnr' && typeof vm.findMat === 'function') {
                  vm.findMat();
               }
               // 填入新的EPC值(替换,不追加)
               vm[focusedField] = epc;
               console.log(`[RFIDInputHelper] ✓ 已替换输入框值为: "${vm[focusedField]}"`);
               // 再次使用 $nextTick 确保值已设置,再触发input事件
               vm.$nextTick(() => {
                  // 如果页面有对应的input处理方法,可以手动触发
                  if (focusedField === 'barcode' && typeof vm.barcodeInput === 'function') {
                     vm.barcodeInput();
                  } else if (focusedField === 'matnr' && typeof vm.findMat === 'function') {
                     vm.findMat();
                  }
               });
            });
         } else {
            // 如果没有 $nextTick,直接设置
            vm[focusedField] = epc;
         }
      } else {
         console.warn('[RFIDInputHelper] 未找到有焦点的输入框,不填入任何输入框');
         console.warn('[RFIDInputHelper] ✗ 未找到有焦点的输入框,不填入任何输入框');
         console.warn('[RFIDInputHelper] 请先点击输入框获得焦点,然后再按扫码按键');
         modal.toast({
            message: '请先点击输入框获得焦点',
            duration: 1.5