跳过了处方打印页面的构建
This commit is contained in:
@@ -755,9 +755,9 @@ async function previewAndPrint() {
|
||||
// 保存预览数据
|
||||
previewData.value = result;
|
||||
|
||||
console.log('执行处方预览和打印');
|
||||
console.log('直接执行处方打印预览');
|
||||
|
||||
// 构建预览HTML
|
||||
// 构建预览HTML(保持原有格式)
|
||||
let previewHtml = '';
|
||||
|
||||
// 为每个处方生成预览内容
|
||||
@@ -837,54 +837,153 @@ async function previewAndPrint() {
|
||||
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');
|
||||
if (!previewWindow) {
|
||||
throw new Error('无法打开预览窗口,请检查浏览器弹窗设置');
|
||||
}
|
||||
// 添加打印样式,确保只打印药品信息
|
||||
const style = document.createElement('style');
|
||||
style.textContent = `
|
||||
/* 确保打印样式只应用于药品信息 */
|
||||
.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);
|
||||
|
||||
// 使用更安全的方式生成HTML内容,避免Vue编译错误
|
||||
const createPreviewHtml = (prescriptionData) => {
|
||||
// 简单的HTML结构,避免复杂标签嵌套
|
||||
let html = '<!DOCTYPE html><html><head><meta charset="utf-8"><title>处方预览</title>';
|
||||
// 设置容器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 : [];
|
||||
|
||||
// 简化的样式
|
||||
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>';
|
||||
// 只保留必要的药品信息
|
||||
medicineHtml += '<div class="prescription-medicines">';
|
||||
medicineHtml += '<h4>处方 ' + (index + 1) + '</h4>';
|
||||
|
||||
html += '</head><body><div class="content">';
|
||||
html += '<div class="header"><h2>处方预览</h2></div>';
|
||||
html += '<button class="print-btn" onclick="window.print()">打印</button>';
|
||||
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 += prescriptionData;
|
||||
// 分页符(最后一个处方除外)
|
||||
if (index < result.length - 1) {
|
||||
medicineHtml += '<div class="page-break"></div>';
|
||||
}
|
||||
|
||||
html += '</div></body></html>';
|
||||
return html;
|
||||
medicineHtml += '</div>';
|
||||
});
|
||||
|
||||
medicineHtml += '</div>';
|
||||
printContainer.innerHTML = medicineHtml;
|
||||
|
||||
// 添加到文档
|
||||
document.body.appendChild(printContainer);
|
||||
|
||||
// 触发打印预览
|
||||
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));
|
||||
previewWindow.document.close();
|
||||
// 监听打印完成事件
|
||||
window.addEventListener('afterprint', cleanup);
|
||||
|
||||
proxy.$modal.msgSuccess('预览成功,请在新窗口中查看并点击打印按钮');
|
||||
// 添加超时清理,防止打印预览取消时资源未释放
|
||||
setTimeout(() => {
|
||||
cleanup();
|
||||
}, 30000); // 30秒后自动清理
|
||||
|
||||
// 同时处理后台打印逻辑(使用原来的打印功能)
|
||||
proxy.$modal.msgSuccess('打印预览已启动');
|
||||
|
||||
// 同时处理后台打印逻辑
|
||||
setTimeout(() => {
|
||||
handleBackgroundPrint(result);
|
||||
}, 1000);
|
||||
}, 500);
|
||||
} catch (error) {
|
||||
console.error('预览和打印过程出错:', error);
|
||||
proxy.$modal.msgError('操作失败: ' + error.message);
|
||||
|
||||
Reference in New Issue
Block a user