提交 | 用户 | 时间
|
0cceb6
|
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 |
this.codeValue = code; |
|
64 |
}, |
|
65 |
}, |
|
66 |
} |
|
67 |
</script> |
|
68 |
<style scoped lang="scss"> |
|
69 |
|
|
70 |
</style> |
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|