huangxiaoqiang
2025-09-25 3dd51bc3ddd00cc0c8b901ad039f877d510a61ff
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
<template>
  <div>
    <vol-box
      v-model="showDetialBox"
      :lazy="true"
      width="500px"
      :padding="15"
      title="打印"
    >
      <!-- 打印专用容器 -->
      <div id="printContainer" style="display:none">
        <div style="text-align:center;margin-bottom:15px">
          <VueQrcode :value="palletCode" :size="400" />
        </div>
        <div style="text-align:center;font-size:18px;font-weight:bold">
          {{ palletCode }}
        </div>
      </div>
 
      <!-- 预览区域 -->
      <div id="previewContent">
        <div style="text-align:center;margin-bottom:15px">
          <VueQrcode :value="palletCode" :size="200" />
        </div>
        <div style="text-align:center;font-size:18px;font-weight:bold">
          {{ palletCode }}
        </div>
      </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 VueQrcode from "vue-qrcode";
 
export default {
  components: { VolBox, VueQrcode },
  data() {
    return {
      showDetialBox: false,
      palletCode: "",
    };
  },
  methods: {
    open(row) {
      this.showDetialBox = true;
      if (row?.palletCode) {
        this.palletCode = row.palletCode;
      }
    },
    async print() {
      // 创建打印窗口
      const printWindow = window.open("", "打印", "width=1200,height=800");
      
      // 构建打印内容
      const printContent = `
        <html>
        <head>
          <style>
            @media print {
              body { margin: 0; padding: 20px; text-align: center; }
              canvas { display: block; margin: 0 auto; }
              div { margin-bottom: 15px; }
            }
          </style>
        </head>
        <body>
          ${document.getElementById("printContainer").innerHTML}
        </body>
        </html>
      `;
      
      printWindow.document.write(printContent);
      printWindow.document.close();
      
      // 延迟确保内容加载完成
      setTimeout(() => {
        printWindow.focus();
        printWindow.print();
        
        // 打印完成后关闭窗口并更新状态
        printWindow.onafterprint = () => {
          this.updatePrintStatus();
          printWindow.close();
        };
      }, 500);
    },
    async updatePrintStatus() {
      try {
        const response = await this.http.post(
          "api/palletCodeInfo/PrintStatusUp",
          { printCode: this.palletCode },
          { loadingText: "数据处理中" }
        );
        
        if (response.status) {
          this.$message.success("操作成功");
          this.$parent.refresh();
        } else {
          this.$message.error(response.message || "操作失败");
        }
      } catch (error) {
        this.$message.error("请求失败");
      } finally {
        this.showDetialBox = false;
      }
    }
  }
};
</script>
 
<style scoped>
/* 预览区域样式 */
#previewContent {
  text-align: center;
  padding: 20px;
}
 
#previewContent canvas {
  display: block;
  margin: 0 auto;
}
</style>
 
<style>
/* 打印样式 */
@media print {
  body {
    margin: 0;
    padding: 15px;
    -webkit-print-color-adjust: exact;
  }
}
</style>