heshaofeng
2026-04-09 ca3e4977395bc02c5d147dffdff7381333fdfbca
ÏîÄ¿´úÂë/WIDESEA_WMSClient/src/extension/inbound/extend/AllcatedPallet.vue
@@ -19,8 +19,8 @@
      <div class="location-section compact">
        <el-form :model="form" :rules="rules" ref="locationForm" class="compact-form">
          <el-form-item label="仓库区域" prop="locationType" class="location-select compact-item">
            <el-select v-model="form.locationType" placeholder="请先选择仓库" clearable filterable
              @change="handleLocationChange" style="width: 100%" :loading="locationLoading" size="medium">
            <el-select v-model="form.locationType" placeholder="自动获取仓库区域" clearable filterable
              @change="handleLocationChange" style="width: 100%" :loading="locationLoading" size="medium" :disabled="true">
              <el-option v-for="item in locationTypes" :key="item.locationType" :label="item.locationTypeDesc"
                :value="item.locationType" />
            </el-select>
@@ -175,7 +175,7 @@
import http from '@/api/http.js';
import VolBox from '@/components/basic/VolBox.vue';
// é˜²æŠ–函数保持不变
// é˜²æŠ–函数
function debounce(func, wait) {
  let timeout;
  return function () {
@@ -251,7 +251,11 @@
            trigger: 'change'
          }
        ]
      }
      },
      // æ–°å¢žï¼šé”®ç›˜äº‹ä»¶ç›‘听标记
      keyPressListenerAdded: false,
      isDialogClosing: false
    }
  },
@@ -272,6 +276,26 @@
  },
  
  watch: {
    // ç›‘听show变量变化
    show(newVal) {
      if (newVal === true) {
        console.log('弹框打开,重置数据');
        this.isDialogClosing = false;
        this.resetData();
        this.$nextTick(() => {
          setTimeout(() => {
            this.fetchUnpalletMaterialDetails();
            this.addKeyPressListener(); // æ·»åŠ é”®ç›˜äº‹ä»¶ç›‘å¬
          }, 300);
        });
      } else if (newVal === false && !this.isDialogClosing) {
        console.log('弹框关闭,移除事件监听');
        this.isDialogClosing = true;
        this.removeKeyPressListener(); // ç§»é™¤é”®ç›˜äº‹ä»¶ç›‘听
        this.resetData();
      }
    },
    visible(newVal, oldVal) {
      this.palletVisible = newVal;
@@ -306,15 +330,34 @@
  },
  
  mounted() {
    document.addEventListener('keypress', this.handleKeyPress);
    // ä¸åœ¨mounted时添加监听,在弹窗打开时添加
  },
  
  beforeDestroy() {
    document.removeEventListener('keypress', this.handleKeyPress);
    // ç¡®ä¿ç»„件销毁时移除监听
    this.removeKeyPressListener();
    this.clearAllTimers();
  },
  
  methods: {
    // æ·»åŠ é”®ç›˜äº‹ä»¶ç›‘å¬
    addKeyPressListener() {
      if (!this.keyPressListenerAdded) {
        document.addEventListener('keypress', this.handleKeyPress);
        this.keyPressListenerAdded = true;
        console.log('键盘事件监听已添加');
      }
    },
    // ç§»é™¤é”®ç›˜äº‹ä»¶ç›‘听
    removeKeyPressListener() {
      if (this.keyPressListenerAdded) {
        document.removeEventListener('keypress', this.handleKeyPress);
        this.keyPressListenerAdded = false;
        console.log('键盘事件监听已移除');
      }
    },
    open() {
      this.show = true;
      this.orderNo = "";
@@ -322,6 +365,13 @@
      this.initLocationTypes();
      this.initwarehouseTypes();
      this.fetchUnpalletMaterialDetails();
      // å¼¹çª—打开时添加键盘事件监听
      this.$nextTick(() => {
        setTimeout(() => {
          this.addKeyPressListener();
        }, 100);
      });
    },
    
    validateLocationType(rule, value, callback) {
@@ -396,6 +446,34 @@
        })
        .finally(() => {
          this.warehouseLoading = false;
        });
    },
    fetchLocationByWarehouse(warehouseCode) {
      if (!warehouseCode) {
        this.form.locationType = null;
        return;
      }
      this.locationLoading = true;
      http.post(`/api/InboundOrder/GetLocationType?code=${warehouseCode}`)
        .then(({ data }) => {
          if (data) {
            this.form.locationType = data.locationType || data;
            if (!this.locationTypes.find(item => item.locationType === this.form.locationType)) {
              this.locationTypes.push({
                locationType: this.form.locationType,
                locationTypeDesc: this.form.locationType
              });
            }
          }
        })
        .catch((err) => {
          console.error("获取仓库区域失败:", err);
          this.$message.error("获取仓库区域失败,请重试");
          this.form.locationType = null;
        })
        .finally(() => {
          this.locationLoading = false;
        });
    },
@@ -483,6 +561,9 @@
    },
    
    resetData() {
      // å…ˆç§»é™¤é”®ç›˜äº‹ä»¶ç›‘听
      this.removeKeyPressListener();
      this.palletGroupedBarcodes = {};
      this.isSubmitting = false;
      this.trayBarcode = '';
@@ -509,6 +590,8 @@
      };
      this.warehouseTypes = [];
      this.locationTypes = [];
      this.isDialogClosing = false;
      this.keyPressListenerAdded = false;
      
      this.$nextTick(() => {
        if (this.$refs.locationForm) {
@@ -529,6 +612,8 @@
    },
    
    handleDialogClose() {
      // å…ˆç§»é™¤é”®ç›˜äº‹ä»¶ç›‘听
      this.removeKeyPressListener();
      this.show = false;
      this.resetData();
    },
@@ -568,6 +653,10 @@
      this.isManualInput = true;
      this.isScanning = false;
      if (this.trayBarcode) {
        this.trayBarcode = this.trayBarcode.replace(/\s+/g, "");
      }
      if (this.manualInputTimer) {
        clearTimeout(this.manualInputTimer);
      }
@@ -581,6 +670,9 @@
      this.isManualInput = true;
      this.isScanning = false;
      if (this.barcode) {
        this.barcode = this.barcode.replace(/\s+/g, "");
      }
      if (this.manualInputTimer) {
        clearTimeout(this.manualInputTimer);
      }
@@ -609,7 +701,9 @@
        .then(valid => {
          if (!valid) return;
          
          const currentTrayBarcode = this.trayBarcode.trim();
          let currentTrayBarcode = (this.trayBarcode || "").replace(/\s+/g, "").trim();
         this.trayBarcode = currentTrayBarcode;
          if (!currentTrayBarcode) {
            this.error = '请输入或扫描托盘条码';
@@ -759,7 +853,7 @@
        });
    },
    // API请求
    // API请求 - æ³¨æ„è¿™é‡Œä¿®æ”¹äº†API地址为/AllGroupPallet
    fetchMaterialData(barcode) {
      return http.post('/api/Inbound/AllGroupPallet', {
        palletCode: this.trayBarcode,
@@ -797,6 +891,11 @@
    // å¤„理扫码枪输入
    handleKeyPress(event) {
      // æ£€æŸ¥å¼¹çª—是否显示
      if (!this.show || this.isDialogClosing) {
        return;
      }
      if (this.isManualInput || this.isSubmitting) {
        return;
      }
@@ -866,6 +965,7 @@
      this.barcode = '';
      this.materials = [];
      this.error = '';
      this.fetchLocationByWarehouse(this.form.warehouseType);
    },
    // åŒºåŸŸåˆ‡æ¢äº‹ä»¶