UNPKG

fastlion-amis

Version:

一种MIS页面生成工具

842 lines (785 loc) 38.7 kB
import { decode } from "js-base64" import { PrinterParam, TableColumn, ColumnInfo } from "../../renderers/Lion/LabelPrint/types" import { FormTemplate, LabelNumber, LabelTemplate, LabelPageParam, LabelCodeType, LabelPrintStyleParam, LabelText, LabelBarCode, LabelEllipse, LabelHtml, LabelLine, LabelRect, LabelShape, LabelTable, LabelImage, Rect, DetailTemplate, CodeRect, StyleRect, } from "./type" import moment from 'moment' import { DetailOptionValues, DetailSettingValues, PrintContent } from "../../renderers/Lion/LabelPrint/components/detail/types" import { ReportOptionValues, ReportSettingValues } from "../../renderers/Lion/LabelPrint/components/report/types" export const buildDetailTemplate = (settings: DetailSettingValues, options: DetailOptionValues, printContent: PrintContent, index: number, numberOfPage: number, batched: boolean): DetailTemplate => { const { width, height, direction, top, bottom, left: defaultLeft, right: defaultRight } = settings const { showHeader, showFooter, showHeaderLine, headerTitle, showFooterLine, titleAllPrint, subTitleAllPrint, showLogo, headerFontSize, headerFontFontFamily, title, showTitle, showDynamicSubTitles, dynamicSubTiltes, titleFontSize, titleFontFamily, subTitleFontSize, subTitleFontFamily, footerFontSize, footerFontFamily, showPrinter, showDate, showPageNum, showBarCode, showQRCode, } = options const { subTitle, extraTitle, codeLabel, codeValue, htmlContent, userName } = printContent const pageWidth = direction == 'vertical' ? width : height const pageHeight = ((direction == 'vertical' ? height : width)) / numberOfPage const headerHeight = showHeader ? 16 : 0 const codeWidth = (showBarCode || showQRCode) ? showBarCode ? 60 : 15 : 0 const codeHeight = (showBarCode || showQRCode) ? showBarCode ? 13 : 15 : 0 const titleHeight = showTitle ? titleFontSize / 3 : 0 const headerTitleHeight = showHeader ? headerFontSize / 3.33333 : 0 const titleMarginTop = showHeader ? 3 : 0 const maxSubtitleRow = Math.max(...dynamicSubTiltes.map(item => item.split('\r').length), 1) const subTitleHeight = showDynamicSubTitles ? (subTitleFontSize / 3 * maxSubtitleRow) + (maxSubtitleRow - 1) * 0.5 : 0 const subTitleMarginTop = showDynamicSubTitles ? 3 : 0 const subTitleCount = dynamicSubTiltes.length const htmlMarginTop = 1.3 const htmlMarginBottom = 8 const footerHeight = footerFontSize / 3.33333 const pageNumWidth = footerFontSize const rect: Rect = { top: pageHeight * index + top, left: defaultLeft, width: pageWidth - defaultLeft - defaultRight, height: pageHeight - top - bottom } const headerRect: Rect = { top: rect.top, left: rect.left, width: rect.width, height: headerHeight } const logoRect: Rect = { top: headerRect.top, left: headerRect.left, width: showLogo ? headerRect.height : 0, height: showLogo ? headerRect.height : 0, content: showLogo ? `<img src="./public/images/sanfu.png"/>` : '' } const headerTitleRect: StyleRect = { top: headerRect.top + (headerRect.height / 2 - 2), left: headerRect.left + logoRect.width + 5, width: headerRect.width - logoRect.width - 5, height: headerTitleHeight, content: showHeader ? headerTitle : '', itemType: 1, fontSize: headerFontSize * 3 / 4, fontFamily: headerFontFontFamily } const headerLineRect: Rect | undefined = showHeader && showHeaderLine ? { top: headerRect.top + headerRect.height, left: rect.left, width: rect.width, height: 0 } : undefined const codeRect: CodeRect = { top: headerRect.top, left: headerRect.width + headerRect.left - codeWidth, width: codeWidth, height: codeHeight, label: '', codeType: showQRCode ? 'QRCode' : 'code39', content: (showBarCode || showQRCode) ? codeValue : undefined } const codeLabelRect: Rect = { top: headerRect.top, left: headerRect.left, width: headerRect.width - codeWidth - 1, height: 10, content: `${codeLabel}:${codeValue}` } const extraTitleRect: Rect = { top: headerRect.top, left: headerRect.left + logoRect.width, width: headerRect.width - logoRect.width, height: headerRect.height, content: extraTitle } const titleRect: StyleRect = { top: headerRect.top + headerRect.height + titleMarginTop + (showHeader ? 0 : 5), left: rect.left, width: rect.width, height: titleHeight, content: showTitle ? title + (subTitle ? `[${subTitle}]` : '') : '', itemType: titleAllPrint && !batched ? 1 : 0, fontSize: titleFontSize * 3 / 4, fontFamily: titleFontFamily } const subTitleRect: Rect = { top: titleRect.top + titleRect.height + subTitleMarginTop, left: defaultLeft, width: rect.width, height: subTitleHeight } const subTitleRect1: StyleRect = { top: subTitleRect.top, left: subTitleRect.left, width: subTitleRect.width / (subTitleCount || 1), height: subTitleRect.height, content: showDynamicSubTitles ? dynamicSubTiltes[0] : undefined, itemType: subTitleAllPrint && !batched ? 1 : 0, fontSize: subTitleFontSize * 3 / 4, fontFamily: subTitleFontFamily } const subTitleRect2: StyleRect = { top: subTitleRect.top, left: subTitleCount <= 1 ? 0 : subTitleRect.left + (subTitleRect.width / subTitleCount), width: subTitleCount <= 1 ? 0 : subTitleRect.width / (subTitleCount || 1), height: subTitleRect.height, content: showDynamicSubTitles ? dynamicSubTiltes[1] : undefined, itemType: subTitleAllPrint && !batched ? 1 : 0, fontSize: subTitleFontSize * 3 / 4, fontFamily: subTitleFontFamily } const subTitleRect3: StyleRect = { top: subTitleRect.top, left: subTitleCount <= 2 ? 0 : subTitleRect.left + (subTitleRect.width / subTitleCount * (subTitleCount - 1)), width: subTitleCount <= 2 ? 0 : subTitleRect.width / (subTitleCount || 1), height: subTitleRect.height, content: showDynamicSubTitles ? dynamicSubTiltes[2] : undefined, itemType: subTitleAllPrint && !batched ? 1 : 0, fontSize: subTitleFontSize * 3 / 4, fontFamily: subTitleFontFamily } const footerRect: Rect = { top: rect.height + rect.top - footerHeight, left: rect.left, width: rect.width, height: footerHeight > 0 ? footerHeight : 0 } const footerLineRect: Rect | undefined = showFooter && showFooterLine ? { top: rect.height + rect.top - footerHeight - 2, left: rect.left, width: rect.width, height: 0 } : undefined const printerRect: StyleRect = { top: footerRect.top, left: rect.left, width: footerRect.width * 0.3, height: showPrinter && showFooter ? footerRect.height : 0, content: showFooter && showPrinter ? `打印者:${userName}` : '', itemType: 1, fontSize: footerFontSize * 3 / 4, fontFamily: footerFontFamily } const dateRect: StyleRect = { top: footerRect.top, left: footerRect.left + printerRect.width, width: footerRect.width * 0.4, height: showDate && showFooter ? footerRect.height : 0, content: showFooter && showDate ? `打印时间:${moment(new Date()).format('YYYY-MM-DD HH:mm:ss')}` : '', itemType: 1, fontSize: footerFontSize * 3 / 4, fontFamily: footerFontFamily } const numRect: StyleRect = { top: footerRect.top, left: footerRect.left + printerRect.width + dateRect.width + (footerRect.width * 0.3 - pageNumWidth * 2) - 2, width: pageNumWidth, height: showFooter ? footerRect.height : 0, content: showFooter && showPageNum ? '第#页' : '', itemType: showFooter && showPageNum ? 2 : 0, fontSize: footerFontSize * 3 / 4, fontFamily: footerFontFamily } const sumRect: StyleRect = { top: footerRect.top, left: numRect.left + numRect.width + 1, width: pageNumWidth, height: showFooter ? footerRect.height : 0, content: showFooter && showPageNum ? '共#页' : '', itemType: showFooter && showPageNum ? 3 : 0, fontSize: footerFontSize * 3 / 4, fontFamily: footerFontFamily } const htmlRect: Rect = { top: subTitleRect.top + subTitleRect.height + htmlMarginTop, left: rect.left, width: rect.width, height: footerRect.top - (subTitleRect.top + subTitleRect.height + htmlMarginTop) - htmlMarginBottom + (showFooter ? 0 : 6), content: htmlContent, } const divider: Rect | undefined = index > 0 ? { top: index * ((direction == 'vertical' ? height : width) / numberOfPage), left: 0, width: pageWidth, height: 0 } : undefined return { divider, header: headerRect, headerLine: headerLineRect, logo: logoRect, headerTitle: headerTitleRect, code: codeRect, codeLabel: codeLabelRect, extraTitle: extraTitleRect, title: titleRect, subTitle: subTitleRect, subTitle1: subTitleRect1, subTitle2: subTitleRect2, subTitle3: subTitleRect3, html: htmlRect, date: dateRect, printer: printerRect, footer: footerRect, footerLine: footerLineRect, num: numRect, sum: sumRect } } export const buildFormTemplate = (settings: ReportSettingValues, options: ReportOptionValues): FormTemplate => { const userInfo = sessionStorage.getItem('userInfo') ?? '{}' const userObj = userInfo && JSON.parse(userInfo) const { width, height, direction, top: defaultTop, bottom: defaultBottom, left: defaultLeft, right: defaultRight } = settings const { showHeaderTitle, headerTitle, showHeaderLine, showLogo, showTitle, titleFontSize, titleFontFamily, headerFontSize, headerFontFamily, titleAllPrint, title, showSubTitle, subTitleAllPrint, subTitle = [], subTitleFontSize, subTitleFontFamily, footerFontSize, footerFontFamily, showFooterLine, showFooterTitle, footerTitle, showPrinter, showDate, showPageNum } = options const pageWidth = direction == 'vertical' ? width : height const pageHeight = direction == 'vertical' ? height : width const headerHeight = (showLogo || showHeaderTitle || showHeaderLine) ? 18 : 0 const titleHeight = showTitle ? titleFontSize / 3 : 0 const maxSubtitleRow = Math.max(...subTitle.map(item => item.split('\r').length), 1) const subTitleHeight = showSubTitle ? (subTitleFontSize / 3 * maxSubtitleRow) + (maxSubtitleRow - 1) * 0.5 : 0 const subTitleMarginTop = showSubTitle ? 3 : 0 const subTitleCount = subTitle.length const footerHeight = footerFontSize / 3.33333 const pageNumWidth = footerFontSize const headerRect: Rect = { top: defaultTop, left: defaultLeft, width: pageWidth - defaultLeft - defaultRight, height: headerHeight } const headerLineRect = showHeaderLine ? { top: headerRect.top + headerRect.height + 2, left: headerRect.left, width: headerRect.width, height: 0 } : undefined const logoRect: Rect = { top: headerRect.top, left: headerRect.left, width: showLogo ? headerRect.height : 0, height: showLogo ? headerRect.height : 0, content: showLogo ? `<img src="./public/images/sanfu.png"/>` : '' } const headerTitleRect: StyleRect = { top: headerRect.top + (headerRect.height / 2 - 2), left: headerRect.left + logoRect.width + 5, width: headerRect.width - logoRect.width - 5, height: titleHeight, content: showHeaderTitle ? headerTitle : '', itemType: 1, fontSize: headerFontSize * 3 / 4, fontFamily: headerFontFamily } const footerRect: Rect = { top: pageHeight - defaultBottom - footerHeight, left: defaultLeft, width: pageWidth - defaultLeft - defaultRight, height: footerHeight } const footerTitleRect: StyleRect = { top: footerRect.top - footerHeight, left: footerRect.left, width: footerRect.width, height: showFooterTitle ? footerHeight : 0, content: showFooterTitle ? footerTitle : '', itemType: 1, fontSize: footerFontSize * 3 / 4, fontFamily: footerFontFamily } const footerLineRect = showFooterLine ? { top: footerRect.top, left: footerRect.left, width: footerRect.width, height: 0 } : undefined const printerRect: StyleRect = { top: footerRect.top + 2, left: footerRect.left, width: footerRect.width * 0.3, height: footerRect.height, content: showPrinter ? `打印者:${userObj.user_name ?? ''}` : '', itemType: 1, fontSize: footerFontSize * 3 / 4, fontFamily: footerFontFamily } const dateRect: StyleRect = { top: footerRect.top + 2, left: footerRect.left + printerRect.width, width: footerRect.width * 0.4, height: footerRect.height, content: showDate ? `打印时间:${moment(new Date()).format('YYYY-MM-DD HH:mm:ss')}` : '', itemType: 1, fontSize: footerFontSize * 3 / 4, fontFamily: footerFontFamily } const numRect: StyleRect = { top: footerRect.top + 2, left: footerRect.left + printerRect.width + dateRect.width + (footerRect.width * 0.3 - pageNumWidth * 2) - 2, width: pageNumWidth, height: footerRect.height, content: showPageNum ? '第#页' : '', itemType: showPageNum ? 2 : 0, fontSize: footerFontSize * 3 / 4, fontFamily: footerFontFamily } const sumRect: StyleRect = { top: footerRect.top + 2, left: numRect.left + numRect.width + 1, width: pageNumWidth, height: footerRect.height, content: showPageNum ? '共#页' : '', itemType: showPageNum ? 3 : 0, fontSize: footerFontSize * 3 / 4, fontFamily: footerFontFamily } const bodyRect: Rect = { top: 5 + headerRect.top + headerRect.height, left: defaultLeft, width: pageWidth - defaultLeft - defaultRight, height: pageHeight - (headerRect.top + headerRect.height + 3) - (footerRect.height + defaultBottom + 3) - (showFooterTitle ? footerHeight : 0) } const titleRect: StyleRect = { top: bodyRect.top, left: bodyRect.left, width: bodyRect.width, height: titleHeight, content: showTitle ? title : '', itemType: titleAllPrint ? 1 : 0, fontSize: titleFontSize * 3 / 4, fontFamily: titleFontFamily, } const subTitleRect: Rect = { top: titleRect.top + titleRect.height + subTitleMarginTop, left: bodyRect.left, width: bodyRect.width, height: subTitleHeight } const subTitleRect1: StyleRect = { top: subTitleRect.top, left: subTitleRect.left, width: subTitleRect.width / (subTitleCount || 1), height: subTitleCount > 0 ? subTitleRect.height : 0, content: showSubTitle ? subTitle[0] : undefined, itemType: subTitleAllPrint ? 1 : 0, fontSize: subTitleFontSize * 3 / 4, fontFamily: subTitleFontFamily } const subTitleRect2: StyleRect = { top: subTitleRect.top, left: subTitleCount <= 1 ? 0 : subTitleRect.left + (subTitleRect.width / subTitleCount), width: subTitleCount <= 1 ? 0 : subTitleRect.width / (subTitleCount || 1), height: subTitleCount > 0 ? subTitleRect.height : 0, content: showSubTitle ? subTitle[1] : undefined, itemType: subTitleAllPrint ? 1 : 0, fontSize: subTitleFontSize * 3 / 4, fontFamily: subTitleFontFamily } const subTitleRect3: StyleRect = { top: subTitleRect.top, left: subTitleCount <= 2 ? 0 : subTitleRect.left + (subTitleRect.width / subTitleCount * (subTitleCount - 1)), width: subTitleCount <= 2 ? 0 : subTitleRect.width / (subTitleCount || 1), height: subTitleCount > 0 ? subTitleRect.height : 0, content: showSubTitle ? subTitle[2] : undefined, itemType: subTitleAllPrint ? 1 : 0, fontSize: subTitleFontSize * 3 / 4, fontFamily: subTitleFontFamily } const tableRect: Rect = { top: subTitleRect.top + subTitleRect1.height + 3, left: bodyRect.left, width: bodyRect.width, height: bodyRect.height - titleRect.height - subTitleRect.height - subTitleMarginTop - 9 } return { header: headerRect, logo: logoRect, headerLine: headerLineRect, headerTitle: headerTitleRect, body: bodyRect, title: titleRect, subTitle: subTitleRect, subTitle1: subTitleRect1, subTitle2: subTitleRect2, subTitle3: subTitleRect3, table: tableRect, footer: footerRect, footerLine: footerLineRect, footerTitle: footerTitleRect, printer: printerRect, date: dateRect, num: numRect, sum: sumRect } } // 计算每行放的个最大数 export const getMaxCount = (templateWidth: number, pageWidth: number, spacing: number): number => { let count = 1 while ((count * templateWidth + (count - 1) * spacing) <= pageWidth) { count++ if (count > 50) { break } } return (count - 1) || 1 } // 解析base64内容构造标签模板 export const buildLabelTemplate = (templateContent: string): LabelTemplate => { const getParams = (dataStr: string): string[] => { const start = dataStr.indexOf('(') + 1 const end = dataStr.lastIndexOf(')') const str = dataStr.substring(start, end) const params = str.split(',') return params.map(value => { const left = value.startsWith('"') ? value.substring(1) : value const right = left.endsWith('"') ? left.substring(0, left.length - 1) : left return right.replaceAll('\\"', '"').replaceAll('\\r', '').replaceAll('\\n', '') }) } const getStyleParams = (dataStr: string): LabelPrintStyleParam => { const isNumber = (value: string): boolean => { return /^[0-9]+.?[0-9]*/.test(value) } const params = getParams(dataStr) const id = isNumber(params[0]) ? Number(params[0]) : params[0] const name = params[1] const value = isNumber(params[2]) ? Number(params[2]) : params[2] return [id, name, value] } const labelTemplate: LabelTemplate = { tempTop: new LabelNumber(0, 297), tempLeft: new LabelNumber(0, 210), tempWidth: new LabelNumber(0, 210), tempHeight: new LabelNumber(0, 297), labelTexts: [], labelHtmls: [], labelImages: [], labelTables: [], labelBarCodes: [], labelRects: [], labelEllipses: [], labelShapes: [], labelLines: [] } const value = decode(templateContent) const tempDatas = value.split('LODOP') const exprs = [LabelCodeType.ADD_RECT, LabelCodeType.ADD_ELLIPSE, LabelCodeType.ADD_LINE] const initData = tempDatas.find(data => data.includes(LabelCodeType.INITA))! const initParams = getParams(initData) const tempWidth = new LabelNumber(initParams[2]) const tempHeight = new LabelNumber(initParams[3]) labelTemplate.tempTop = new LabelNumber(initParams[0]) labelTemplate.tempLeft = new LabelNumber(initParams[1]) labelTemplate.tempWidth = tempWidth labelTemplate.tempHeight = tempHeight for (let i = 0; i < tempDatas.length; i++) { const data = tempDatas[i] let j = i + 1 const params = getParams(data) // 添加文本 if (data.includes(LabelCodeType.ADD_TEXT)) { const styles: LabelPrintStyleParam[] = [] while (tempDatas[j] && tempDatas[j].includes(LabelCodeType.SET_STYLE)) { const param = getStyleParams(tempDatas[j]) styles.push(param) j++ } const labelText: LabelText = data.includes(LabelCodeType.ADD_TEXTA) ? { fieldName: params[0], top: new LabelNumber(params[1], tempHeight.number), left: new LabelNumber(params[2], tempWidth.number), width: new LabelNumber(params[3], tempWidth.number), height: new LabelNumber(params[4], tempHeight.number), content: params[5], styles } : { top: new LabelNumber(params[0], tempHeight.number), left: new LabelNumber(params[1], tempWidth.number), width: new LabelNumber(params[2], tempWidth.number), height: new LabelNumber(params[3], tempHeight.number), content: params[4], styles } labelTemplate.labelTexts.push(labelText) } // 添加富文本和图片 if (data.includes(LabelCodeType.ADD_HTML)) { const styles: LabelPrintStyleParam[] = [] while (tempDatas[j] && tempDatas[j].includes(LabelCodeType.SET_STYLE)) { const param = getStyleParams(tempDatas[j]) styles.push(param) j++ } const fieldName = styles.find(([_id, name]) => name === 'ItemName')?.[2] as string | undefined const labelHtml: LabelHtml = { top: new LabelNumber(params[0], tempHeight.number), left: new LabelNumber(params[1], tempWidth.number), width: new LabelNumber(params[2], tempWidth.number), height: new LabelNumber(params[3], tempHeight.number), content: params[4], fieldName, styles } labelTemplate.labelHtmls.push(labelHtml) } // 添加图片 if (data.includes(LabelCodeType.ADD_IMAGE)) { const styles: LabelPrintStyleParam[] = [] while (tempDatas[j] && tempDatas[j].includes(LabelCodeType.SET_STYLE)) { const param = getStyleParams(tempDatas[j]) styles.push(param) j++ } const fieldName = styles.find(([_id, name]) => name === 'ItemName')?.[2] as string | undefined const labelImage: LabelImage = { top: new LabelNumber(params[0], tempHeight.number), left: new LabelNumber(params[1], tempWidth.number), width: new LabelNumber(params[2], tempWidth.number), height: new LabelNumber(params[3], tempHeight.number), url: params.slice(4).join(), fieldName, styles } labelTemplate.labelImages.push(labelImage) } // 添加表格 if (data.includes(LabelCodeType.ADD_TABLE)) { const styles: LabelPrintStyleParam[] = [] while (tempDatas[j] && tempDatas[j].includes(LabelCodeType.SET_STYLE)) { const param = getStyleParams(tempDatas[j]) styles.push(param) j++ } const fieldName = styles.find(([_id, name]) => name === 'ItemName')?.[2] as string | undefined const labelTable: LabelTable = { top: new LabelNumber(params[0], tempHeight.number), left: new LabelNumber(params[1], tempWidth.number), width: new LabelNumber(params[2], tempWidth.number), height: new LabelNumber(params[3], tempHeight.number), content: params.slice(4).join(), fieldName, styles } labelTemplate.labelTables.push(labelTable) } // 添加条码 if (data.includes(LabelCodeType.ADD_BARCODE)) { const styles: LabelPrintStyleParam[] = [] while (tempDatas[j] && tempDatas[j].includes(LabelCodeType.SET_STYLE)) { const param = getStyleParams(tempDatas[j]) styles.push(param) j++ } const labelBarCode: LabelBarCode = data.includes(LabelCodeType.ADD_BARCODEA) ? { fieldName: params[0], top: new LabelNumber(params[1], tempHeight.number), left: new LabelNumber(params[2], tempWidth.number), width: new LabelNumber(params[3], tempWidth.number), height: new LabelNumber(params[4], tempHeight.number), codeType: params[5], codeValue: params[6], styles } : { top: new LabelNumber(params[0], tempHeight.number), left: new LabelNumber(params[1], tempWidth.number), width: new LabelNumber(params[2], tempWidth.number), height: new LabelNumber(params[3], tempHeight.number), codeType: params[4], codeValue: params[5], styles } labelTemplate.labelBarCodes.push(labelBarCode) } // 添加其它图形 if (data.includes(LabelCodeType.ADD_SHAPE)) { const labelShape: LabelShape = { shapeType: Number(params[0]), top: new LabelNumber(params[1], tempHeight.number), left: new LabelNumber(params[2], tempWidth.number), width: new LabelNumber(params[3], tempWidth.number), height: new LabelNumber(params[4], tempHeight.number), lineStyle: Number(params[5]), lineWidth: Number(params[6]), fillColor: params[7] } labelTemplate.labelShapes.push(labelShape) } // 添加图形 const expr = exprs.find(expr => data.includes(expr)) if (expr) { const label: LabelLine = { top: new LabelNumber(params[0], tempHeight.number), left: new LabelNumber(params[1], tempWidth.number), width: new LabelNumber(params[2], tempWidth.number), height: new LabelNumber(params[3], tempHeight.number), lineStyle: Number(params[4]), lineWidth: Number(params[5]) } switch (expr) { case LabelCodeType.ADD_RECT: labelTemplate.labelRects.push(label) break case LabelCodeType.ADD_ELLIPSE: labelTemplate.labelEllipses.push(label) break case LabelCodeType.ADD_LINE: labelTemplate.labelLines.push(label) break default: break } } } return labelTemplate } // 设置模板内标签的数据,一页放一个模板 export const setLabelTemplateData = (labelTemplate: LabelTemplate, printData: any[], baseUrl?: string, tableColumn?: TableColumn): LabelTemplate[][] => { const { labelTexts, labelHtmls, labelImages, labelBarCodes, labelTables } = labelTemplate const labelDatas: LabelTemplate[][] = [] for (let i = 0; i < printData.length; i++) { const tempTexts: LabelText[] = [] const tempHtmls: LabelHtml[] = [] const tempImages: LabelImage[] = [] const tempTables: LabelTable[] = [] const tempBarCodes: LabelBarCode[] = [] const labelData = printData[i] const fields = Object.keys(labelData) labelTexts.forEach(text => { let content = text.content const field = fields.find(field => field === text.fieldName) if (field) { const data = labelData[field] if (data) { if (content.includes(`[${field}]`)) { content = content.replaceAll(`[${field}]`, data) } else { content = data } } } tempTexts.push({ ...text, content }) }) labelHtmls.forEach(html => { let content = html.content const field = fields.find(field => field === html.fieldName) if (field) { const data = labelData[field] if (data) { const info = data.info if (info) { const url = baseUrl ? (baseUrl + info[0].addr) : info[0].addr content = `<img style="width:100%" src=${url} alt="" />` } else if (typeof data === 'string') { content = data.replaceAll('text-decoration-line', 'text-decoration') } else { content = data } } } tempHtmls.push({ ...html, content }) }) labelImages.forEach(image => { let url = image.url const field = fields.find(field => field === image.fieldName) if (field) { const data = labelData[field] if (data) { url = `<img style="width:100%" src=${data} alt="" />` } } tempImages.push({ ...image, url }) }) labelTables.forEach(table => { let content = table.content const field = fields.find(field => field === table.fieldName) if (field) { const data = labelData[field] if (typeof data !== 'string' && tableColumn) { content = buildTableData(tableColumn[field], data) } } tempTables.push({ ...table, content }) }) labelBarCodes.forEach(barCode => { let codeValue = barCode.codeValue const field = fields.find(field => field === barCode.fieldName) if (field) { const data = labelData[field] if (data) { codeValue = data } } tempBarCodes.push({ ...barCode, codeValue }) }) labelDatas.push([ { ...labelTemplate, labelTexts: tempTexts, labelHtmls: tempHtmls, labelTables: tempTables, labelBarCodes: tempBarCodes, } ]) } return labelDatas } // 设置模板内标签的数据,一页放多个模板 export const setLabelTemplateDataWithArrange = (labelTemplate: LabelTemplate, printData: any[], printerParam: PrinterParam, countOfPage: number, countOfColumn: number, baseUrl?: string): LabelTemplate[][] => { const labelCount = printData.length const { tempWidth, tempHeight, labelTexts, labelHtmls, labelImages, labelBarCodes, labelRects, labelEllipses, labelLines, labelShapes } = labelTemplate const { colSpacing, rowSpacing } = printerParam const pageParams: LabelPageParam[] = [] for (let i = 0; i < labelCount; i++) { const labelData = printData[i] const tempTexts: LabelText[] = [] const tempHtmls: LabelHtml[] = [] const tempImages: LabelImage[] = [] const tempBarCodes: LabelBarCode[] = [] const tempShapes: LabelShape[] = [] const tempRects: LabelRect[] = [] const tempEllipses: LabelEllipse[] = [] const tempLines: LabelLine[] = [] const pageIndex = Math.floor(i / countOfPage) const column = i % countOfColumn const row = Math.floor(i % countOfPage / countOfColumn) const left = tempWidth.number * column + column * colSpacing const top = tempHeight.number * row + row * rowSpacing const fields = Object.keys(labelData) labelTexts.forEach(text => { let content = text.content const field = fields.find(field => field === text.fieldName) if (field) { const data = labelData[field] if (data) { if (content.includes(`[${field}]`)) { content = content.replaceAll(`[${field}]`, data) } else { content = data } } } const textLeft = new LabelNumber(left + text.left.number) const textTop = new LabelNumber(top + text.top.number) tempTexts.push({ ...text, content, left: textLeft, top: textTop }) }) labelHtmls.forEach(html => { let content = html.content const field = fields.find(field => field === html.fieldName) if (field) { const data = labelData[field] if (data) { const info = data.info if (info) { const url = baseUrl ? (baseUrl + info[0].addr) : info[0].addr content = `<img style="width:100%" src=${url} alt="" />` } else if (typeof data === 'string') { content = data.replaceAll('text-decoration-line', 'text-decoration') } else { content = data } } } const htmlLeft = new LabelNumber(left + html.left.number) const htmlTop = new LabelNumber(top + html.top.number) tempHtmls.push({ ...html, content, left: htmlLeft, top: htmlTop }) }) labelImages.forEach(image => { let url = image.url const field = fields.find(field => field === image.fieldName) if (field) { const data = labelData[field] if (url) { url = `<img style="width:100%" src=${data} alt="" />` } } const imageLeft = new LabelNumber(left + image.left.number) const imageTop = new LabelNumber(top + image.top.number) tempImages.push({ ...image, url, left: imageLeft, top: imageTop }) }) labelBarCodes.forEach(barCode => { let codeValue = barCode.codeValue const field = fields.find(field => field === barCode.fieldName) if (field) { const data = labelData[field] if (data) { codeValue = data } } const barCodeLeft = new LabelNumber(left + barCode.left.number) const barCodeTop = new LabelNumber(top + barCode.top.number) tempBarCodes.push({ ...barCode, codeValue, left: barCodeLeft, top: barCodeTop }) }) labelShapes.forEach(shape => { const shapeLeft = new LabelNumber(left + shape.left.number) const shapeTop = new LabelNumber(top + shape.top.number) tempShapes.push({ ...shape, left: shapeLeft, top: shapeTop }) }) labelEllipses.forEach(ellipse => { const ellipseLeft = new LabelNumber(left + ellipse.left.number) const ellipseTop = new LabelNumber(top + ellipse.top.number) tempEllipses.push({ ...ellipse, left: ellipseLeft, top: ellipseTop }) }) labelRects.forEach(rect => { const rectLeft = new LabelNumber(left + rect.left.number) const rectTop = new LabelNumber(top + rect.top.number) tempRects.push({ ...rect, left: rectLeft, top: rectTop }) }) labelLines.forEach(line => { const lineLeft = new LabelNumber(left + line.left.number) const lineTop = new LabelNumber(top + line.top.number) tempLines.push({ ...line, left: lineLeft, top: lineTop }) }) pageParams.push([pageIndex, { ...labelTemplate, tempTop: new LabelNumber(top), tempLeft: new LabelNumber(left), labelTexts: tempTexts, labelHtmls: tempHtmls, labelBarCodes: tempBarCodes, labelShapes: tempShapes, labelEllipses: tempEllipses, labelRects: tempRects, labelLines: tempLines }]) } const pageIndexs = Array.from(new Set(pageParams.map(rect => rect[0]))) const labelDatas: LabelTemplate[][] = [] for (let i = 0; i < pageIndexs.length; i++) { const pageIndex = pageIndexs[i] const tempDatas: LabelTemplate[] = [] for (let j = 0; j < pageParams.length; j++) { const pageParam = pageParams[j]; const pageTemplate = pageParam[1] if (pageParam[0] === pageIndex) { tempDatas.push(pageTemplate) } } labelDatas.push(tempDatas) } return labelDatas } const buildTableData = (columnInfo: ColumnInfo, rowDatas: any[]) => { const { columns, statistics } = columnInfo columns.forEach(column => { const stastic = statistics?.find(stastic => stastic.field === column.name) column.statistics = stastic?.property }) const div = document.createElement('div') const table = document.createElement('table') table.setAttribute('border', '1') table.setAttribute('cellSpacing', '0') table.setAttribute('width', '100%') table.setAttribute('style', 'border-collapse:collapse') table.setAttribute('bordercolor', '#333333') const thead = document.createElement('thead') const tr = document.createElement('tr') for (let i = 0; i < columns.length; i++) { const th = document.createElement('th') const column = columns[i] th.textContent = column.label tr.appendChild(th) } thead.appendChild(tr) table.appendChild(thead) const tbody = document.createElement('tbody') for (let i = 0; i < rowDatas.length; i++) { const row = rowDatas[i] const tr = document.createElement('tr') for (let j = 0; j < columns.length; j++) { const td = document.createElement('td') const column = columns[j] td.textContent = row[column.name] tr.appendChild(td) } tbody.appendChild(tr) } table.appendChild(tbody) if (statistics) { const tfoot = document.createElement('tfoot') const maxRow = statistics.map(item => item.property.length).sort((a, b) => b - a)?.[0] ?? 0 for (let i = 0; i < maxRow; i++) { const tr = document.createElement('tr') for (let j = 0; j < columns.length; j++) { const column = columns[j] const statistic = column.statistics?.[i] const td = document.createElement('td') if (statistic) { td.textContent = statistic.label td.setAttribute('format', statistic.format) td.setAttribute('tdata', statistic.tdata) } tr.appendChild(td) } tfoot.appendChild(tr) } table.appendChild(tfoot) } div.appendChild(table) return div.innerHTML }