gaoluyang
2 天以前 2323c93baf009d2f60acaec545622611673203cb
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>
    <el-dialog :title="operationType === 'add' ? '新增二维码' : '编辑二维码'"
               v-model="dialogVisitable" width="500px" @close="cancel">
      <el-form :model="form" :rules="rules" ref="formRef" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="设备名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入设备名称" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="地点" prop="taxTrans">
              <el-input v-model="form.taxTrans" placeholder="请输入地点" maxlength="30"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div>
        <el-button type="primary" @click="submitForm">生成并打印二维码</el-button>
      </div>
      <div v-if="isShowQrCode" class="print-section" ref="qrCodeContainer" id="qrCodeContainer">
        <vue-qrcode :value="qrCodeValue" :width="qrCodeSize"></vue-qrcode>
      </div>
    </el-dialog>
  </div>
</template>
 
<script setup>
import useUserStore from "@/store/modules/user.js";
import {reactive, ref} from "vue";
import printJS from 'print-js'; // 引入 print.js
 
const { proxy } = getCurrentInstance()
const emit = defineEmits()
const userStore = useUserStore()
const dialogVisitable = ref(false);
const isShowQrCode = ref(false);
const operationType = ref('add');
 
const qrCodeValue = ref('https://example.com');
const qrCodeSize = ref(100);
const data = reactive({
  form: {
    name: '',
    taxTrans: '',
  },
  rules: {
    name: [{ required: true, message: '请输入设备名称', trigger: 'blur' }],
    taxTrans: [{ required: true, message: '请输入地点', trigger: 'blur' }]
  }
})
const { form, rules } = toRefs(data)
 
 
// 打开弹框
const openDialog = async (type, row) => {
  dialogVisitable.value = true
}
// 提交合并表单
const submitForm = () => {
  proxy.$refs["formRef"].validate(valid => {
    if (valid) {
      // 将表单数据转为 JSON 字符串作为二维码内容
      qrCodeValue.value = JSON.stringify(form.value);
      isShowQrCode.value = true;
      
      // 延迟执行打印,避免 DOM 更新前就调用打印
      setTimeout(() => {
        printJS({
          printable: 'qrCodeContainer',//页面
          type: "html",//文档类型
          maxWidth: 360,
          style: `@page {
                margin:0;
                size: 400px 75px collapse;
                margin-top:3px;
                &:first-of-type{
                  margin-top:0 !important;
                }
              }
              html{
                zoom:100%;
              }
              @media print{
                width: 400px;
                height: 75px;
                margin:0;
              }`,
          targetStyles: ["*"], // 使用dom的所有样式,很重要
          font_size: '0.20cm',
        });
      }, 300);
    }
  })
}
// 关闭合并表单
const cancel = () => {
  proxy.resetForm("formRef")
  dialogVisitable.value = false
  emit('closeDia')
}
defineExpose({ openDialog })
</script>
 
<style scoped>
.print-section {
  text-align: center;
  margin-top: 30px;
}
</style>