yyt
2024-06-26 0cceb649e1dc443c2a91d26d81eacb0867c96db3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<template>
  <el-input
    οnkeyup="this.value=this.value.replace(/\D|/g,'')"
    clearable
    maxlength="116"
    v-model="codeValue"
    placeholder="请输入条形码"
  />
</template>
<script>
export default {
  data(){
    return {
      codeValue: "",
      code: "",
      lastTime: "",
      nextTime: "",
      lastCode: "",
      nextCode: "",
      dtmainId: "",
    };
  },
  created() {
    window.document.onkeypress = (e) => {
      if (window.event) {
        // IE
        this.nextCode = e.keyCode;
      } else if (e.which) {
        // Netscape/Firefox/Opera
        this.nextCode = e.which;
      }
      if (e.which === 13) {
        // 键盘回车事件
        if (this.code.length < 3) return; // 扫码枪的速度很快,手动输入的时间不会让code的长度大于2,所以这里不会对扫码枪有效
 
        //console.log("扫码结束。");
        //console.log("条形码:", this.code);
        this.parseQRCode(this.code); // 获取到扫码枪输入的内容,做别的操作
        this.lastCode = "";
        this.lastTime = "";
        return;
      }
      this.nextTime = new Date().getTime();
      if (!this.lastTime && !this.lastCode) {
        this.code = ""; // 清空上次的条形码
        this.code += e.key;
        //console.log("扫码开始---", this.code);
      }
      if (this.lastCode && this.lastTime && this.nextTime - this.lastTime > 500) {
        // 当扫码前有keypress事件时,防止首字缺失
        this.code = e.key;
        //console.log("防止首字缺失。。。", this.code);
      } else if (this.lastCode && this.lastTime) {
        this.code += e.key;
        //console.log("扫码中。。。", this.code);
      }
      this.lastCode = this.nextCode;
      this.lastTime = this.nextTime;
    };
  },
  methods: {
    parseQRCode(code) {
      // if (code.length === 16) {
      //   console.log(code);
      // } else if (code.length === 0) {
      //   console.log("请输入条码!");
      // } else {
      //   alert("条码不合法:" + code);
      // }
      this.codeValue = code;
      // 发送网络请求
    },
  },
}
</script>
<style scoped lang="scss">
 
</style>