qinchulong
3 天以前 dc467182bfaeeb0dfd9bc2d6c4ec8fe64b179446
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
<template>
  <div>
    <vol-box
      v-model="showDetialBox"
      :lazy="true"
      width="300px"
      :padding="15"
      title="打印"
    >
      <!-- 条形码容器 -->
      <div id="printContent" style="text-align: center;">
        <div id="barcodeContainer" style="width: 200px; margin: 0 auto;"></div>
      </div>
      
      <!-- 编码文本 -->
      <div id="palletcode" style="text-align: center; margin-top: 15px; font-size: 16px;">
        {{ palletCode }}
      </div>
 
      <template #footer>
        <el-button type="primary" size="small" @click="print()">打印</el-button>
        <el-button type="danger" size="small" @click="showDetialBox = false">关闭</el-button>
      </template>
    </vol-box>
  </div>
</template>
 
<script>
import VolBox from "@/components/basic/VolBox.vue";
import JsBarcode from "jsbarcode";
 
export default {
  components: { VolBox },
  data() {
    return {
      showDetialBox: false,
      palletCode: "",
      row: null
    };
  },
  methods: {
    open(row) {
      this.row = row;
      this.showDetialBox = true;
      if (row && row.palletCode) {
        this.palletCode = row.palletCode;
        // 等待DOM渲染完成后生成条形码
        this.$nextTick(() => {
          this.generateBarcode();
        });
      }
    },
 
    generateBarcode() {
      const container = document.getElementById('barcodeContainer');
      if (!container) {
        this.$message.error("条形码容器不存在");
        return;
      }
      
      // 清空容器并生成新的条形码
      container.innerHTML = '';
      const canvas = document.createElement('canvas');
      container.appendChild(canvas);
      
      try {
        JsBarcode(canvas, this.palletCode, {
          format: 'CODE128',
          width: 2,
          height: 100,
          displayValue: false,
          margin: 10,
          renderer: 'canvas'
        });
      } catch (error) {
        this.$message.error(`条形码生成失败:${error.message}`);
      }
    },
 
    print() {
      // 获取条形码Canvas元素
      const barcodeCanvas = document.querySelector('#barcodeContainer canvas');
      if (!barcodeCanvas) {
        this.$message.error("未找到条形码图像,请重试");
        return;
      }
 
      // 将Canvas转换为图片URL(关键修复:解决打印窗口中Canvas渲染问题)
      const barcodeDataUrl = barcodeCanvas.toDataURL('image/png');
      
      // 获取文本内容
      const palletcodeText = document.getElementById("palletcode").innerText;
      
      // 打开打印窗口并写入内容
      const printWindow = window.open("", "_blank");
      printWindow.document.write(`
        <!DOCTYPE html>
        <html>
        <head>
          <meta charset="UTF-8">
          <title>打印条形码</title>
          <style>
            body { 
              margin: 0; 
              padding: 20px; 
              text-align: center; 
            }
            .barcode-image { 
              max-width: 100%; 
              height: auto; 
              margin: 0 auto; 
            }
            .code-text { 
              margin-top: 15px; 
              font-size: 16px; 
              font-family: Arial, sans-serif; 
            }
          </style>
        </head>
        <body>
          <!-- 使用图片代替Canvas,确保打印兼容性 -->
          <img src="${barcodeDataUrl}" class="barcode-image" alt="条形码">
          <div class="code-text">${palletcodeText}</div>
        </body>
        </html>
      `);
      
      printWindow.document.close();
      printWindow.focus();
      
      // 等待图像加载完成后再打印(关键修复:解决图像未加载的问题)
      const imgElement = printWindow.document.querySelector('.barcode-image');
      imgElement.onload = () => {
        setTimeout(() => {
          printWindow.print();
          printWindow.close();
          
          
          // 调用API更新打印状态
          this.updatePrintStatus();
        }, 200); // 增加延迟确保图像完全渲染
      };
    },
 
    updatePrintStatus() {
      console.log(this.palletCode)
      this.http
        .post("api/Dt_PalletInfo/PrintStatusUp?printCode="+this.palletCode , null, "数据处理中")
        .then((x) => {
          if (!x.status) return this.$message.error(x.message);
          this.$message.success("打印成功");
          this.$parent.refresh();
          this.showDetialBox = false;
        })
        .catch(() => {
          this.$message.error("更新打印状态失败");
        });
    }
  }
};
</script>