class Template { private moduleList: any[]; constructor(moduleList: any[]) { this.moduleList = moduleList; } // 将预览内容转换为HTML字符串的函数 generateHtmlString = () => { let htmlString = `
`; this.moduleList.forEach((module) => { const config = module.config; if (module.type === "title") { htmlString += `
${config.title || ""}
`; } if (module.type === "dealerInfo") { htmlString += `
`; if (config.showDealerName || config.showWatermelonGrade) { htmlString += `
${ config.showWatermelonGrade ? `${config.dealerName || ""}-${config.watermelonGrade || ""}` : config.dealerName || "" }
`; } else { htmlString += `
`; } if (config.showDestination || config.showVehicleNumber) { htmlString += `
${config.destination || ""} ${config.vehicleNumber || ""}
`; } else { htmlString += `
`; } htmlString += `
`; } if (module.type === "shippingInfo") { htmlString += `
`; if (config.showShippingFrom) { htmlString += `
发货地:
${config.shippingFrom || ""}
`; } if (config.showDate) { htmlString += `
日期:
${config.date || ""}
`; } htmlString += `
`; } if (module.type === "weightInfo") { if (config.data) { config.data.forEach((item: any) => { htmlString += `
`; if (config.showNetWeight) { htmlString += `
净重:
${item.netWeight || ""}
${config.netWeightUnit === "1" ? "斤" : "公斤"}
`; } if (config.showBoxWeight) { htmlString += `
箱重:
${item.boxWeight || ""}
${config.boxWeightUnit === "1" ? "斤" : "公斤"}
`; } if (config.showGrossWeight) { htmlString += `
毛重:
${item.grossWeight || ""}
${config.grossWeightUnit === "1" ? "斤" : "公斤"}
`; } if (config.showUnitPrice) { htmlString += `
单价:
${item.unitPrice || ""}
${config.unitPriceUnit === "1" ? "元/斤" : "元/公斤"}
`; } if (config.showAmount) { htmlString += `
金额:
${item.totalAmount || ""}
`; } if (config.showGrade) { htmlString += `
品级:
${item.watermelonGrade || ""}
`; } htmlString += `
`; }); } htmlString += `
`; if (config.showAccountCompany) { htmlString += `
入账公司:
${config.accountCompany || ""}
`; } if (config.showSumAmount) { htmlString += `
总计:
${config.sumAmount || ""}
`; } htmlString += `
`; } if (module.type === "packingSpec") { htmlString += `
`; // 计算需要显示的列数 const visibleColumnCount = [ config.showBoxType, config.showQuantity, config.showUnitPrice, config.showAmount, config.showUnitWeight, config.showWeight, ].filter(Boolean).length + 1; // +1 是因为"规格:"列总是显示 const gridClass = `grid w-full gap-0 text-base grid-cols-${visibleColumnCount}`; htmlString += `
规格:
`; htmlString += `
`; if (config.columns) { config.columns.forEach((column: any, index: number) => { if (index === 0) { htmlString += `
 
`; return; } if ( (column.dataIndex === "boxType" && config.showBoxType) || (column.dataIndex === "quantity" && config.showQuantity) || (column.dataIndex === "unitPrice" && config.showUnitPrice) || (column.dataIndex === "amount" && config.showAmount) || (column.dataIndex === "unitWeight" && config.showUnitWeight) || (column.dataIndex === "weight" && config.showWeight) ) { htmlString += `
${column.title || ""}
`; } }); } htmlString += `
`; htmlString += `
`; if (config.data) { config.data.forEach((item: any, index: number) => { htmlString += `
${item.boxSpecName}
`; htmlString += `
${item.boxType || ""}
`; if (config.showQuantity) { htmlString += `
${item.quantity || ""}
`; } if (config.showUnitPrice) { htmlString += `
${item.unitPrice || ""}
`; } if (config.showAmount) { htmlString += `
${item.amount || ""}
`; } if (config.showUnitWeight) { htmlString += `
${item.unitWeight || ""}
`; } if (config.showWeight) { htmlString += `
${item.weight || ""}
`; } htmlString += `
`; }); } htmlString += `
总件数
`; if (config.showQuantity) { htmlString += `
${ config.data?.reduce( (acc: any, cur: any) => acc + Number(cur.quantity || 0), 0, ) || 0 }
`; } if (config.showUnitPrice) { htmlString += `
`; } if (config.showAmount) { htmlString += `
${ config.data?.reduce( (acc: any, cur: any) => acc + Number(cur.amount || 0), 0, ) || 0 }
`; } if (config.showUnitWeight) { htmlString += `
`; } if (config.showWeight) { htmlString += `
${ config.data?.reduce( (acc: any, cur: any) => acc + Number(cur.weight || 0), 0, ) || 0 }
`; } htmlString += `
`; htmlString += `
`; } if (module.type === "vehicleInfo") { htmlString += `
`; if (config.showDriverPhone) { htmlString += `
司机号码:
${config.driverPhone || ""}
`; } if (config.showLicensePlate) { htmlString += `
车牌:
${config.licensePlate || ""}
`; } if (config.showEstimatedArrivalTime) { htmlString += `
预计到仓时间:
${config.estimatedArrivalTime || ""}
`; } if (config.showRemarks) { htmlString += `
备注:
${config.remarks || ""}
`; } if (config.showFreightDebt) { htmlString += `
${config.freightDebtTitle || "运费欠"}:
${config.freightDebt || ""}
`; } if (config.showStrawMatDebt) { htmlString += `
草帘欠:
${config.strawMatDebt || ""}
`; } htmlString += `
`; } if (module.type === "otherFees") { htmlString += `
`; if (config.feeItems) { config.feeItems.forEach((feeType: any) => { htmlString += `
${(config.feeLabels && config.feeLabels[feeType]) || ""}:
${config[feeType] || ""}元
`; }); } htmlString += `
`; } if (module.type === "totalAmount") { htmlString += `
`; if (config.showTotalAmount) { htmlString += `
${config.sumTitle || "合计金额"}:
${config.amount || ""}
`; } if (config.showFarmer) { htmlString += `
瓜农:
${config.farmer || ""}
`; } htmlString += `
`; } if (module.type === "otherInfo") { htmlString += `
车次:
${config.vehicleNumber}
收货地:
${config.destination}
`; if (config.showShippingFrom) { htmlString += `
产地:
${config.shippingFrom}
`; } htmlString += `
供应商:
${config.accountCompany}
发车时间:
${config.date}
`; if (config.showEstimatedArrivalTime) { htmlString += `
到达时间:
${config.estimatedArrivalTime}
`; } if (config.data) { config.data?.forEach((item: any) => { if (config.showGrade) { htmlString += `
品名:
${item.watermelonGrade}
`; } htmlString += `
发货重量:
以公司入库重量为准。
净瓜单价:
${item.unitPrice} ${config.unitPriceUnit === "1" ? "元/斤" : "元/公斤"}
`; }); } htmlString += `
大约重量:
${config.data?.reduce( (acc: any, cur: any) => acc + cur.grossWeight, 0, )}
箱数
${config.data?.reduce( (acc: any, cur: any) => acc + cur.boxCount, 0, )}
车号:
${config.licensePlate}
手机号:
${config.driverPhone}
`; } }); htmlString += `
`; return htmlString; }; }