2025-10-30 14:36:01 +08:00
|
|
|
|
<template>
|
|
|
|
|
|
<div class="label-render-container">
|
|
|
|
|
|
<!-- 操作按钮 -->
|
2025-10-30 16:36:17 +08:00
|
|
|
|
|
2025-10-30 14:36:01 +08:00
|
|
|
|
<!-- 标签预览容器 -->
|
|
|
|
|
|
<div class="preview-container" id="label-preview-container" ref="labelRef">
|
|
|
|
|
|
<ProductionTagPreview v-if="labelType === '2'" :content="content" />
|
|
|
|
|
|
<OuterTagPreview v-if="labelType === '3'" :content="content" />
|
|
|
|
|
|
<SampleTagPreview v-if="labelType === '4'" :content="content" />
|
|
|
|
|
|
<ForgeTagPreview v-if="labelType === '5'" :content="content" />
|
|
|
|
|
|
<SaltSprayTagPreview v-if="labelType === '6'" :content="content" />
|
|
|
|
|
|
</div>
|
2025-10-30 16:36:17 +08:00
|
|
|
|
<div class="action-buttons">
|
|
|
|
|
|
<el-button type="primary" @click="downloadLabelAsImage">下载标签图片</el-button>
|
|
|
|
|
|
<el-button type="primary" @click="printLabel" style="margin-left: 10px;">打印标签</el-button>
|
|
|
|
|
|
</div>
|
2025-10-30 14:36:01 +08:00
|
|
|
|
</div>
|
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
|
|
<script>
|
|
|
|
|
|
import domToImage from 'dom-to-image';
|
|
|
|
|
|
import printJS from 'print-js';
|
|
|
|
|
|
import { Message } from 'element-ui'; // 若使用Element UI,引入消息提示
|
|
|
|
|
|
|
|
|
|
|
|
import ProductionTagPreview from './ProductionTagPreview.vue';
|
|
|
|
|
|
import OuterTagPreview from './OuterTagPreview.vue';
|
|
|
|
|
|
import SampleTagPreview from './SampleTagPreview.vue';
|
|
|
|
|
|
import ForgeTagPreview from './ForgeTagPreview.vue';
|
|
|
|
|
|
import SaltSprayTagPreview from './SaltSprayTagPreview.vue';
|
|
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
name: 'LabelRender',
|
|
|
|
|
|
components: {
|
|
|
|
|
|
ProductionTagPreview,
|
|
|
|
|
|
OuterTagPreview,
|
|
|
|
|
|
SampleTagPreview,
|
|
|
|
|
|
ForgeTagPreview,
|
|
|
|
|
|
SaltSprayTagPreview,
|
|
|
|
|
|
},
|
|
|
|
|
|
props: {
|
|
|
|
|
|
labelType: {
|
|
|
|
|
|
type: String,
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
content: {
|
|
|
|
|
|
type: Object,
|
|
|
|
|
|
required: true,
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
|
|
|
|
|
methods: {
|
|
|
|
|
|
// -------- 图片下载方法 --------
|
|
|
|
|
|
async downloadLabelAsImage() {
|
|
|
|
|
|
// 1. 获取要转换的DOM容器
|
|
|
|
|
|
const labelContainer = document.getElementById('label-preview-container');
|
|
|
|
|
|
if (!labelContainer) {
|
|
|
|
|
|
Message.error('未找到标签容器,无法下载');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
// 2. 用dom-to-image生成PNG图片的DataURL
|
|
|
|
|
|
const dataUrl = await domToImage.toPng(labelContainer);
|
|
|
|
|
|
|
|
|
|
|
|
// 3. 创建临时a标签,触发下载
|
|
|
|
|
|
const downloadLink = document.createElement('a');
|
|
|
|
|
|
downloadLink.href = dataUrl;
|
|
|
|
|
|
downloadLink.download = `标签_${new Date().getTime()}.png`; // 自定义文件名(带时间戳防重复)
|
|
|
|
|
|
document.body.appendChild(downloadLink);
|
|
|
|
|
|
downloadLink.click();
|
|
|
|
|
|
document.body.removeChild(downloadLink); // 下载后移除临时标签
|
|
|
|
|
|
} catch (error) {
|
|
|
|
|
|
console.error('标签图片下载失败:', error);
|
|
|
|
|
|
Message.error('标签图片下载失败,请重试');
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
// 打印方法
|
|
|
|
|
|
printLabel() {
|
|
|
|
|
|
// 1. 获取标签容器DOM
|
|
|
|
|
|
const labelContainer = document.getElementById('label-preview-container');
|
|
|
|
|
|
if (!labelContainer) {
|
|
|
|
|
|
Message.error('未找到标签容器,无法打印');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 4. 调用print-js打印
|
|
|
|
|
|
printJS({
|
|
|
|
|
|
printable: 'label-preview-container', // 要打印的元素ID
|
|
|
|
|
|
type: 'html', // 打印类型为HTML
|
|
|
|
|
|
header: null, // 不显示页眉
|
|
|
|
|
|
footer: null, // 不显示页脚
|
|
|
|
|
|
// style: printStyles, // 注入打印样式
|
|
|
|
|
|
scanStyles: true, // 禁用自动扫描页面样式(避免冲突)
|
|
|
|
|
|
targetStyles: ['*'], // 允许所有样式生效
|
|
|
|
|
|
printContainer: true, // 打印指定容器
|
|
|
|
|
|
onError: (error) => { // 错误处理
|
|
|
|
|
|
console.error('打印失败:', error);
|
|
|
|
|
|
Message.error('打印失败,请重试');
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
};
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style scoped>
|
|
|
|
|
|
.label-render-container {
|
|
|
|
|
|
width: 100%;
|
|
|
|
|
|
display: flex;
|
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
|
/* 按钮与预览垂直排列 */
|
|
|
|
|
|
justify-content: flex-start;
|
|
|
|
|
|
align-items: center;
|
2025-10-30 16:36:17 +08:00
|
|
|
|
padding: 1rem;
|
2025-10-30 14:36:01 +08:00
|
|
|
|
box-sizing: border-box;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.action-buttons {
|
2025-10-30 16:36:17 +08:00
|
|
|
|
margin-top: 1rem;
|
2025-10-30 14:36:01 +08:00
|
|
|
|
/* 按钮与预览区的间距 */
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.preview-container {
|
|
|
|
|
|
background-color: #fff;
|
|
|
|
|
|
color: #000;
|
|
|
|
|
|
padding: 1.5rem;
|
|
|
|
|
|
width: fit-content;
|
|
|
|
|
|
min-width: 200px;
|
|
|
|
|
|
border-radius: 6px;
|
|
|
|
|
|
box-shadow: 0 0 60px rgba(255, 255, 255, 0.2);
|
|
|
|
|
|
border: 1px solid rgba(255, 255, 255, 0.1);
|
|
|
|
|
|
overflow: hidden;
|
|
|
|
|
|
transition: all 0.3s ease;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
.preview-container:hover {
|
|
|
|
|
|
box-shadow: 0 0 80px rgba(255, 255, 255, 0.3);
|
|
|
|
|
|
transform: translateY(-2px);
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|