跳过了处方打印页面的构建

This commit is contained in:
叶锦涛
2025-11-04 15:45:12 +08:00
parent 0e1ae53194
commit e5edb6bda2

View File

@@ -755,9 +755,9 @@ async function previewAndPrint() {
// 保存预览数据 // 保存预览数据
previewData.value = result; previewData.value = result;
console.log('执行处方预览和打印'); console.log('直接执行处方打印预览');
// 构建预览HTML // 构建预览HTML(保持原有格式)
let previewHtml = ''; let previewHtml = '';
// 为每个处方生成预览内容 // 为每个处方生成预览内容
@@ -837,54 +837,153 @@ async function previewAndPrint() {
previewHtml += '<hr class="prescription-separator">'; previewHtml += '<hr class="prescription-separator">';
}); });
// 将打印数据序列化,以便在预览窗口中访问 // 创建临时打印容器(不可见)
const serializedData = encodeURIComponent(JSON.stringify(result)); const printContainer = document.createElement('div');
printContainer.style.position = 'fixed';
printContainer.style.left = '-9999px';
printContainer.style.top = '-9999px';
printContainer.style.width = '800px';
// 创建预览窗口 // 添加打印样式,确保只打印药品信息
const previewWindow = window.open('', '_blank'); const style = document.createElement('style');
if (!previewWindow) { style.textContent = `
throw new Error('无法打开预览窗口,请检查浏览器弹窗设置'); /* 确保打印样式只应用于药品信息 */
.print-content {
font-family: Arial, sans-serif;
font-size: 12px;
}
.medicine-table {
width: 100%;
border-collapse: collapse;
}
.medicine-table th,
.medicine-table td {
border: 1px solid #000;
padding: 8px;
text-align: left;
}
.medicine-table th {
background: #f2f2f2;
font-weight: bold;
}
.page-break {
page-break-after: always;
}
/* 隐藏不需要打印的元素 */
@media print {
body > *:not(#print-container) {
display: none !important;
}
#print-container {
position: static !important;
width: 100% !important;
}
}
`;
document.head.appendChild(style);
// 设置容器ID以便CSS选择
printContainer.id = 'print-container';
// 仅构建药品信息表格
let medicineHtml = '<div class="print-content">';
result.forEach((prescription, index) => {
if (!prescription) return;
const prescriptionList = Array.isArray(prescription.prescriptionList) ? prescription.prescriptionList : [];
// 只保留必要的药品信息
medicineHtml += '<div class="prescription-medicines">';
medicineHtml += '<h4>处方 ' + (index + 1) + '</h4>';
if (prescriptionList.length > 0) {
medicineHtml += '<table class="medicine-table">';
medicineHtml += '<thead>';
medicineHtml += '<tr>';
medicineHtml += '<th>药品名称</th>';
medicineHtml += '<th>规格</th>';
medicineHtml += '<th>单价</th>';
medicineHtml += '<th>数量</th>';
medicineHtml += '<th>金额</th>';
medicineHtml += '<th>用法用量</th>';
medicineHtml += '</tr>';
medicineHtml += '</thead>';
medicineHtml += '<tbody>';
prescriptionList.forEach((item) => {
medicineHtml += '<tr>';
medicineHtml += '<td>' + (item.medicineName || item.itemName || 'N/A') + '</td>';
medicineHtml += '<td>' + (item.totalVolume || item.spec || '') + '</td>';
medicineHtml += '<td>' + (item.unitPrice || item.price || '0.00') + '</td>';
medicineHtml += '<td>' + (item.quantity || 'N/A') + ' ' + (item.unitCode_dictText || item.unit || '') + '</td>';
medicineHtml += '<td>' + (item.totalPrice || '0.00') + '</td>';
// 组合用法用量信息
const usageInfo = [];
if (item.dose && item.doseUnitCode_dictText) usageInfo.push(item.dose + item.doseUnitCode_dictText);
if (item.methodCode_dictText || item.usage) usageInfo.push(item.methodCode_dictText || item.usage);
if (item.frequency) usageInfo.push(item.frequency);
medicineHtml += '<td>' + usageInfo.join('<br/>') + '</td>';
medicineHtml += '</tr>';
});
medicineHtml += '</tbody>';
medicineHtml += '<tfoot>';
medicineHtml += '<tr>';
medicineHtml += '<td colspan="4" style="text-align: right; font-weight: bold;">总计:</td>';
medicineHtml += '<td colspan="2" style="font-weight: bold;">' + (prescription.medTotalAmount || '0.00') + '</td>';
medicineHtml += '</tr>';
medicineHtml += '</tfoot>';
medicineHtml += '</table>';
} else {
medicineHtml += '<p>暂无药品信息</p>';
} }
// 使用更安全的方式生成HTML内容避免Vue编译错误 // 分页符(最后一个处方除外)
const createPreviewHtml = (prescriptionData) => { if (index < result.length - 1) {
// 简单的HTML结构避免复杂标签嵌套 medicineHtml += '<div class="page-break"></div>';
let html = '<!DOCTYPE html><html><head><meta charset="utf-8"><title>处方预览</title>'; }
// 简化的样式 medicineHtml += '</div>';
html += '<style>'; });
html += 'body{font-family:Arial,sans-serif;margin:20px;}';
html += '.content{max-width:800px;margin:0 auto;}';
html += '.header{text-align:center;margin-bottom:20px;}';
html += '.print-btn{background:#1890ff;color:white;border:none;padding:10px 20px;margin-bottom:20px;cursor:pointer;}';
html += 'table{width:100%;border-collapse:collapse;margin-bottom:20px;}';
html += 'th,td{border:1px solid #ddd;padding:8px;}';
html += 'th{background:#f2f2f2;}';
html += '.prescription{margin-bottom:40px;padding-bottom:20px;border-bottom:1px solid #ddd;}';
html += '@media print{.print-btn{display:none;}}';
html += '</style>';
html += '</head><body><div class="content">'; medicineHtml += '</div>';
html += '<div class="header"><h2>处方预览</h2></div>'; printContainer.innerHTML = medicineHtml;
html += '<button class="print-btn" onclick="window.print()">打印</button>';
// 添加处方内容 // 添加到文档
html += prescriptionData; document.body.appendChild(printContainer);
html += '</div></body></html>'; // 触发打印预览
return html; window.print();
// 打印完成后移除临时容器和样式
const cleanup = () => {
// 移除临时容器
if (printContainer && document.body.contains(printContainer)) {
document.body.removeChild(printContainer);
}
// 移除临时样式
if (style && document.head.contains(style)) {
document.head.removeChild(style);
}
// 移除事件监听器
window.removeEventListener('afterprint', cleanup);
}; };
// 使用创建的函数生成HTML并写入 // 监听打印完成事件
previewWindow.document.write(createPreviewHtml(previewHtml)); window.addEventListener('afterprint', cleanup);
previewWindow.document.close();
proxy.$modal.msgSuccess('预览成功,请在新窗口中查看并点击打印按钮'); // 添加超时清理,防止打印预览取消时资源未释放
setTimeout(() => {
cleanup();
}, 30000); // 30秒后自动清理
// 同时处理后台打印逻辑(使用原来的打印功能) proxy.$modal.msgSuccess('打印预览已启动');
// 同时处理后台打印逻辑
setTimeout(() => { setTimeout(() => {
handleBackgroundPrint(result); handleBackgroundPrint(result);
}, 1000); }, 500);
} catch (error) { } catch (error) {
console.error('预览和打印过程出错:', error); console.error('预览和打印过程出错:', error);
proxy.$modal.msgError('操作失败: ' + error.message); proxy.$modal.msgError('操作失败: ' + error.message);