Crunchy
2025-06-14 7e460156de73171f9660ce48f80703e79f8b478d
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<template>
  <div class="barcode">
<!--绑定id方法-->
<!-- <div id='box'>
   <p>打印内容</p>
</div>
<div v-print='#box'>打印</div> -->
<!--绑定对象方法-->
<div id='printMe'>
  <vue-barcode
  :value="value"
  >不支持vue-barcode</vue-barcode>
   <div>
    <div>产品名称</div>
    <div>数量</div>
    <div>单位</div>
   </div>
   <div>
    <div>时间</div>
    <div>规格</div>
   </div>
</div>
 
<div v-for="(item,index) in table" :key="index">
  {{item}}
</div>
 
<el-button @click="showCode">生成条形码</el-button>
<el-button v-print='printObj'>打印</el-button>
<el-input v-show="false" class="input" v-model="value" ref="input"></el-input> 
 
  </div>
</template>
 
<script>
   import VueBarcode from 'vue-barcode'
 
export default{
   data(){
      return {
         printObj:{
            id: "printMe", // 打印的区域
            // preview: false, // 预览工具是否启用
            previewTitle: '打印条码', // 预览页面的标题
            popTitle: '', // 打印页面的页眉
            previewBeforeOpenCallback(vue) {
              console.log('正在加载预览窗口')
            },
            previewOpenCallback(vue) {
              console.log('已经加载完预览窗口')
            },
            clickMounted: (vue) => {
              console.log("触发点击打印回调");
              vue.isShowPrint = true  //弹框显示条码
            },
            beforeOpenCallback(vue) {
              console.log('打开之前',vue.barcodeNum)
            },
            openCallback (vue) {
              vue.isShowPrint = false  // 关闭条码显示弹框
              console.log('执行了打印',vue.barcodeNum)
            },
         },
         value: '',
         table:[]
      }
   },
   components: { VueBarcode },
   mounted() {
    
    this.makeCode('1234567891234567')
    this.addScanMonitor()
   },
   methods: {
    // 监听扫描
    showCode() {
      this.makeCode('1234567891234567')
// window.document.onkeypress=undefined
    },
    addScanMonitor() {
      window.document.onkeypress = e => {
        console.log(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('扫码结束,条形码:', this.code)
          this.table.push(this.code)
          this.scanningForm.scanCode = this.code
          this.lastCode = ''
          this.lastTime = ''
          this.handleSubmitScanning()
          return
        }
 
        this.nextTime = new Date().getTime()
        if (!this.lastTime && !this.lastCode) {
          this.code = '' // 清空上次的条形码
          // 继续扫描一下条前关闭弹窗
          // this.handleCloseTipsVisible()
          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
      }
    },
    makeCode(msg) {
      // let obj = {jack: '1212'}
     this.value = msg
 
    }
   }
}
</script>
 
<style lang="scss">
 
  #printMe{
  // height: 4cm;
  width: 8cm;
  display: flex;
  flex-direction: column;
  text-align: center;
  img{
    display: inline-block;
    text-align: center;
    width: 6cm;
    height: 2cm;
  }
  >div{
    width: 100%;
    display: flex;
    >div{
      flex: 1;
      // height: 30px;
      border: 1px solid #ddd;
    }
  }
}
@media print {
  @page {
    size: 8cm 4cm !important;
    // margin: 0cm;
    // padding: 0cm;
    // size: portrait;
    // border: 1px solid #ddd;
    
  }
  #printMe{
    display: block;
    width: 100%;
      overflow: hidden;
    }
  }
</style>