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