|
|
|
|
@ -63,6 +63,9 @@ const props = withDefaults(
|
|
|
|
|
printPaperHeight?: number
|
|
|
|
|
printMaxWidth?: number
|
|
|
|
|
printMaxHeight?: number
|
|
|
|
|
templateJsonUrl?: string
|
|
|
|
|
templateJson?: any
|
|
|
|
|
printData?: Record<string, any>
|
|
|
|
|
}>(),
|
|
|
|
|
{
|
|
|
|
|
imageUrl: '',
|
|
|
|
|
@ -82,7 +85,10 @@ const props = withDefaults(
|
|
|
|
|
printPaperWidth: 80,
|
|
|
|
|
printPaperHeight: 80,
|
|
|
|
|
printMaxWidth: 180,
|
|
|
|
|
printMaxHeight: 120
|
|
|
|
|
printMaxHeight: 120,
|
|
|
|
|
templateJsonUrl: '',
|
|
|
|
|
templateJson: undefined,
|
|
|
|
|
printData: () => ({})
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
@ -202,26 +208,88 @@ const handlePreview = () => {
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const replaceTemplateValues = (templateJson: any, printData: Record<string, any>) => {
|
|
|
|
|
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]
|
|
|
|
|
|
|
|
|
|
if (value === undefined || value === null) return element
|
|
|
|
|
|
|
|
|
|
const newOptions = { ...element.options }
|
|
|
|
|
|
|
|
|
|
if (qid === 'qrcodeUrl') {
|
|
|
|
|
newOptions.src = value
|
|
|
|
|
newOptions.testData = value
|
|
|
|
|
} else {
|
|
|
|
|
newOptions.field = qid
|
|
|
|
|
if (newOptions.testData === undefined || newOptions.testData === '') {
|
|
|
|
|
newOptions.testData = String(value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...element,
|
|
|
|
|
options: newOptions
|
|
|
|
|
}
|
|
|
|
|
}) || []
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const handlePrint = async () => {
|
|
|
|
|
if (!props.imageUrl || !props.showPrint) return
|
|
|
|
|
const printSize = await resolvePrintSize(props.imageUrl)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
let templateJson: any
|
|
|
|
|
let printData: Record<string, any>
|
|
|
|
|
|
|
|
|
|
printData = {
|
|
|
|
|
qrcodeUrl: props.imageUrl,
|
|
|
|
|
printId: props.printId === undefined || props.printId === null ? '' : String(props.printId),
|
|
|
|
|
...props.printData
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (props.templateJson) {
|
|
|
|
|
templateJson = replaceTemplateValues(props.templateJson, printData)
|
|
|
|
|
} 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)
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('获取打印模板失败', error)
|
|
|
|
|
message.error('获取打印模板失败')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const printSize = await resolvePrintSize(props.imageUrl)
|
|
|
|
|
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 paperSize = templateJson?.panels?.[0] ? {
|
|
|
|
|
width: templateJson.panels[0].width,
|
|
|
|
|
height: templateJson.panels[0].height
|
|
|
|
|
} : { width: 80, height: 80 }
|
|
|
|
|
|
|
|
|
|
hiprintPreviewDialogRef.value?.open({
|
|
|
|
|
title: props.printTitle,
|
|
|
|
|
printData: {
|
|
|
|
|
qrcodeUrl: props.imageUrl,
|
|
|
|
|
printId
|
|
|
|
|
},
|
|
|
|
|
templateJson: buildQrcodeTemplateJson(props.imageUrl, printId, imageWidth, imageHeight, paperHeight),
|
|
|
|
|
printData,
|
|
|
|
|
templateJson,
|
|
|
|
|
withDefaultQrcodeLayout: false,
|
|
|
|
|
paperSize: {
|
|
|
|
|
width: imageWidth,
|
|
|
|
|
height: paperHeight
|
|
|
|
|
}
|
|
|
|
|
paperSize
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|