admin
2 天以前 6e4a6789e948bc6a8dc9fc1aa3eebc2980be3072
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>样机标签打印</span>
          </div>
          <el-form ref="form" :model="form" :rules="rules" label-width="100px">
            <el-form-item label="组别" prop="groupName">
              <el-select v-model="form.groupName" placeholder="请选择组别" style="width: 100%">
                <el-option
                  v-for="item in groupOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
 
            <el-form-item label="样机编号" prop="prototypeCode">
              <el-input
                v-model="form.prototypeCode"
                placeholder="请输入样机编号"
                style="width: 100%">
              </el-input>
            </el-form-item>
 
            <el-form-item>
              <el-button type="primary" @click="handlePrint" :loading="printing">
                <i class="el-icon-printer"></i> 打印标签
              </el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
 
      <el-col :span="12">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>预览区域</span>
          </div>
          <div class="print-area">
            <div id="printSection" class="qrcode-container">
              <qrcode-vue :value="qrCodeValue" :size="300" level="H"></qrcode-vue>
              <div class="info-text">
                <div class="text-item">组别:{{ form.groupName || '-' }}</div>
                <div class="text-item">样机编号:{{ form.prototypeCode || '-' }}</div>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>
 
<script>
import QrcodeVue from 'qrcode.vue'
import QRCode from 'qrcode'
 
export default {
  name: 'PrototypeLabelPrinting',
  components: {
    QrcodeVue
  },
  data() {
    return {
      printing: false,
      form: {
        groupName: '',
        prototypeCode: ''
      },
      rules: {
        groupName: [
          { required: true, message: '请选择组别', trigger: 'change' }
        ],
        prototypeCode: [
          { required: true, message: '请输入样机编号', trigger: 'blur' }
        ]
      },
      groupOptions: [
        { value: '研发一组', label: '研发一组' },
        { value: '研发二组', label: '研发二组' },
        { value: '研发三组', label: '研发三组研发三组' }
      ]
    }
  },
  computed: {
    qrCodeValue() {
      if (!this.form.groupName || !this.form.prototypeCode) {
        return '暂无数据'
      }
      return `${this.form.groupName}${this.form.prototypeCode}`
    }
  },
  methods: {
    handlePrint() {
      this.$message.success('打印')
 
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.printing = true
          try {
            const qrDataUrl = await QRCode.toDataURL(this.qrCodeValue, {
              width: 800,
              margin: 1,
              errorCorrectionLevel: 'H'
            })
 
            const testPrintContent = `
              <!DOCTYPE html>
              <html>
                <head>
                  <title>测试打印</title>
                </head>
                <body>
                  <h1>这是一个测试打印内容</h1>
                </body>
              </html>
            `
 
            // 第一个打印机的内容 - 原始标签
            const firstPrintContent = `
              <!DOCTYPE html>
              <html>
                <head>
                  <title>样机标签打印-1</title>
                  <style>
                    @page {
                      size: 100mm 100mm;
                      margin: 0;
                    }
                    @media print {
                      html, body {
                        margin: 0;
                        padding: 0;
                        height: 100mm;
                        page-break-after: avoid;
                        page-break-before: avoid;
                      }
                    }
                    .print-container {
                      display: flex;
                      flex-direction: column;
                      align-items: center;
                      justify-content: center;
                      width: 100mm;
                      height: 80mm;
                      box-sizing: border-box;
                      padding: 5mm;
                      page-break-inside: avoid;
                    }
                    .qr-code {
                      width: 80mm;
                      height: 80mm;
                      margin-bottom: 3mm;
                    }
                    .qr-code img {
                      width: 100%;
                      height: 100%;
                      object-fit: contain;
                    }
                    .info-text {
                      text-align: center;
                      font-size: 3mm;
                      font-weight: bold;
                      line-height: 1.5;
                    }
                    .info-text div {
                      margin: 1mm 0;
                    }
                  </style>
                </head>
                <body>
                  <div class="print-container">
                    <div class="qr-code">
                      <img src="${qrDataUrl}" alt="QR Code"/>
                    </div>
                    <div class="info-text">
                      <div>组别:${this.form.groupName}</div>
                      <div>样机编号:${this.form.prototypeCode}</div>
                      <div>原始标签</div>
                    </div>
                  </div>
                </body>
              </html>
            `
 
            // 第二个打印机的内容 - 备用标签
            const secondPrintContent = `
              <!DOCTYPE html>
              <html>
                <head>
                  <title>样机标签打印-2</title>
                  <style>
                    @page {
                      size: 100mm 100mm;
                      margin: 0;
                    }
                    @media print {
                      html, body {
                        margin: 0;
                        padding: 0;
                        height: 100mm;
                        page-break-after: avoid;
                        page-break-before: avoid;
                      }
                    }
                    .print-container {
                      display: flex;
                      flex-direction: column;
                      align-items: center;
                      justify-content: center;
                      width: 100mm;
                      height: 80mm;
                      box-sizing: border-box;
                      padding: 5mm;
                      page-break-inside: avoid;
                    }
                    .qr-code {
                      width: 80mm;
                      height: 80mm;
                      margin-bottom: 3mm;
                    }
                    .qr-code img {
                      width: 100%;
                      height: 100%;
                      object-fit: contain;
                    }
                    .info-text {
                      text-align: center;
                      font-size: 3mm;
                      font-weight: bold;
                      line-height: 1.5;
                    }
                    .info-text div {
                      margin: 1mm 0;
                      color: red;
                    }
                  </style>
                </head>
                <body>
                  <div class="print-container">
                    <div class="qr-code">
                      <img src="${qrDataUrl}" alt="QR Code"/>
                    </div>
                    <div class="info-text">
                      <div>组别:${this.form.groupName}</div>
                      <div>样机编号:${this.form.prototypeCode}</div>
                      <div>备用标签</div>
                    </div>
                  </div>
                </body>
              </html>
            `
 
            // 指定打印机名称和对应的内容
            const printTasks = [
              {
                printerName: 'Kyocera ECOSYS M4125idn KX',  // 使用系统中实际存在的打印机名称
                content: testPrintContent
              },
              {
                printerName: 'Kyocera ECOSYS M4125idn KX',  // 使用系统中实际存在的打印机名称
                content: firstPrintContent
              },
              {
                printerName: 'Kyocera ECOSYS M4125idn KX',  // 使用系统中实际存在的打印机名称
                content: secondPrintContent
              }
            ]
 
            // 监听打印完成事件
            if (window.electron?.isElectron) {
              this.$message.success('22')
 
              window.electron.ipcRenderer.on('print-complete', (result) => {
                if (result.success) {
                  this.$message.success('打印成功')
                } else {
                  this.$message.error(`打印失败: ${result.error}`)
                  console.error('Print error:', result.error)
                }
              })
 
              // 发送打印任务到主进程
              printTasks.forEach(task => {
                this.$message.success('33')
 
                window.electron.ipcRenderer.send('silent-print', {
                  content: task.content,
                  printerName: task.printerName
                })
              })
            }
 
            this.printing = false
          } catch (error) {
            console.error('打印失败:', error)
            this.$message.error('打印失败,请重试')
            this.printing = false
          }
        }
      })
    }
  }
}
</script>
 
<style lang="scss" scoped>
.app-container {
  padding: 20px;
 
  .box-card {
    margin-bottom: 20px;
 
    ::v-deep .el-card__header {
      padding: 12px 20px;
      border-bottom: 1px solid #dcdfe6;
 
      .clearfix {
        font-size: 15px;
        font-weight: bold;
        color: #303133;
      }
    }
  }
}
 
.print-area {
  display: flex;
  justify-content: center;
  padding: 20px;
 
  .qrcode-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 30px;
    border: 1px dashed #d9d9d9;
    border-radius: 4px;
    background-color: #fafafa;
    width: 400px;
    height: 400px;
 
    .info-text {
      margin-top: 20px;
      text-align: center;
 
      .text-item {
        margin: 8px 0;
        font-size: 14px;
        color: #606266;
        font-weight: bold;
      }
    }
  }
}
</style>