<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>
|