js 扫码枪,扫码发货

扫码枪就是模拟键盘输入事件

  <div v-for="(item,index) in strackQrcodeList.length" :key="index">
                        <!-- strack.length -->
                      <el-input
                        class="handle-input mb10"
                        ref="inputFocus"
                        v-focus
                        palceholder="请将光标保持在输入框内,使用扫码枪扫码"
                        @focus="batchFocus(item-1)"
                        @input="batchItem(strackQrcodeList[item-1],index)"
                        v-model="strackQrcodeList[item-1]" 
                      ></el-input>
                      <!-- 
                        
                        
                        -->
                      <el-button @click="reset(item-1)">删除</el-button>
                    </div>



//  methods:

//关联批次聚焦
    batchFocus(index) {
      
      this.type = 2;
      this.scanCode(index);
    },
scanCode(index) {
      let code = "";
      let lastTime, nextTime;
      let lastCode, nextCode;
      let that = this;
      
      
      window.document.onkeydown = function(e) {
       
        if (window.event) {
          // IE
          nextCode = e.keyCode;
        } else if (e.which) {
          // Netscape/Firefox/Opera
          nextCode = e.which;
        }
        if (e.which === 13) {
          if (CodeSplit(code).length < 3) return; // 手动输入的时间不会让code的长度大于2,所以这里只会对扫码枪有
          
          console.log(code)
          console.log("‘扫码结束‘");
         var codeLenght=CodeSplit(code).length
              if(codeLenght>=9&&codeLenght<13){
                that.bigNum=CodeSplit(code)
                // let bigZh=that.bigNum.substring(0,codeLenght-4)
                // let bigSmall=that.bigNum.substring(0,codeLenght-8)
                that.bigArr.push(that.bigNum)
                // that.bigArr.push(bigSmall)
              }
              if(codeLenght>=13&&codeLenght<17){
                that.zhNum=CodeSplit(code)
                that.zhArr.push(that.zhNum)
              }
              if(codeLenght>=17){
                that.smallNum=CodeSplit(code)
              }
              console.log(that.bigArr)
              if(that.bigArr.includes(CodeSplit(code).substring(0,codeLenght-4))&&CodeSplit(code)!=''){
                that.$message.warning(`中箱批次号`+CodeSplit(code)+`已包含在大箱批次号`+that.bigNum+`中 请重新扫码`)

              }
              if(that.bigArr.includes(CodeSplit(code).substring(0,codeLenght-8))&&CodeSplit(code)!=''){
                that.$message.warning(`小箱批次号`+CodeSplit(code)+`已包含在大箱批次号`+that.bigNum+`中 请重新扫码`)
              }
              if(that.zhArr.includes(CodeSplit(code).substring(0,codeLenght-4))&&CodeSplit(code)!=''){
                that.$message.warning(`小箱批次号`+CodeSplit(code)+`已包含在中箱批次号`+that.zhNum+`中 请重新扫码`)
              }


          if(that.$route.path=='/scanningSendYc'){
              that.getCode(CodeSplit(code), index); // 获取到扫码枪输入的内容,做别的操作
          }
          code = "";
          lastCode = "";
          lastTime = "";
          return;
        }
        if(e.which==8){  //backspace事件
              code = ''
            lastCode = ''
            lastTime = ''
            return
        }
        nextTime = new Date().getTime()
        if (!lastTime && !lastCode) {
            code += e.key
        }
        if (lastCode && lastTime ) { 
            code += e.key
        } 
        lastCode = nextCode
        lastTime = nextTime
        
      };
    },
  batchItem(item,index){
      
      if(item.includes('code=')){
        let textNum1=item.split('=')[1]
        this.strackQrcodeList[index]=textNum1.split('&')[0]
        
        this.getCode(this.strackQrcodeList[index],index)
      }
    },
    getCode(code, index) {
      console.log(code,'码')
      if (this.type == 1) {
        //快递单号
        this.formLabelAlign.logisticsNo=this.formLabelAlign.logisticsNo+','
        console.log(this.formLabelAlign, "for");
      } else {
        //调接口查询
        axiosRequest(
          "agentOrder/verification",
          { code: code, shopProductId: this.currentProId },
          "post"
        ).then(res => {
          console.log(res,'zhe')
          if(res.code==200){
                    let num = 0
                    for(let i=0;i<this.strackQrcodeList.length-1;i++){
                        console.log(this.strackQrcodeList.indexOf(code))
                        if(this.strackQrcodeList[i]==code){
                             num+=1
                        }
                      
                    }
                     if(num<=0){
                             this.strackQrcodeList[index] = code
                             this.strackQrcodeList.push('')
                        }else{
                            this.strackQrcodeList.splice(index,1)
                            this.strackQrcodeList.push('')
                            this.$message({
                                message:'存在重复的批次号' + code,
                                type:'warning',
                                duration:5000
                            })
                        }
                   
                }else{
                    this.strackQrcodeList.splice(index,1)
                    this.strackQrcodeList.push('')
                    
                }
        });
      }
    },

 

posted @ 2021-04-27 16:28  收藏小能手  阅读(283)  评论(0编辑  收藏  举报