fix(print): 修复处方打印功能并优化路由查询解析

- 使用全局用户存储替代代理访问医院名称
- 更改打印方法为浏览器打印预览方式,无需客户端连接
- 添加打印样式处理和回调函数
- 在主入口文件初始化hiprint并配置本地客户端连接
- 移除重复的路由查询解析逻辑,简化代码结构
This commit is contained in:
2026-03-03 11:17:53 +08:00
parent fc0f5a11be
commit 45fdca65a7
3 changed files with 40 additions and 35 deletions

View File

@@ -96,27 +96,6 @@ function resolvePath(routePath, routeQuery) {
return { path: getNormalPath(props.basePath + '/' + routePath), query: query } return { path: getNormalPath(props.basePath + '/' + routePath), query: query }
} catch (e) { } catch (e) {
// 如果解析失败,将其作为普通字符串处理 // 如果解析失败,将其作为普通字符串处理
console.warn('Failed to parse routeQuery as JSON:', routeQuery, e);
return getNormalPath(props.basePath + '/' + routePath)
}
try {
let query = JSON.parse(routeQuery);
return { path: getNormalPath(props.basePath + '/' + routePath), query: query }
} catch (e) {
console.error('Failed to parse routeQuery:', routeQuery, e);
return getNormalPath(props.basePath + '/' + routePath)
}
try {
// 检查 routeQuery 是否已经是对象
if (typeof routeQuery === 'object') {
return { path: getNormalPath(props.basePath + '/' + routePath), query: routeQuery }
}
// 尝试解析 JSON 字符串
let query = JSON.parse(routeQuery);
return { path: getNormalPath(props.basePath + '/' + routePath), query: query }
} catch (e) {
// 如果解析失败,将其作为普通字符串处理
console.warn('Failed to parse routeQuery as JSON:', routeQuery, e);
return getNormalPath(props.basePath + '/' + routePath) return getNormalPath(props.basePath + '/' + routePath)
} }
} }

View File

@@ -3,7 +3,7 @@ import {createApp} from 'vue';
import Cookies from 'js-cookie'; import Cookies from 'js-cookie';
// 导入 hiprint 并挂载到全局 window 对象 // 导入 hiprint 并挂载到全局 window 对象
import {hiprint} from 'vue-plugin-hiprint'; import {hiprint, autoConnect, disAutoConnect} from 'vue-plugin-hiprint';
import ElementPlus, {ElDialog, ElMessage} from 'element-plus'; import ElementPlus, {ElDialog, ElMessage} from 'element-plus';
import zhCn from 'element-plus/es/locale/lang/zh-cn'; import zhCn from 'element-plus/es/locale/lang/zh-cn';
@@ -52,6 +52,32 @@ import {registerComponents} from './template';
window.hiprint = hiprint; window.hiprint = hiprint;
// 初始化 hiprint 并连接本地客户端
hiprint.init({
providers: []
});
// 延迟连接,确保 hiwebSocket 已初始化
setTimeout(() => {
if (hiprint.hiwebSocket) {
// 设置连接地址和 token
hiprint.hiwebSocket.setHost('http://localhost:17521', 'hiprint-test');
console.log('hiprint 连接地址:', hiprint.hiwebSocket.host);
// 等待连接建立
setTimeout(() => {
console.log('hiprint 连接状态:', hiprint.hiwebSocket.connected);
if (hiprint.hiwebSocket.connected) {
console.log('hiprint 客户端连接成功');
} else {
console.warn('hiprint 客户端未连接,请检查客户端是否运行');
}
}, 2000);
} else {
console.warn('hiprint.hiwebSocket 未初始化');
}
}, 500);
const app = createApp(App); const app = createApp(App);
if (chrome.webview !== undefined) { if (chrome.webview !== undefined) {

View File

@@ -465,6 +465,9 @@ import {hiprint} from 'vue-plugin-hiprint';
import templateJson from '@/components/Print/Pharmacy.json'; import templateJson from '@/components/Print/Pharmacy.json';
import chineseMedicineTemplateJson from '@/components/Print/ChineseMedicinePrescription.json'; import chineseMedicineTemplateJson from '@/components/Print/ChineseMedicinePrescription.json';
import {formatInventory} from '../../../utils/his'; import {formatInventory} from '../../../utils/his';
import useUserStore from '@/store/modules/user';
const userStore = useUserStore();
const {proxy} = getCurrentInstance(); const {proxy} = getCurrentInstance();
const showSearch = ref(true); const showSearch = ref(true);
@@ -718,21 +721,18 @@ async function printPrescription() {
// 根据药品分类选择对应的打印模板 // 根据药品分类选择对应的打印模板
const template = tcmFlag.value === '1' ? chineseMedicineTemplateJson : templateJson; const template = tcmFlag.value === '1' ? chineseMedicineTemplateJson : templateJson;
const printElements = JSON.parse( const printElements = JSON.parse(
JSON.stringify(template).replace(/{{HOSPITAL_NAME}}/g, proxy.$store.useUserStore().hospitalName) JSON.stringify(template).replace(/{{HOSPITAL_NAME}}/g, userStore.hospitalName)
); );
var hiprintTemplate = new hiprint.PrintTemplate({template: printElements}); // 定义模板 var hiprintTemplate = new hiprint.PrintTemplate({template: printElements}); // 定义模板
hiprintTemplate.print2(result, {
height: 210, // 使用浏览器打印预览方式(不需要客户端连接)
width: 148, hiprintTemplate.print(result, {}, {
}); styleHandler: () => {
// 发送任务到打印机成功 return '<style>@media print { @page { margin: 0; } }</style>';
hiprintTemplate.on('printSuccess', function (e) { },
console.log('打印成功'); callback: () => {
}); console.log('打印窗口已打开');
// 发送任务到打印机失败 }
hiprintTemplate.on('printError', function (e) {
console.log('打印失败');
proxy.$modal.msgError('打印失败,请检查打印机连接');
}); });
} catch (error) { } catch (error) {
console.error('处方打印失败:', error); console.error('处方打印失败:', error);