zhouwx
2024-08-21 48d5ab35c3fcdc6edca1278d1474a1b54a431191
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
<template>
  <div class="notice">
    <el-dialog
        v-model="dialogVisible"
        width="400px"
        :before-close="handleClose"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
    >
      <div style="display: flex;flex-direction: column;align-items: flex-end">
        <el-button type="primary" style="margin-bottom: 5px" @click="printCode">打印</el-button>
      </div>
      <el-card style="max-width: 480px">
        <div :id="state.form.code">
          <div style="display: flex;flex-direction: column;align-items: center;">
            <span style="font-size:14px;margin-top:10px;">{{title}}</span>
            <vue-qr :size="80" :margin="0" :auto-color="true" :dot-scale="1" :text="state.form.code" style="margin: 10px 0 5px 0"></vue-qr>
            <span style="font-size:10px;margin-top:5px;">{{state.form.code}}</span>
          </div>
        </div>
      </el-card>
    </el-dialog>
  </div>
</template>
<script setup>
import {reactive, ref, toRefs} from 'vue'
import {ElMessage} from "element-plus";
import VueQr from "vue-qr/src/packages/vue-qr.vue";
 
const dialogVisible = ref(false);
const title = ref("");
const busRef = ref();
const length = ref()
const emit = defineEmits(["getList"]);
 
const state = reactive({
})
 
 
const openDialog = async (type,value) => {
  if(type == 'pro'){
    title.value = value.productBasic.name + ' ' + '—' + ' '+ value.productBasic.productSn
  }else {
    title.value = value.hazmatBasic.name + ' ' + '—' + ' '+ value.hazmatBasic.productSn
  }
 
  state.form = JSON.parse(JSON.stringify(value));
  dialogVisible.value = true;
}
const handleClose = () => {
  dialogVisible.value = false;
  emit("getList")
}
const printCode = () => {
  let data = []
  data = [state.form]
  let qrCodes=''
  for(let index in data){
    console.log(index,'index')
    qrCodes+=`<div class="my-list-col"><div class="centered-content">${document.getElementById(data[index].code).innerHTML}</div></div>`
  }
  const printContent=document.createElement('div')
  printContent.innerHTML=qrCodes
 
  //创建一个新的隐藏的iframe元素
  const printFrame =document.createElement('iframe')
  printFrame.style.display='none'
  document.body.appendChild(printFrame)
 
  const printDocument=printFrame.contentWindow.document
  printDocument.open()
  printDocument.write(`
  <html>
  <head>
    <title>Print</title>
    <style>
      @media print {
        .page-break { page-break-after:always;} /* 定义分页符格式*/
        .centered-content { text-align:center;} /* 居中对齐内容*/
        @page { size:50mm 40mm;margin:0mm; }
      }
    </style>
  </head>
  <body style='margin:0;padding:0;'>${printContent.innerHTML}</body>
  </html>
  `)
  printDocument.close()
  //在打印窗口中调用打印功能
  console.log('printFrame.contentWindow.document.body.style',printFrame.contentWindow.document.body.style)
  printFrame.contentWindow.print()
  //移除隐藏的iframe元素
  document.body.removeChild(printFrame)
  // handleClose()
}
 
defineExpose({
  openDialog
});
 
</script>
 
<style scoped lang="scss">
.notice{
  :deep(.el-form .el-form-item__label) {
    font-size: 15px;
  }
  .file {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
  }
}
</style>