feat(contract): 新增合同打印功能,重构产品金额计算逻辑

1. 新增合同打印预览功能,支持A4格式打印,包含合同完整信息和产品明细
2. 新增多个产品金额计算工具函数,统一管理产品税额、无税单价等字段计算
3. 重构产品内容组件,新增税率除数、无税单价、税额列,实现字段联动自动计算
4. 新增合同logo静态资源,优化表格布局和样式
This commit is contained in:
2026-06-02 08:56:31 +08:00
parent a425a9052a
commit 6b8eac4139
6 changed files with 799 additions and 531 deletions

View File

@@ -133,13 +133,40 @@
<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
@pagination="getList" style="padding: 10px; margin-bottom: 10px !important;" />
<!-- 导出预览对话框 -->
<el-dialog title="导出预览" :visible.sync="exportDialogVisible" width="95%" top="20px" append-to-body
:close-on-click-modal="false" @open="generatePreviewHtml">
<div style="display: flex; gap: 16px; height: calc(100vh - 200px);">
<!-- 左侧列配置 -->
<div style="width: 200px; flex-shrink: 0; border: 1px solid #e4e7ed; border-radius: 4px; padding: 12px; overflow-y: auto;">
<div style="font-weight: bold; margin-bottom: 10px; font-size: 14px;">产品表列配置</div>
<el-checkbox v-model="selectAllColumns" :indeterminate="columnIndeterminate" @change="handleSelectAllColumns"
style="margin-bottom: 8px;">全选</el-checkbox>
<div v-for="col in columnConfigs" :key="col.key" style="margin-bottom: 6px;">
<el-checkbox v-model="col.checked" @change="onColumnChange">{{ col.label }}</el-checkbox>
</div>
<el-divider />
<div style="font-size: 12px; color: #909399;">勾选的列将显示在导出的产品表中</div>
</div>
<!-- 右侧预览 -->
<div style="flex: 1; border: 1px solid #e4e7ed; border-radius: 4px; overflow: hidden; background: #f0f0f0;">
<iframe ref="previewFrame" style="width: 100%; height: 100%; border: none; background: #fff;"></iframe>
</div>
</div>
<span slot="footer">
<el-button @click="exportDialogVisible = false"> </el-button>
<el-button type="primary" :loading="exportLoading" @click="confirmExport">确认导出</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
import { listOrder, updateOrder } from "@/api/crm/order";
import * as ExcelJS from 'exceljs';
import { saveAs } from 'file-saver';
import html2canvas from 'html2canvas';
import jsPDF from 'jspdf';
import contractLogo from '@/assets/images/contractLogo.png';
import {
parseProductContent,
convertToChinese
@@ -175,6 +202,24 @@ export default {
signLocation: undefined,
status: undefined,
},
// 导出预览
exportDialogVisible: false,
exportLoading: false,
exportRow: null,
selectAllColumns: true,
columnIndeterminate: false,
columnConfigs: [
{ key: 'spec', label: '规格mm', checked: true },
{ key: 'material', label: '材质', checked: true },
{ key: 'quantity', label: '数量(吨)', checked: true },
{ key: 'taxPrice', label: '含税单价(元/吨)', checked: true },
{ key: 'taxDivisor', label: '税率除数', checked: true },
{ key: 'noTaxPrice', label: '无税单价(元/吨)', checked: true },
{ key: 'taxTotal', label: '含税总额(元)', checked: true },
{ key: 'noTaxTotal', label: '无税总额(元)', checked: true },
{ key: 'taxAmount', label: '税额(元)', checked: true },
{ key: 'remark', label: '备注', checked: true },
],
};
},
created() {
@@ -235,529 +280,359 @@ export default {
toggleMoreFilter() {
this.showMoreFilter = !this.showMoreFilter;
},
convertToChinese(amount) {
return convertToChinese(amount);
/** 导出合同 - 打开预览对话框 */
handleExport(row) {
this.exportRow = row;
this.exportDialogVisible = true;
},
/** 导出合同 */
async handleExport(row) {
// 1. 创建excel
const workbook = new ExcelJS.Workbook();
const worksheet = workbook.addWorksheet('产品销售合同');
// let orderItems = [];
// // 2. 查询合同详情
// const res = await listOrderItem({ orderId: row.orderId, pageNum: 1, pageSize: 1000 });
// orderItems = res.rows || [];
/** 全选/取消全选 */
handleSelectAllColumns(val) {
this.columnConfigs.forEach(col => { col.checked = val; });
this.columnIndeterminate = false;
this.generatePreviewHtml();
},
/** 单个列勾选变化 */
onColumnChange() {
const checkedCount = this.columnConfigs.filter(c => c.checked).length;
this.selectAllColumns = checkedCount === this.columnConfigs.length;
this.columnIndeterminate = checkedCount > 0 && checkedCount < this.columnConfigs.length;
this.generatePreviewHtml();
},
/** 根据列配置生成产品表HTMl */
buildProductTableHtml(productData, products) {
const activeCols = this.columnConfigs.filter(c => c.checked);
const hasCol = (key) => activeCols.some(c => c.key === key);
// 2. 设置列宽
worksheet.columns = [
{ width: 10 },
{ width: 20 },
{ width: 15 },
{ width: 15 },
{ width: 15 },
{ width: 15 },
{ width: 15 },
{ width: 20 }
];
// 表头
let headerCells = '<th style="border:1px solid #000;padding:4px 4px;font-weight:bold;width:30px;">序号</th>';
if (hasCol('spec')) headerCells += '<th style="border:1px solid #000;padding:4px 4px;font-weight:bold;width:80px;">规格(mm)</th>';
if (hasCol('material')) headerCells += '<th style="border:1px solid #000;padding:4px 4px;font-weight:bold;width:60px;">材质</th>';
if (hasCol('quantity')) headerCells += '<th style="border:1px solid #000;padding:4px 4px;font-weight:bold;width:55px;">数量(吨)</th>';
if (hasCol('taxPrice')) headerCells += '<th style="border:1px solid #000;padding:4px 4px;font-weight:bold;width:70px;">含税单价(元/吨)</th>';
if (hasCol('taxDivisor')) headerCells += '<th style="border:1px solid #000;padding:4px 4px;font-weight:bold;width:45px;">税率除数</th>';
if (hasCol('noTaxPrice')) headerCells += '<th style="border:1px solid #000;padding:4px 4px;font-weight:bold;width:70px;">无税单价(元/吨)</th>';
if (hasCol('taxTotal')) headerCells += '<th style="border:1px solid #000;padding:4px 4px;font-weight:bold;width:70px;">含税总额(元)</th>';
if (hasCol('noTaxTotal')) headerCells += '<th style="border:1px solid #000;padding:4px 4px;font-weight:bold;width:70px;">无税总额(元)</th>';
if (hasCol('taxAmount')) headerCells += '<th style="border:1px solid #000;padding:4px 4px;font-weight:bold;width:55px;">税额(元)</th>';
if (hasCol('remark')) headerCells += '<th style="border:1px solid #000;padding:4px 4px;font-weight:bold;width:80px;">备注</th>';
const colCount = activeCols.length + 1; // +1 for 序号
// 3. 合并单元格并设置内容
// 公司信息
worksheet.mergeCells('A1:H1');
worksheet.getCell('A1').value = '嘉祥科伦普重工有限公司';
worksheet.getCell('A1').font = { size: 16, bold: true };
worksheet.getCell('A1').alignment = { horizontal: 'center', vertical: 'middle' };
// 合同标题
worksheet.mergeCells('A2:F2');
worksheet.getCell('A2').value = '产品销售合同';
worksheet.getCell('A2').font = { size: 18, bold: true };
worksheet.getCell('A2').alignment = { horizontal: 'center', vertical: 'middle' };
// 合同编号
worksheet.mergeCells('G2:H2');
worksheet.getCell('G2').value = `合同编号:${row.contractCode || ''}`;
worksheet.getCell('G2').alignment = { horizontal: 'right', vertical: 'middle' };
// 供方信息
worksheet.getCell('A3').value = `供方(甲方):${row.supplier || '嘉祥科伦普重工有限公司'}`;
worksheet.getCell('A3').alignment = { horizontal: 'left', vertical: 'middle' };
// 签订时间
worksheet.getCell('E3').value = `签订时间:${row.signTime ? this.parseTime(row.signTime, '{y}年{m}月{d}日') : '2026年 月 日'}`;
worksheet.getCell('E3').alignment = { horizontal: 'left', vertical: 'middle' };
// 需方信息
worksheet.getCell('A4').value = `需方(乙方):${row.customer || ''}`;
worksheet.getCell('A4').alignment = { horizontal: 'left', vertical: 'middle' };
// 签订地点
worksheet.getCell('E4').value = `签订地点:${row.signLocation || '山东省济宁市嘉祥县'}`;
worksheet.getCell('E4').alignment = { horizontal: 'left', vertical: 'middle' };
// 产品内容标题
worksheet.getCell('A5').value = '一、产品内容';
worksheet.getCell('A5').font = { bold: true };
// 产品名称和生产厂家
worksheet.mergeCells('A6:C6');
worksheet.getCell('A6').value = `产品名称:${row.productName || '冷硬钢卷'}`;
worksheet.getCell('A6').alignment = { horizontal: 'left', vertical: 'middle' };
worksheet.getCell('A6').border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
worksheet.mergeCells('D6:H6');
worksheet.getCell('D6').value = `生产厂家:${row.manufacturer || '嘉祥科伦普重工有限公司'}`;
worksheet.getCell('D6').alignment = { horizontal: 'left', vertical: 'middle' };
worksheet.getCell('D6').border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
// 产品表格标题(分为两行)
// 合并序号、规格、材质、备注列的单元格(两行)
const mergedHeaders = ['序号', '规格mm', '材质', '备注'];
const mergedColumns = [1, 2, 3, 8];
mergedColumns.forEach((col, index) => {
// 合并两行
worksheet.mergeCells(7, col, 8, col);
const cell = worksheet.getCell(7, col);
cell.value = mergedHeaders[index];
cell.font = { bold: true };
cell.fill = { type: 'pattern', pattern: 'solid' };
cell.border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
cell.alignment = { horizontal: 'center', vertical: 'middle' };
// 数据行
let bodyRows = '';
products.forEach((product, index) => {
let cells = `<td style="border:1px solid #000;padding:3px 4px;text-align:center;">${index + 1}</td>`;
if (hasCol('spec')) cells += `<td style="border:1px solid #000;padding:3px 4px;text-align:center;">${product.spec || ''}</td>`;
if (hasCol('material')) cells += `<td style="border:1px solid #000;padding:3px 4px;text-align:center;">${product.material || ''}</td>`;
if (hasCol('quantity')) cells += `<td style="border:1px solid #000;padding:3px 4px;text-align:center;">${product.quantity || ''}</td>`;
if (hasCol('taxPrice')) cells += `<td style="border:1px solid #000;padding:3px 4px;text-align:center;">${product.taxPrice || ''}</td>`;
if (hasCol('taxDivisor')) cells += `<td style="border:1px solid #000;padding:3px 4px;text-align:center;">${product.taxDivisor || '1.13'}</td>`;
if (hasCol('noTaxPrice')) cells += `<td style="border:1px solid #000;padding:3px 4px;text-align:center;">${(product.noTaxPrice || 0).toFixed(2)}</td>`;
if (hasCol('taxTotal')) cells += `<td style="border:1px solid #000;padding:3px 4px;text-align:center;">${(product.taxTotal || 0).toFixed(2)}</td>`;
if (hasCol('noTaxTotal')) cells += `<td style="border:1px solid #000;padding:3px 4px;text-align:center;">${(product.noTaxTotal || 0).toFixed(2)}</td>`;
if (hasCol('taxAmount')) cells += `<td style="border:1px solid #000;padding:3px 4px;text-align:center;">${(product.taxAmount || 0).toFixed(2)}</td>`;
if (hasCol('remark')) cells += `<td style="border:1px solid #000;padding:3px 4px;text-align:center;max-width:100px;word-wrap:break-word;">${product.remark || ''}</td>`;
bodyRows += `<tr>${cells}</tr>`;
});
// 数量、含税单价、不含税单价、含税总额的标题和单位各占一
const quantityHeaders = ['数量', '(吨)'];
const taxPriceHeaders = ['含税单价', '(元/吨)'];
const noTaxPriceHeaders = ['不含税单价', '(元/吨)'];
const taxTotalHeaders = ['含税总额', '(元)'];
// 合计
const totalQty = products.reduce((a, p) => a + (parseFloat(p.quantity) || 0), 0);
const totalTax = products.reduce((a, p) => a + (parseFloat(p.taxTotal) || 0), 0);
const totalNoTax = products.reduce((a, p) => a + (parseFloat(p.noTaxTotal) || 0), 0);
const totalTaxAmt = products.reduce((a, p) => a + (parseFloat(p.taxAmount) || 0), 0);
const totalAmountInWords = productData.totalAmountInWords || convertToChinese(totalTax) || '零元整';
// 数量列
worksheet.getCell(7, 4).value = quantityHeaders[0];
worksheet.getCell(8, 4).value = quantityHeaders[1];
// 含税单价列
worksheet.getCell(7, 5).value = taxPriceHeaders[0];
worksheet.getCell(8, 5).value = taxPriceHeaders[1];
// 不含税单价列
worksheet.getCell(7, 6).value = noTaxPriceHeaders[0];
worksheet.getCell(8, 6).value = noTaxPriceHeaders[1];
// 含税总额列
worksheet.getCell(7, 7).value = taxTotalHeaders[0];
worksheet.getCell(8, 7).value = taxTotalHeaders[1];
// 设置样式
for (let row = 7; row <= 8; row++) {
for (let col = 4; col <= 7; col++) {
const cell = worksheet.getCell(row, col);
cell.font = { bold: true };
cell.fill = { type: 'pattern', pattern: 'solid' };
cell.border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
cell.alignment = { horizontal: 'center', vertical: 'middle' };
// 合计行单元格(按列顺序填充)
let totalCells = '';
let cellIdx = 0;
activeCols.forEach((col) => {
if (cellIdx < 2) {
// 前 2 个活动列spec、material与序号一起被"合计" colspan=3 覆盖
if (cellIdx === 0) {
totalCells += `<td colspan="3" style="border:1px solid #000;padding:4px 4px;font-weight:bold;text-align:center;">合&nbsp;&nbsp;计</td>`;
}
} else {
let val = '';
if (col.key === 'quantity') val = totalQty.toFixed(2);
else if (col.key === 'taxTotal') val = totalTax.toFixed(2);
else if (col.key === 'noTaxTotal') val = totalNoTax.toFixed(2);
else if (col.key === 'taxAmount') val = totalTaxAmt.toFixed(2);
totalCells += `<td style="border:1px solid #000;padding:3px 4px;text-align:center;">${val}</td>`;
}
}
cellIdx++;
});
const html = `
<table style="width:100%;border-collapse:collapse;font-size:11px;margin-bottom:6px;">
<tr>
<td colspan="4" style="border:1px solid #000;padding:3px 6px;font-weight:bold;text-align:left;">产品名称:${productData.productName || ''}</td>
<td colspan="${colCount - 4 > 0 ? colCount - 4 : 1}" style="border:1px solid #000;padding:3px 6px;font-weight:bold;text-align:left;">生产厂家:嘉祥科伦普重工有限公司</td>
</tr>
<tr style="background-color:#f5f5f5;">${headerCells}</tr>
${bodyRows}
<tr>${totalCells}</tr>
<tr>
<td colspan="${colCount}" style="border:1px solid #000;padding:4px 6px;font-weight:bold;text-align:left;">合计人民币(大写):${totalAmountInWords}</td>
</tr>
</table>`;
return html;
},
/** 生成预览HTML并更新iframe */
generatePreviewHtml() {
const row = this.exportRow;
if (!row) return;
// 产品表格数据, 来源于一个json字符串productContentjson结构参考ProductContent.vue
// 解析产品内容
let productData = parseProductContent(row.productContent);
if (!productData.productName && row.productName) {
productData.productName = row.productName;
}
const products = productData.products && productData.products.length > 0 ? productData.products : [];
// if (orderItems) {
// try {
// // 改为从orderItems中获取产品内容
// const productName = orderItems[0].productType || '冷硬钢卷';
// const remark = row.remark || '';
// const products = orderItems.map(item => ({
// spec: item.finishedProductSpec || '',
// material: item.material || '',
// quantity: parseFloat(item.weight || 0),
// taxPrice: parseFloat(item.contractPrice || 0),
// noTaxPrice: parseFloat(item.itemAmount || 0),
// taxTotal: parseFloat(item.contractPrice) * parseFloat(item.weight || 0),
// remark: item.remark || ''
// }));
const productTableHtml = this.buildProductTableHtml(productData, products);
// const totalQuantity = products.reduce((acc, product) => acc + parseFloat(product.quantity || 0), 0);
// const totalTaxTotal = products.reduce((acc, product) => acc + parseFloat(product.taxTotal || 0), 0);
// const totalAmountInWords = this.convertToChinese(totalTaxTotal);
// productData = {
// products,
// productName,
// remark,
// totalQuantity,
// totalTaxTotal,
// totalAmountInWords
// };
// } catch (error) {
// console.error('解析产品内容失败:', error);
// }
// }
// 更新产品名称
worksheet.getCell('A6').value = `产品名称:${productData.productName}`;
// 产品表格数据
let currentRow = 9;
let totalQuantity = 0;
let totalTaxTotal = 0;
if (productData.products && productData.products.length > 0) {
productData.products.forEach((product, index) => {
const rowNum = currentRow + index;
worksheet.getCell(`A${rowNum}`).value = index + 1;
worksheet.getCell(`B${rowNum}`).value = product.spec || '';
worksheet.getCell(`C${rowNum}`).value = product.material || '';
worksheet.getCell(`D${rowNum}`).value = product.quantity || 0;
worksheet.getCell(`E${rowNum}`).value = product.taxPrice || 0;
worksheet.getCell(`F${rowNum}`).value = product.noTaxPrice || 0;
worksheet.getCell(`G${rowNum}`).value = product.taxTotal || 0;
worksheet.getCell(`H${rowNum}`).value = product.remark || '';
// 设置数据行边框
for (let i = 1; i <= 8; i++) {
const cell = worksheet.getCell(rowNum, i);
cell.border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
cell.alignment = { horizontal: 'center', vertical: 'middle' };
}
// 累加合计
totalQuantity += parseFloat(product.quantity || 0);
totalTaxTotal += parseFloat(product.taxTotal || 0);
});
currentRow += productData.products.length;
} else {
// 至少一行空数据
worksheet.getCell(`A${currentRow}`).value = 1;
worksheet.getCell(`B${currentRow}`).value = '';
worksheet.getCell(`C${currentRow}`).value = 'SPCC';
worksheet.getCell(`D${currentRow}`).value = '';
worksheet.getCell(`E${currentRow}`).value = '';
worksheet.getCell(`F${currentRow}`).value = 0.00;
worksheet.getCell(`G${currentRow}`).value = 0;
worksheet.getCell(`H${currentRow}`).value = '';
// 设置数据行边框
for (let i = 1; i <= 8; i++) {
const cell = worksheet.getCell(currentRow, i);
cell.border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
cell.alignment = { horizontal: 'center', vertical: 'middle' };
}
currentRow++;
}
// 合计行
worksheet.getCell(`A${currentRow}`).value = '合 计';
worksheet.getCell(`A${currentRow}`).font = { bold: true };
worksheet.getCell(`A${currentRow}`).border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
worksheet.getCell(`A${currentRow}`).alignment = { horizontal: 'center', vertical: 'middle' };
worksheet.getCell(`D${currentRow}`).value = productData.totalQuantity || totalQuantity;
worksheet.getCell(`D${currentRow}`).border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
worksheet.getCell(`D${currentRow}`).alignment = { horizontal: 'center', vertical: 'middle' };
worksheet.getCell(`G${currentRow}`).value = productData.totalTaxTotal || totalTaxTotal;
worksheet.getCell(`G${currentRow}`).border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
worksheet.getCell(`G${currentRow}`).alignment = { horizontal: 'center', vertical: 'middle' };
worksheet.getCell(`H${currentRow}`).value = '';
worksheet.getCell(`H${currentRow}`).border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
worksheet.getCell(`H${currentRow}`).alignment = { horizontal: 'center', vertical: 'middle' };
currentRow++;
// 大写金额
worksheet.mergeCells(`A${currentRow}:B${currentRow}`);
worksheet.getCell(`A${currentRow}`).value = `合计人民币(大写)`;
worksheet.getCell(`A${currentRow}`).font = { bold: true };
worksheet.getCell(`A${currentRow}`).border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
worksheet.getCell(`A${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
worksheet.mergeCells(`C${currentRow}:H${currentRow}`);
worksheet.getCell(`C${currentRow}`).value = `${productData.totalAmountInWords || row.totalAmountUpper || '零元整'}`;
worksheet.getCell(`C${currentRow}`).font = { bold: true };
worksheet.getCell(`C${currentRow}`).border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
worksheet.getCell(`C${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
currentRow++;
// 备注行
worksheet.mergeCells(`B${currentRow}:H${currentRow}`);
worksheet.getCell(`A${currentRow}`).value = '备注:';
worksheet.getCell(`A${currentRow}`).alignment = { horizontal: 'left', vertical: 'top' };
worksheet.getCell(`A${currentRow}`).border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
worksheet.getCell(`B${currentRow}`).alignment = { horizontal: 'left', vertical: 'top' };
worksheet.getCell(`B${currentRow}`).value = productData.remark || '';
worksheet.getCell(`B${currentRow}`).border = {
top: { style: 'thin' },
left: { style: 'thin' },
bottom: { style: 'thin' },
right: { style: 'thin' }
};
worksheet.getCell(`B${currentRow}`).alignment = { horizontal: 'left', vertical: 'top', wrapText: true };
currentRow++;
// 空白行保持至少5行空白
// const emptyRows = Math.max(5, 15 - currentRow + 1);
// for (let i = 0; i < emptyRows; i++) {
// const rowNum = currentRow + i;
// for (let j = 1; j <= 8; j++) {
// const cell = worksheet.getCell(rowNum, j);
// cell.border = {
// top: { style: 'thin' },
// left: { style: 'thin' },
// bottom: { style: 'thin' },
// right: { style: 'thin' }
// };
// }
// }
// currentRow += emptyRows;
// 产品表格之后是其他合同内容contractContent字段存储的是HTML格式的富文本
// 解析合同内容
let contractContentHtml = '';
if (row.contractContent) {
// 优化HTML处理保留p标签作为换行符将多个p标签单元格合并
let htmlContent = row.contractContent;
// 提取所有p标签内容
const pTagRegex = /<p[^>]*>([\s\S]*?)<\/p>/g;
let match;
const pContents = [];
while ((match = pTagRegex.exec(htmlContent)) !== null) {
let content = match[1];
// 移除其他HTML标签
content = content.replace(/<[^>]*>/g, '');
// 处理HTML实体
content = content.replace(/&nbsp;/g, ' ');
content = content.replace(/&lt;/g, '<');
content = content.replace(/&gt;/g, '>');
content = content.replace(/&amp;/g, '&');
content = content.replace(/&quot;/g, '"');
content = content.replace(/&#39;/g, "'");
// 清理空格和换行
content = content.trim();
// 如果不是以大写汉字数字开头,添加一个中文字符的缩进
let content = match[1].replace(/<[^>]*>/g, '').replace(/&nbsp;/g, ' ').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&#39;/g, "'").trim();
if (content) {
// 检查是否以大写汉字数字开头(一、二、三、四、五、六、七、八、九、十)
const chineseNumberRegex = /^[一二三四五六七八九十]+、/;
if (!chineseNumberRegex.test(content)) {
content = ' ' + content; // 使用全角空格作为中文字符缩进
}
pContents.push(content);
pContents.push(chineseNumberRegex.test(content) ? content : ' ' + content);
}
}
// 如果没有提取到p标签内容尝试提取所有文本内容
if (pContents.length === 0) {
let textContent = htmlContent.replace(/<[^>]*>/g, '');
textContent = textContent.replace(/&nbsp;/g, ' ').trim();
const textContent = htmlContent.replace(/<[^>]*>/g, '').replace(/&nbsp;/g, ' ').trim();
if (textContent) pContents.push(textContent);
}
if (pContents.length > 0) {
contractContentHtml = '<div style="margin-top:8px;font-size:12px;line-height:1.8;">' + pContents.join('<br/>') + '</div>';
}
}
// 如果不是以大写汉字数字开头,添加一个中文字符的缩进
if (textContent) {
// 检查是否以大写汉字数字开头(一、二、三、四、五、六、七、八、九、十)
const chineseNumberRegex = /^[一二三四五六七八九十]+、/;
if (!chineseNumberRegex.test(textContent)) {
textContent = ' ' + textContent; // 使用全角空格作为中文字符缩进
const totalTax = products.reduce((a, p) => a + (parseFloat(p.taxTotal) || 0), 0);
const amountWords = productData.totalAmountInWords || convertToChinese(totalTax) || '零元整';
const fullHtml = `
<!DOCTYPE html>
<html>
<head><meta charset="utf-8"><title>合同预览</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: 'SimSun','宋体',serif; color: #000; background: #e8e8e8; font-size: 12px; line-height: 1.6; display: flex; flex-direction: column; align-items: center; padding: 20px 0; }
.a4-page { width: 794px; min-height: 1123px; padding: 30px 40px; background: #fff; margin-bottom: 20px; box-shadow: 0 2px 12px rgba(0,0,0,0.15); }
.company-header { display: flex; align-items: flex-start; justify-content: space-between; margin-bottom: 6px; }
.contract-title { text-align: center; font-size: 20px; font-weight: bold; letter-spacing: 6px; margin-bottom: 14px; }
.info-row { display: flex; justify-content: space-between; margin-bottom: 4px; font-size: 13px; line-height: 2; }
.section-title { font-size: 13px; font-weight: bold; margin-bottom: 4px; margin-top: 8px; }
table { width: 100%; border-collapse: collapse; }
th, td { border: 1px solid #000; padding: 3px 4px; text-align: center; font-size: 11px; }
th { background-color: #f5f5f5; font-weight: bold; }
.sign-section { margin-top: 24px; font-size: 12px; line-height: 2.2; }
.sign-section .col { width: 48%; }
.sign-row { display: flex; justify-content: space-between; }
</style>
</head>
<body>
<div class="a4-page" style="position:relative;">
<img src="${contractLogo}" style="position:absolute;left:20px;top:20px;height:80px;" crossorigin="anonymous" />
<div style="text-align:center;padding-top:10px;">
<div style="font-size:20px;font-weight:bold;letter-spacing:2px;">嘉祥科伦普重工有限公司</div>
</div>
<div style="display:flex;justify-content:center;align-items:baseline;margin:12px 0 8px 0;">
<div style="font-size:22px;font-weight:bold;letter-spacing:6px;">产 品 销 售 合 同</div>
<div style="font-size:11px;margin-left:40px;">合同编号:${row.contractCode || ''}</div>
</div>
<div class="info-row">
<div style="width:55%;">
<div>供方(甲方):${row.supplier || ''}</div>
<div>需方(乙方):${row.customer || ''}</div>
</div>
<div style="width:40%;">
<div>签订时间:${row.signTime ? this.parseTime(row.signTime, '{y}年{m}月{d}日') : ''}</div>
<div>签订地点:${row.signLocation || ''}</div>
</div>
</div>
<div class="section-title">一、产品内容</div>
${productTableHtml}
${contractContentHtml}
<div class="sign-section">
<div class="sign-row">
<div class="col">
<div>供方(甲方):${row.supplier || ''}</div>
<div>地址:${row.supplierAddress || ''}</div>
<div>电话:${row.supplierPhone || ''}</div>
<div>开户行:${row.supplierBank || ''}</div>
<div>账号:${row.supplierAccount || ''}</div>
<div>税号:${row.supplierTaxNo || ''}</div>
</div>
<div class="col">
<div>需方(乙方):${row.customer || ''}</div>
<div>地址:${row.customerAddress || ''}</div>
<div>电话:${row.customerPhone || ''}</div>
<div>开户行:${row.customerBank || ''}</div>
<div>账号:${row.customerAccount || ''}</div>
<div>税号:${row.customerTaxNo || ''}</div>
</div>
</div>
</div>
</div>
</body>
</html>`;
this.$nextTick(() => {
const iframe = this.$refs.previewFrame;
if (iframe) {
iframe.srcdoc = fullHtml;
}
});
},
/** 确认导出PDF */
async confirmExport() {
const row = this.exportRow;
if (!row) return;
this.exportLoading = true;
const loading = this.$loading({
lock: true,
text: '正在生成PDF请稍候...',
background: 'rgba(0, 0, 0, 0.7)'
});
try {
let productData = parseProductContent(row.productContent);
if (!productData.productName && row.productName) {
productData.productName = row.productName;
}
const products = productData.products && productData.products.length > 0 ? productData.products : [];
const productTableHtml = this.buildProductTableHtml(productData, products);
// 解析合同内容
let contractContentHtml = '';
if (row.contractContent) {
let htmlContent = row.contractContent;
const pTagRegex = /<p[^>]*>([\s\S]*?)<\/p>/g;
let match;
const pContents = [];
while ((match = pTagRegex.exec(htmlContent)) !== null) {
let content = match[1].replace(/<[^>]*>/g, '').replace(/&nbsp;/g, ' ').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&amp;/g, '&').replace(/&quot;/g, '"').replace(/&#39;/g, "'").trim();
if (content) {
const chineseNumberRegex = /^[一二三四五六七八九十]+、/;
pContents.push(chineseNumberRegex.test(content) ? content : ' ' + content);
}
pContents.push(textContent);
}
if (pContents.length === 0) {
const textContent = htmlContent.replace(/<[^>]*>/g, '').replace(/&nbsp;/g, ' ').trim();
if (textContent) pContents.push(textContent);
}
if (pContents.length > 0) {
contractContentHtml = '<div style="margin-top:10px;font-size:12px;line-height:1.8;">' + pContents.join('<br/>') + '</div>';
}
}
// 直接合并单元格并设置内容,避免合并已合并的单元格
if (pContents.length > 0) {
// 计算需要的行数
const contentLines = pContents.reduce((total, content) => {
return total + (content.match(/\n/g) || []).length + 1;
}, 0);
const totalTax = products.reduce((a, p) => a + (parseFloat(p.taxTotal) || 0), 0);
const amountWords = productData.totalAmountInWords || convertToChinese(totalTax) || '零元整';
// 合并单元格
const endRow = currentRow + Math.ceil(contentLines / 2); // 估算需要的行数
worksheet.mergeCells(`A${currentRow}:H${endRow}`);
// 构建完整的合同HTML794px 宽适合A4
const contractHtml = `
<div id="contract-pdf-content" style="width:794px;padding:30px 40px;font-family:'SimSun','宋体',serif;color:#000;background:#fff;box-sizing:border-box;position:relative;">
<img src="${contractLogo}" style="position:absolute;left:20px;top:20px;height:80px;" crossorigin="anonymous" />
<div style="text-align:center;padding-top:10px;">
<div style="font-size:20px;font-weight:bold;letter-spacing:2px;">嘉祥科伦普重工有限公司</div>
</div>
<div style="display:flex;justify-content:center;align-items:baseline;margin:12px 0 8px 0;">
<div style="font-size:22px;font-weight:bold;letter-spacing:6px;">产 品 销 售 合 同</div>
<div style="font-size:11px;margin-left:40px;">合同编号:${row.contractCode || ''}</div>
</div>
<div style="display:flex;justify-content:space-between;margin-bottom:8px;font-size:13px;line-height:2;">
<div style="width:55%;">
<div>供方(甲方):${row.supplier || ''}</div>
<div>需方(乙方):${row.customer || ''}</div>
</div>
<div style="width:40%;text-align:left;">
<div>签订时间:${row.signTime ? this.parseTime(row.signTime, '{y}年{m}月{d}日') : ''}</div>
<div>签订地点:${row.signLocation || ''}</div>
</div>
</div>
<div style="font-size:13px;font-weight:bold;margin-bottom:5px;">一、产品内容</div>
${productTableHtml}
${contractContentHtml}
<div style="margin-top:40px;font-size:12px;line-height:2.2;">
<div style="display:flex;justify-content:space-between;">
<div style="width:48%;">
<div>供方(甲方):${row.supplier || ''}</div>
<div>地址:${row.supplierAddress || ''}</div>
<div>电话:${row.supplierPhone || ''}</div>
<div>开户行:${row.supplierBank || ''}</div>
<div>账号:${row.supplierAccount || ''}</div>
<div>税号:${row.supplierTaxNo || ''}</div>
</div>
<div style="width:48%;">
<div>需方(乙方):${row.customer || ''}</div>
<div>地址:${row.customerAddress || ''}</div>
<div>电话:${row.customerPhone || ''}</div>
<div>开户行:${row.customerBank || ''}</div>
<div>账号:${row.customerAccount || ''}</div>
<div>税号:${row.customerTaxNo || ''}</div>
</div>
</div>
</div>
</div>
`;
// 设置合并后单元格的内容和样式
const mergedContent = pContents.join('\n');
worksheet.getCell(`A${currentRow}`).value = mergedContent;
worksheet.getCell(`A${currentRow}`).alignment = { horizontal: 'left', vertical: 'top', wrapText: true };
const container = document.createElement('div');
container.style.position = 'fixed';
container.style.left = '-9999px';
container.style.top = '0';
container.innerHTML = contractHtml;
document.body.appendChild(container);
// 调整合并后单元格的行高
const lineCount = (mergedContent.match(/\n/g) || []).length + 1;
const height = Math.max(60, lineCount * 15);
worksheet.getRow(currentRow).height = height;
const element = document.getElementById('contract-pdf-content');
await new Promise(resolve => setTimeout(resolve, 500));
// 调整currentRow
currentRow = endRow + 1;
const canvas = await html2canvas(element, {
scale: 2,
useCORS: true,
allowTaint: true,
logging: false,
backgroundColor: '#ffffff',
imageTimeout: 5000
});
document.body.removeChild(container);
const pdfWidth = 210;
const pdfHeight = 297;
const imgWidth = pdfWidth;
const imgHeight = (canvas.height * pdfWidth) / canvas.width;
const pdf = new jsPDF('p', 'mm', 'a4');
const imgData = canvas.toDataURL('image/jpeg', 0.95);
let heightLeft = imgHeight;
let position = 0;
pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
heightLeft -= pdfHeight;
while (heightLeft > 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(imgData, 'JPEG', 0, position, imgWidth, imgHeight);
heightLeft -= pdfHeight;
}
pdf.save(`合同_${row.contractCode || row.contractName || '未命名'}.pdf`);
this.$message.success('PDF导出成功');
this.exportDialogVisible = false;
} catch (error) {
console.error('PDF导出失败:', error);
this.$message.error('PDF导出失败请重试');
} finally {
loading.close();
this.exportLoading = false;
}
// 设置合同尾部信息,每一行的前四列与后四列分别合并
// 内容参考
// 设置合同尾部信息,每一行的前四列与后四列分别合并
// 内容参考ContractPreview.vue
if (currentRow > 0) {
// 空行
currentRow++;
// 供方(甲方)信息
worksheet.mergeCells(`A${currentRow}:D${currentRow}`);
worksheet.getCell(`A${currentRow}`).value = `供方(甲方):${row.supplier || '嘉祥科伦普重工有限公司'}`;
worksheet.getCell(`A${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
worksheet.mergeCells(`E${currentRow}:H${currentRow}`);
worksheet.getCell(`E${currentRow}`).value = `需方(乙方):${row.customer || ''}`;
worksheet.getCell(`E${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
currentRow++;
// 地址信息
worksheet.mergeCells(`A${currentRow}:D${currentRow}`);
worksheet.getCell(`A${currentRow}`).value = `地址:${row.supplierAddress || ''}`;
worksheet.getCell(`A${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
worksheet.mergeCells(`E${currentRow}:H${currentRow}`);
worksheet.getCell(`E${currentRow}`).value = `地址:${row.customerAddress || ''}`;
worksheet.getCell(`E${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
currentRow++;
// 电话信息
worksheet.mergeCells(`A${currentRow}:D${currentRow}`);
worksheet.getCell(`A${currentRow}`).value = `电话:${row.supplierPhone || ''}`;
worksheet.getCell(`A${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
worksheet.mergeCells(`E${currentRow}:H${currentRow}`);
worksheet.getCell(`E${currentRow}`).value = `电话:${row.customerPhone || ''}`;
worksheet.getCell(`E${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
currentRow++;
// 开户行信息
worksheet.mergeCells(`A${currentRow}:D${currentRow}`);
worksheet.getCell(`A${currentRow}`).value = `开户行:${row.supplierBank || ''}`;
worksheet.getCell(`A${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
worksheet.mergeCells(`E${currentRow}:H${currentRow}`);
worksheet.getCell(`E${currentRow}`).value = `开户行:${row.customerBank || ''}`;
worksheet.getCell(`E${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
currentRow++;
// 账号信息
worksheet.mergeCells(`A${currentRow}:D${currentRow}`);
worksheet.getCell(`A${currentRow}`).value = `账号:${row.supplierAccount || ''}`;
worksheet.getCell(`A${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
worksheet.mergeCells(`E${currentRow}:H${currentRow}`);
worksheet.getCell(`E${currentRow}`).value = `账号:${row.customerAccount || ''}`;
worksheet.getCell(`E${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
currentRow++;
// 税号信息
worksheet.mergeCells(`A${currentRow}:D${currentRow}`);
worksheet.getCell(`A${currentRow}`).value = `税号:${row.supplierTaxNo || ''}`;
worksheet.getCell(`A${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
worksheet.mergeCells(`E${currentRow}:H${currentRow}`);
worksheet.getCell(`E${currentRow}`).value = `税号:${row.customerTaxNo || ''}`;
worksheet.getCell(`E${currentRow}`).alignment = { horizontal: 'left', vertical: 'middle' };
currentRow++;
}
// 4. 设置行高
worksheet.getRow(1).height = 40;
worksheet.getRow(2).height = 30;
worksheet.getRow(3).height = 20;
worksheet.getRow(4).height = 20;
worksheet.getRow(5).height = 20;
worksheet.getRow(6).height = 20;
worksheet.getRow(7).height = 25;
// 自动调整数据行高
for (let i = 8; i <= currentRow; i++) {
if (!worksheet.getRow(i).height) {
worksheet.getRow(i).height = 20;
}
}
// 5. 导出文件
const buffer = await workbook.xlsx.writeBuffer();
const blob = new Blob([buffer], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
saveAs(blob, `合同_${row.contractCode || row.contractName || '未命名'}.xlsx`);
},
}
};