fix(print): 优化打印功能调试日志和修复位置ID获取问题 BUG#217

- 在printUtils.js中添加详细的打印诊断日志,包含hiprint对象检查、模板数据检查、医院名称处理等步骤
- 修复orderGroupDrawer.vue中的positionId获取逻辑,优先使用item.positionId
- 修复prescriptionlist.vue中quantity和totalPrice的计算问题
- 修复服务器端DiagTreatMAppServiceImpl中pricingFlag过滤条件处理问题
- 修复EleInvoiceMapper.xml中orgClassEnum类型的CAST转换问题
- 优化打印错误处理和异常捕获机制
- 添加完整的打印流程日志跟踪功能
This commit is contained in:
2026-03-18 18:17:54 +08:00
parent 7c29c6359f
commit 0c06b05764
7 changed files with 177 additions and 52 deletions

View File

@@ -269,23 +269,63 @@ export function executePrint(data, template, printerName, options = {}, business
return new Promise((resolve, reject) => {
try {
// 调试信息
console.log('hiprint 对象:', hiprint);
console.log('hiprint.PrintTemplate:', hiprint.PrintTemplate);
console.log('模板数据:', template);
console.log('========== 打印诊断日志开始 ==========');
console.log('[1] hiprint对象检查:');
console.log(' - hiprint存在:', !!hiprint);
console.log(' - hiprint.PrintTemplate存在:', !!(hiprint && hiprint.PrintTemplate));
console.log(' - hiprint.hiwebSocket存在:', !!(hiprint && hiprint.hiwebSocket));
console.log(' - hiprint.hiwebSocket.connected:', hiprint?.hiwebSocket?.connected);
console.log('[2] 模板数据检查:');
console.log(' - 模板类型:', typeof template);
console.log(' - 模板存在:', !!template);
console.log(' - 模板panels存在:', !!(template && template.panels));
console.log(' - panels数量:', template?.panels?.length);
if (template?.panels?.[0]) {
const panel = template.panels[0];
console.log(' - panel.name:', panel.name, '(类型:', typeof panel.name, ')');
console.log(' - panel.index:', panel.index);
console.log(' - panel.printElements数量:', panel.printElements?.length);
// 检查每个元素的类型
if (panel.printElements) {
panel.printElements.forEach((el, idx) => {
console.log(` - 元素[${idx}]:`, el.printElementType?.type, '-', el.printElementType?.title);
});
}
}
const userStore = useUserStore();
const processedTemplate = JSON.parse(
JSON.stringify(template).replace(/{{HOSPITAL_NAME}}/g, userStore.hospitalName)
);
console.log('[3] 医院名称:', userStore.hospitalName);
let processedTemplate;
try {
processedTemplate = JSON.parse(
JSON.stringify(template).replace(/{{HOSPITAL_NAME}}/g, userStore.hospitalName)
);
console.log('[4] 模板处理成功');
} catch (parseError) {
console.error('[4] 模板处理失败:', parseError);
throw new Error('模板处理失败: ' + parseError.message);
}
console.log('打印模板:', processedTemplate.panels?.[0]?.name, '元素数量:', processedTemplate.panels?.[0]?.printElements?.length);
console.log('[5] 打印数据检查:');
console.log(' - 数据类型:', typeof data);
console.log(' - 数据存在:', !!data);
if (data && typeof data === 'object') {
console.log(' - 数据字段:', Object.keys(data));
}
// 创建打印模板
let hiprintTemplate;
try {
console.log('[6] 开始创建PrintTemplate...');
hiprintTemplate = new hiprint.PrintTemplate({ template: processedTemplate });
console.log('[6] PrintTemplate创建成功:', hiprintTemplate);
} catch (templateError) {
console.error('创建打印模板失败:', templateError);
console.error('[6] 创建打印模板失败:', templateError);
console.error('错误堆栈:', templateError.stack);
console.error('模板内容:', JSON.stringify(processedTemplate, null, 2));
throw new Error('打印模板创建失败: ' + templateError.message);
}
@@ -296,47 +336,79 @@ export function executePrint(data, template, printerName, options = {}, business
width: 148,
...options,
};
console.log('[7] 打印选项:', printOptions);
// 检查客户端是否连接
const isClientConnected = hiprint.hiwebSocket && hiprint.hiwebSocket.connected;
console.log('[8] 客户端连接状态:', isClientConnected);
// 如果指定了打印机且客户端已连接,添加到打印选项中
if (printerName && isClientConnected) {
printOptions.printer = printerName;
// 保存到缓存
savePrinterToCache(printerName, businessName);
console.log('[8] 使用指定打印机:', printerName);
}
// 打印成功回调
hiprintTemplate.on('printSuccess', function (e) {
console.log('[9] 打印成功:', e);
console.log('========== 打印诊断日志结束 ==========');
resolve({ success: true, event: e });
});
// 打印失败回调
hiprintTemplate.on('printError', function (e) {
console.error('[9] 打印失败:', e);
console.log('========== 打印诊断日志结束 ==========');
reject({ success: false, event: e, message: '打印失败' });
});
// 根据客户端连接状态选择打印方式
console.log('[10] 开始执行打印...');
if (isClientConnected && printerName) {
// 客户端已连接且指定了打印机,使用静默打印
hiprintTemplate.print2(data, printOptions);
console.log('[10] 使用print2静默打印');
try {
hiprintTemplate.print2(data, printOptions);
console.log('[10] print2调用完成');
} catch (print2Error) {
console.error('[10] print2调用失败:', print2Error);
console.error('[10] print2错误堆栈:', print2Error.stack);
throw new Error('print2打印失败: ' + print2Error.message);
}
} else {
// 客户端未连接或未指定打印机,使用浏览器打印预览(不需要客户端连接)
console.log('打印客户端未连接,使用浏览器打印预览方式');
hiprintTemplate.print(data, printOptions, {
styleHandler: () => {
return '<style>@media print { @page { margin: 0; } }</style>';
},
callback: () => {
console.log('打印窗口已打开');
}
});
console.log('[10] 使用print浏览器打印预览');
console.log('[10] hiprintTemplate.print方法存在:', typeof hiprintTemplate.print === 'function');
try {
hiprintTemplate.print(data, printOptions, {
styleHandler: () => {
console.log('[10] styleHandler被调用');
return '<style>@media print { @page { margin: 0; } }</style>';
},
callback: (e) => {
console.log('[10] 打印回调被调用:', e);
}
});
console.log('[10] print调用完成');
} catch (printError) {
console.error('[10] print调用失败:', printError);
console.error('[10] print错误堆栈:', printError.stack);
throw new Error('print打印失败: ' + printError.message);
}
// 浏览器打印模式下直接resolve因为打印窗口已打开
console.log('[10] 浏览器打印模式,直接返回成功');
console.log('========== 打印诊断日志结束 ==========');
resolve({ success: true, message: '打印窗口已打开' });
}
} catch (error) {
reject({ success: false, error: error, message: error.message || '打印过程中发生错误' });
console.error('[ERROR] 打印过程中发生错误:', error);
console.error('[ERROR] 错误类型:', error?.constructor?.name);
console.error('[ERROR] 错误消息:', error?.message);
console.error('[ERROR] 错误堆栈:', error?.stack);
console.log('========== 打印诊断日志结束 ==========');
reject({ success: false, error: error, message: error?.message || '打印过程中发生错误' });
}
});
}