|
|
|
|
@ -39,6 +39,7 @@ import request from '@/config/axios'
|
|
|
|
|
import HiprintPreviewDialog from '@/components/HiprintPreviewDialog/index.vue'
|
|
|
|
|
import { createImageViewer } from '@/components/ImageViewer'
|
|
|
|
|
import { ElLoading } from 'element-plus'
|
|
|
|
|
import { getSimpleDictDataList } from '@/api/system/dict/dict.data'
|
|
|
|
|
|
|
|
|
|
const { t } = useI18n()
|
|
|
|
|
const message = useMessage()
|
|
|
|
|
@ -66,6 +67,7 @@ const props = withDefaults(
|
|
|
|
|
templateJsonUrl?: string
|
|
|
|
|
templateJson?: any
|
|
|
|
|
printData?: Record<string, any>
|
|
|
|
|
printTemplateType?: string | number
|
|
|
|
|
}>(),
|
|
|
|
|
{
|
|
|
|
|
imageUrl: '',
|
|
|
|
|
@ -88,7 +90,8 @@ const props = withDefaults(
|
|
|
|
|
printMaxHeight: 120,
|
|
|
|
|
templateJsonUrl: '',
|
|
|
|
|
templateJson: undefined,
|
|
|
|
|
printData: () => ({})
|
|
|
|
|
printData: () => ({}),
|
|
|
|
|
printTemplateType: undefined
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@ -99,14 +102,49 @@ const emit = defineEmits<{
|
|
|
|
|
const hiprintPreviewDialogRef = ref()
|
|
|
|
|
const refreshLoading = ref(false)
|
|
|
|
|
|
|
|
|
|
type TemplateFieldMap = {
|
|
|
|
|
qrcodeField?: string
|
|
|
|
|
nameField?: string
|
|
|
|
|
codeField?: string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let printTemplateDictCache: any[] | undefined
|
|
|
|
|
|
|
|
|
|
const showActionMask = computed(() => props.showPreview || props.showPrint || props.showRefresh)
|
|
|
|
|
|
|
|
|
|
const parseTemplateFieldMap = (remark: any): TemplateFieldMap | undefined => {
|
|
|
|
|
const parts = String(remark || '')
|
|
|
|
|
.split(',')
|
|
|
|
|
.map((item) => item.trim())
|
|
|
|
|
.filter(Boolean)
|
|
|
|
|
if (!parts.length) return undefined
|
|
|
|
|
return {
|
|
|
|
|
qrcodeField: parts[0],
|
|
|
|
|
nameField: parts[1],
|
|
|
|
|
codeField: parts[2] || parts[1]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getTemplateFieldMap = async (): Promise<TemplateFieldMap | undefined> => {
|
|
|
|
|
if (props.printTemplateType === undefined || props.printTemplateType === null || props.printTemplateType === '') {
|
|
|
|
|
return undefined
|
|
|
|
|
}
|
|
|
|
|
if (!printTemplateDictCache) {
|
|
|
|
|
printTemplateDictCache = await getSimpleDictDataList()
|
|
|
|
|
}
|
|
|
|
|
const matched = (printTemplateDictCache || []).find(
|
|
|
|
|
(item: any) => item.dictType === 'print_template_type' && String(item.value) === String(props.printTemplateType)
|
|
|
|
|
)
|
|
|
|
|
return parseTemplateFieldMap(matched?.remark)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const buildQrcodeTemplateJson = (
|
|
|
|
|
qrcodeUrl: string,
|
|
|
|
|
printId: string,
|
|
|
|
|
printData: Record<string, any>,
|
|
|
|
|
imageWidth: number,
|
|
|
|
|
imageHeight: number,
|
|
|
|
|
paperHeight: number
|
|
|
|
|
paperHeight: number,
|
|
|
|
|
fieldMap?: TemplateFieldMap
|
|
|
|
|
) => ({
|
|
|
|
|
panels: [
|
|
|
|
|
{
|
|
|
|
|
@ -124,7 +162,7 @@ const buildQrcodeTemplateJson = (
|
|
|
|
|
width: imageWidth,
|
|
|
|
|
height: imageHeight,
|
|
|
|
|
src: qrcodeUrl,
|
|
|
|
|
field: 'qrcodeUrl',
|
|
|
|
|
field: fieldMap?.qrcodeField || 'qrcodeUrl',
|
|
|
|
|
title: '二维码图片',
|
|
|
|
|
testData: qrcodeUrl
|
|
|
|
|
},
|
|
|
|
|
@ -133,23 +171,7 @@ const buildQrcodeTemplateJson = (
|
|
|
|
|
type: 'image'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
options: {
|
|
|
|
|
left: 0,
|
|
|
|
|
top: imageHeight + 2,
|
|
|
|
|
height: 10,
|
|
|
|
|
width: imageWidth,
|
|
|
|
|
testData: printId,
|
|
|
|
|
title: 'ID',
|
|
|
|
|
field: 'printId',
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
fontSize: 10
|
|
|
|
|
},
|
|
|
|
|
printElementType: {
|
|
|
|
|
title: '文本',
|
|
|
|
|
type: 'text'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
...buildDefaultTextElements(printData, imageWidth, imageHeight, fieldMap)
|
|
|
|
|
],
|
|
|
|
|
paperNumberDisabled: true,
|
|
|
|
|
paperNumberContinue: true,
|
|
|
|
|
@ -159,6 +181,60 @@ const buildQrcodeTemplateJson = (
|
|
|
|
|
]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
const buildDefaultTextElements = (
|
|
|
|
|
printData: Record<string, any>,
|
|
|
|
|
imageWidth: number,
|
|
|
|
|
imageHeight: number,
|
|
|
|
|
fieldMap?: TemplateFieldMap
|
|
|
|
|
) => {
|
|
|
|
|
const fields = [
|
|
|
|
|
{ title: '名称', field: fieldMap?.nameField },
|
|
|
|
|
{ title: '编码', field: fieldMap?.codeField }
|
|
|
|
|
].filter((item) => item.field)
|
|
|
|
|
|
|
|
|
|
if (!fields.length) {
|
|
|
|
|
const printId = printData.printId === undefined || printData.printId === null ? '' : String(printData.printId)
|
|
|
|
|
if (!printId) return []
|
|
|
|
|
return [
|
|
|
|
|
{
|
|
|
|
|
options: {
|
|
|
|
|
left: 0,
|
|
|
|
|
top: imageHeight + 2,
|
|
|
|
|
height: 10,
|
|
|
|
|
width: imageWidth,
|
|
|
|
|
testData: printId,
|
|
|
|
|
title: 'ID',
|
|
|
|
|
field: 'printId',
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
fontSize: 10
|
|
|
|
|
},
|
|
|
|
|
printElementType: {
|
|
|
|
|
title: '文本',
|
|
|
|
|
type: 'text'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fields.map((item, index) => ({
|
|
|
|
|
options: {
|
|
|
|
|
left: 0,
|
|
|
|
|
top: imageHeight + 2 + index * 12,
|
|
|
|
|
height: 10,
|
|
|
|
|
width: imageWidth,
|
|
|
|
|
testData: printData[item.field!] === undefined || printData[item.field!] === null ? '' : String(printData[item.field!]),
|
|
|
|
|
title: item.title,
|
|
|
|
|
field: item.field,
|
|
|
|
|
textAlign: 'center',
|
|
|
|
|
fontSize: 10
|
|
|
|
|
},
|
|
|
|
|
printElementType: {
|
|
|
|
|
title: '文本',
|
|
|
|
|
type: 'text'
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const getImageSize = (src: string) =>
|
|
|
|
|
new Promise<{ width: number; height: number }>((resolve, reject) => {
|
|
|
|
|
const img = new Image()
|
|
|
|
|
@ -208,33 +284,44 @@ const handlePreview = () => {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const replaceTemplateValues = (templateJson: any, printData: Record<string, any>) => {
|
|
|
|
|
const resolveElementFieldKey = (element: any, fieldMap?: TemplateFieldMap) => {
|
|
|
|
|
const fieldKey = typeof element?.options?.field === 'string' ? element.options.field.trim() : ''
|
|
|
|
|
const qidKey = typeof element?.options?.qid === 'string' ? element.options.qid.trim() : ''
|
|
|
|
|
const title = String(element?.options?.title || element?.printElementType?.title || '').trim()
|
|
|
|
|
|
|
|
|
|
if (fieldMap) {
|
|
|
|
|
if (title.includes('二维码')) return fieldMap.qrcodeField || fieldKey || qidKey
|
|
|
|
|
if (title.includes('名称')) return fieldMap.nameField || fieldKey || qidKey
|
|
|
|
|
if (title.includes('编码') || title.includes('条码') || title.includes('文本')) return fieldMap.codeField || fieldKey || qidKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fieldKey || qidKey
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const replaceTemplateValues = (templateJson: any, printData: Record<string, any>, fieldMap?: TemplateFieldMap) => {
|
|
|
|
|
if (!templateJson?.panels) return templateJson
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...templateJson,
|
|
|
|
|
panels: templateJson.panels.map((panel: any) => ({
|
|
|
|
|
...panel,
|
|
|
|
|
printElements: panel.printElements?.map((element: any) => {
|
|
|
|
|
if (!element?.options?.qid) return element
|
|
|
|
|
|
|
|
|
|
const qid = element.options.qid
|
|
|
|
|
const value = printData[qid]
|
|
|
|
|
|
|
|
|
|
const valueKey = resolveElementFieldKey(element, fieldMap)
|
|
|
|
|
if (!valueKey) return element
|
|
|
|
|
|
|
|
|
|
const value = printData[valueKey]
|
|
|
|
|
if (value === undefined || value === null) return element
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const newOptions = { ...element.options }
|
|
|
|
|
|
|
|
|
|
if (qid === 'qrcodeUrl') {
|
|
|
|
|
|
|
|
|
|
if (element?.printElementType?.type === 'image') {
|
|
|
|
|
newOptions.src = value
|
|
|
|
|
newOptions.testData = value
|
|
|
|
|
} else {
|
|
|
|
|
newOptions.field = qid
|
|
|
|
|
if (newOptions.testData === undefined || newOptions.testData === '') {
|
|
|
|
|
newOptions.testData = String(value)
|
|
|
|
|
}
|
|
|
|
|
newOptions.field = valueKey
|
|
|
|
|
newOptions.testData = String(value)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...element,
|
|
|
|
|
options: newOptions
|
|
|
|
|
@ -249,6 +336,7 @@ const handlePrint = async () => {
|
|
|
|
|
|
|
|
|
|
let templateJson: any
|
|
|
|
|
let printData: Record<string, any>
|
|
|
|
|
const templateFieldMap = await getTemplateFieldMap()
|
|
|
|
|
|
|
|
|
|
printData = {
|
|
|
|
|
qrcodeUrl: props.imageUrl,
|
|
|
|
|
@ -257,12 +345,12 @@ const handlePrint = async () => {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (props.templateJson) {
|
|
|
|
|
templateJson = replaceTemplateValues(props.templateJson, printData)
|
|
|
|
|
templateJson = replaceTemplateValues(props.templateJson, printData, templateFieldMap)
|
|
|
|
|
} else if (props.templateJsonUrl) {
|
|
|
|
|
try {
|
|
|
|
|
const response = await request.get({ url: props.templateJsonUrl })
|
|
|
|
|
templateJson = typeof response.data === 'string' ? JSON.parse(response.data) : response.data
|
|
|
|
|
templateJson = replaceTemplateValues(templateJson, printData)
|
|
|
|
|
templateJson = replaceTemplateValues(templateJson, printData, templateFieldMap)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取打印模板失败', error)
|
|
|
|
|
message.error('获取打印模板失败')
|
|
|
|
|
@ -273,10 +361,11 @@ const handlePrint = async () => {
|
|
|
|
|
const imageWidth = printSize.width
|
|
|
|
|
const imageHeight = printSize.height
|
|
|
|
|
const printId = props.printId === undefined || props.printId === null ? '' : String(props.printId)
|
|
|
|
|
const idAreaHeight = printId ? 14 : 0
|
|
|
|
|
const paperHeight = imageHeight + idAreaHeight
|
|
|
|
|
|
|
|
|
|
templateJson = buildQrcodeTemplateJson(props.imageUrl, printId, imageWidth, imageHeight, paperHeight)
|
|
|
|
|
const hasMappedFields = Boolean(templateFieldMap?.nameField || templateFieldMap?.codeField)
|
|
|
|
|
const textLineCount = hasMappedFields ? [templateFieldMap?.nameField, templateFieldMap?.codeField].filter(Boolean).length : (printId ? 1 : 0)
|
|
|
|
|
const paperHeight = imageHeight + textLineCount * 12 + (textLineCount ? 4 : 0)
|
|
|
|
|
|
|
|
|
|
templateJson = buildQrcodeTemplateJson(props.imageUrl, printData, imageWidth, imageHeight, paperHeight, templateFieldMap)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const paperSize = templateJson?.panels?.[0] ? {
|
|
|
|
|
|