Files
his/openhis-ui-vue3/src/utils/printUtils.js
chenqi a47306825a docs(requirement): 添加手术室维护界面需求文档
- 创建手术室维护界面PRD文档
- 定义页面概述、核心功能和用户价值
- 设计整体布局和页面区域详细描述
- 规范交互功能和数据结构说明
- 说明开发实现要点和注意事项
- 移除中医诊断主诊断功能实现说明文档
- 移除公告通知弹窗功能说明文档
- 移除手术人员字段不显示问题解决方案文档
- 移除手术和麻醉信息Redis缓存实现说明文档
- 移除手术室管理添加类型和所属科室字段说明文档
2026-01-13 14:41:27 +08:00

350 lines
11 KiB
JavaScript

/**
* 打印工具类
* 集中管理所有打印相关功能
*/
// 打印模板映射表 .
const TEMPLATE_MAP = {
// CLINIC_CHARGE: () => import('@/views/charge/cliniccharge/components/template.json'),
// DISPOSAL: () => import('@/views/clinicmanagement/disposal/components/disposalTemplate.json'),
//处方签
PRESCRIPTION: () => import('@/components/Print/Prescription.json'),
//处置单
DISPOSAL: () => import('@/components/Print/Disposal.json'),
//门诊日结
DAY_END: () => import('@/components/Print/DailyOutpatientSettlement.json'),
WESTERN_MEDICINE: () =>
import('@/views/pharmacymanagement/westernmedicine/components/templateJson.json'),
IN_HOSPITAL_DISPENSING: () =>
import('@/views/drug/inHospitalDispensing/components/templateJson.json'),
//门诊挂号
OUTPATIENT_REGISTRATION: () => import('@/components/Print/OutpatientRegistration.json'),
//门诊收费
OUTPATIENT_CHARGE: () => import('@/components/Print/OutpatientBilling.json'),
//门诊病历
OUTPATIENT_MEDICAL_RECORD: () => import('@/components/Print/OutpatientMedicalRecord.json'),
//门诊输液贴
OUTPATIENT_INFUSION: () => import('@/components/Print/OutpatientInfusion.json'),
//手术记录
OPERATIVE_RECORD: () => import('@/components/Print/OperativeRecord.json'),
//红旗门诊病历
HQOUTPATIENT_MEDICAL_RECORD: () => import('@/components/Print/HQOutpatientMedicalRecord.json'),
//预交金
ADVANCE_PAYMENT: () => import('@/components/Print/AdvancePayment.json'),
//中药处方单
CHINESE_MEDICINE_PRESCRIPTION: () =>
import('@/components/Print/ChineseMedicinePrescription.json'),
//药房处方单
PHARMACY_PRESCRIPTION: () => import('@/components/Print/Pharmacy.json'),
//中药医生处方单
DOC_CHINESE_MEDICINE_PRESCRIPTION: () =>
import('@/components/Print/DocChineseMedicinePrescription.json'),
};
/**
* 极简打印方法
* @param {string} templateName 打印模板名称(常量或字符串)
* @param {Array|Object} data 打印数据
* @param {string} printerName 打印机名称(可选)
* @param {Object} options 打印选项(可选)
* @returns {Promise} 打印结果Promise
*/
export async function simplePrint(templateName, data, printerName, options = {}) {
try {
// 获取模板
const template = await loadTemplate(templateName);
// 执行打印,业务名称默认为模板名称
return await executePrint(data, template, printerName, options, templateName);
} catch (error) {
console.error('打印失败:', error);
throw error;
}
}
/**
* 加载打印模板
* @param {string} templateName 模板名称
* @returns {Promise<Object>} 模板对象
*/
async function loadTemplate(templateName) {
// 如果是常量形式,获取实际的键名
const templateKey = typeof templateName === 'symbol' ? templateName.description : templateName;
if (!TEMPLATE_MAP[templateKey]) {
throw new Error(`未找到打印模板: ${templateKey}`);
}
try {
const templateModule = await TEMPLATE_MAP[templateKey]();
return templateModule.default || templateModule;
} catch (error) {
console.error(`加载模板 ${templateKey} 失败:`, error);
throw error;
}
}
/**
* 带打印机选择的极简打印方法
* @param {string} templateName 打印模板名称
* @param {Array|Object} data 打印数据
* @param {Function} showPrinterDialog 显示打印机选择对话框的函数
* @param {Object} modal 消息提示对象
* @param {Function} callback 打印完成后的回调函数(可选)
* @returns {Promise} 打印结果Promise
*/
export async function simplePrintWithDialog(
templateName,
data,
showPrinterDialog,
modal,
callback
) {
try {
// 获取模板
const template = await loadTemplate(templateName);
// 执行打印
await selectPrinterAndPrint(data, template, showPrinterDialog, modal, callback, templateName);
} catch (error) {
modal.msgError(error.message || '打印失败');
if (callback) callback(error);
}
}
// 导出模板名称常量
export const PRINT_TEMPLATE = {
// CLINIC_CHARGE: 'CLINIC_CHARGE',
DAY_END: 'DAY_END',
WESTERN_MEDICINE: 'WESTERN_MEDICINE',
IN_HOSPITAL_DISPENSING: 'IN_HOSPITAL_DISPENSING',
//门诊挂号
OUTPATIENT_REGISTRATION: 'OUTPATIENT_REGISTRATION',
//门诊收费
OUTPATIENT_CHARGE: 'OUTPATIENT_CHARGE',
//处方签
PRESCRIPTION: 'PRESCRIPTION',
//处置单
DISPOSAL: 'DISPOSAL',
//门诊病历
OUTPATIENT_MEDICAL_RECORD: 'OUTPATIENT_MEDICAL_RECORD',
//门诊输液贴
OUTPATIENT_INFUSION: 'OUTPATIENT_INFUSION',
//手术记录
OPERATIVE_RECORD: 'OPERATIVE_RECORD',
//红旗门诊病历
HQOUTPATIENT_MEDICAL_RECORD: 'HQOUTPATIENT_MEDICAL_RECORD',
//预交金
ADVANCE_PAYMENT: 'ADVANCE_PAYMENT',
//中药处方单
CHINESE_MEDICINE_PRESCRIPTION: 'CHINESE_MEDICINE_PRESCRIPTION',
//药房处方单
PHARMACY_PRESCRIPTION: 'PHARMACY_PRESCRIPTION',
//中药医生处方单
DOC_CHINESE_MEDICINE_PRESCRIPTION: 'DOC_CHINESE_MEDICINE_PRESCRIPTION',
};
/**
* 获取打印机列表
* @returns {Array} 打印机列表
*/
export function getPrinterList() {
try {
const printerList =
window.hiprint && window.hiprint.hiwebSocket
? window.hiprint.hiwebSocket.getPrinterList()
: [];
return printerList || [];
} catch (error) {
console.error('获取打印机列表失败:', error);
return [];
}
}
import useUserStore from '@/store/modules/user';
import {ElMessage} from 'element-plus';
/**
* 获取当前登录用户ID
* @returns {string} 用户ID
*/
function getCurrentUserId() {
try {
// 从Pinia store中获取当前用户ID
const userStore = useUserStore();
return userStore.id || '';
} catch (e) {
console.error('获取用户ID失败:', e);
return '';
}
}
/**
* 生成打印机缓存键
* @param {string} businessName 打印业务名称
* @returns {string} 缓存键
*/
function getPrinterCacheKey(businessName) {
const userId = getCurrentUserId();
return `selectedPrinter_${businessName || 'default'}_${userId}`;
}
/**
* 从缓存获取上次选择的打印机
* @param {string} businessName 打印业务名称
* @returns {string} 打印机名称
*/
export function getCachedPrinter(businessName = 'default') {
const cacheKey = getPrinterCacheKey(businessName);
return localStorage.getItem(cacheKey) || '';
}
/**
* 保存打印机选择到缓存
* @param {string} printerName 打印机名称
* @param {string} businessName 打印业务名称
*/
export function savePrinterToCache(printerName, businessName = 'default') {
if (printerName) {
const cacheKey = getPrinterCacheKey(businessName);
localStorage.setItem(cacheKey, printerName);
}
}
/**
* 执行打印操作
* @param {Array} data 打印数据
* @param {Object} template 打印模板
* @param {string} printerName 打印机名称(可选)
* @param {Object} options 打印选项(可选)
* @param {string} businessName 打印业务名称(可选)
* @returns {Promise} 打印结果Promise
*/
export function executePrint(data, template, printerName, options = {}, businessName = 'default') {
return new Promise((resolve, reject) => {
try {
if (!window.hiprint) {
throw new Error('打印插件未加载');
}
const userStore = useUserStore();
const processedTemplate = JSON.parse(
JSON.stringify(template).replace(/{{HOSPITAL_NAME}}/g, userStore.hospitalName)
);
const hiprintTemplate = new window.hiprint.PrintTemplate({ template: processedTemplate });
const printOptions = {
title: '打印标题',
height: 210,
width: 148,
...options,
};
// 如果指定了打印机,添加到打印选项中
if (printerName) {
printOptions.printer = printerName;
// 保存到缓存
savePrinterToCache(printerName, businessName);
}
// 打印成功回调
hiprintTemplate.on('printSuccess', function (e) {
resolve({ success: true, event: e });
});
// 打印失败回调
hiprintTemplate.on('printError', function (e) {
reject({ success: false, event: e, message: '打印失败' });
});
// 执行打印
hiprintTemplate.print2(data, printOptions);
} catch (error) {
reject({ success: false, error: error, message: error.message || '打印过程中发生错误' });
}
});
}
/**
* 选择打印机并执行打印
* @param {Array} data 打印数据
* @param {Object} template 打印模板
* @param {Function} showPrinterDialog 显示打印机选择对话框的函数
* @param {Object} modal 消息提示对象
* @param {Function} callback 打印完成后的回调函数
* @param {string} businessName 打印业务名称(可选)
*/
export async function selectPrinterAndPrint(
data,
template,
showPrinterDialog,
modal,
callback,
businessName = 'default'
) {
try {
// 获取打印机列表
const printerList = getPrinterList();
if (printerList.length === 0) {
modal.msgWarning('未检测到可用打印机');
return;
}
// 获取缓存的打印机
const cachedPrinter = getCachedPrinter(businessName);
let selectedPrinter = '';
// 判断打印机选择逻辑
if (printerList.length === 1) {
selectedPrinter = printerList[0].name;
await executePrint(data, template, selectedPrinter, {}, businessName);
if (callback) callback();
} else if (cachedPrinter && printerList.some((printer) => printer.name === cachedPrinter)) {
selectedPrinter = cachedPrinter;
await executePrint(data, template, selectedPrinter, {}, businessName);
if (callback) callback();
} else {
// 调用显示打印机选择对话框的函数
showPrinterDialog(printerList, async (chosenPrinter) => {
try {
await executePrint(data, template, chosenPrinter, {}, businessName);
if (callback) callback();
} catch (error) {
modal.msgError(error.message || '打印失败');
}
});
}
} catch (error) {
modal.msgError(error.message || '获取打印机列表失败');
}
}
// 预览打印
export function previewPrint(elementDom) {
if (elementDom) {
//初始化
window.hiprint.init();
const hiprintTemplate = new window.hiprint.PrintTemplate();
// printByHtml为预览打印
hiprintTemplate.printByHtml(elementDom, {});
} else {
ElMessage({
type: 'error',
message: '加载模版失败',
});
}
}
// 默认导出简化的打印方法
export default {
print: simplePrint,
printWithDialog: simplePrintWithDialog,
TEMPLATE: PRINT_TEMPLATE,
executePrint,
selectPrinterAndPrint,
getPrinterList,
getCachedPrinter,
savePrinterToCache,
};