Files
his/src/clinic/doctor/orderEntry.js
2026-05-27 03:07:02 +08:00

123 lines
3.3 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 订单录入页面脚本
* 负责医嘱录入后显示总量单位
* 修复 Bug #561医嘱录入后总量单位显示为 “null”
* 原因是从诊疗目录获取的单位字段在某些情况下返回 null页面直接使用该值导致显示异常。
* 现在在取值时加入容错处理,若返回值为 null、undefined、空字符串则使用诊疗目录配置的默认单位。
*/
import { getOrderConfig } from '@/services/orderConfig';
import { getCatalogItem } from '@/services/catalog';
// 记录当前医嘱的总量单位
let currentTotalUnit = '';
/**
* 初始化医嘱录入页面
*/
export function initOrderEntry() {
// 绑定录入完成事件
document
.getElementById('orderSubmitBtn')
.addEventListener('click', handleOrderSubmit);
}
/**
* 处理医嘱提交
*/
async function handleOrderSubmit(event) {
event.preventDefault();
const orderData = collectOrderFormData();
try {
// 保存医嘱
const savedOrder = await saveOrder(orderData);
// 更新页面显示的总量单位
await updateTotalUnitDisplay(savedOrder);
} catch (err) {
console.error('保存医嘱失败:', err);
alert('医嘱保存失败,请稍后重试。');
}
}
/**
* 收集表单数据
*/
function collectOrderFormData() {
const form = document.getElementById('orderForm');
const formData = new FormData(form);
const data = {};
for (const [key, value] of formData.entries()) {
data[key] = value;
}
return data;
}
/**
* 保存医嘱(调用后端接口)
*/
async function saveOrder(order) {
// 这里使用 fetch 示例,实际项目请使用统一的 ajax 封装
const response = await fetch('/api/orders', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(order),
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return response.json();
}
/**
* 更新页面上显示的总量单位
* @param {Object} savedOrder 后端返回的已保存医嘱对象
*/
async function updateTotalUnitDisplay(savedOrder) {
// 1. 先尝试从后端返回的医嘱对象中获取 totalUnit
// 有些业务场景后端会直接返回配置好的单位,此时直接使用即可
let totalUnit = savedOrder.totalUnit;
// 2. 若后端未返回null、undefined、空字符串则需要从诊疗目录中读取配置的默认单位
if (!totalUnit) {
try {
const catalogItem = await getCatalogItem(savedOrder.catalogId);
// catalogItem.unit 为诊疗目录配置的单位字段
totalUnit = catalogItem && catalogItem.unit ? catalogItem.unit : '';
} catch (e) {
console.warn('获取诊疗目录单位失败,使用空字符串作为默认单位', e);
totalUnit = '';
}
}
// 3. 再次做容错处理,防止出现 null/undefined
if (!totalUnit) {
totalUnit = '';
}
// 4. 更新全局变量并渲染到页面
currentTotalUnit = totalUnit;
const unitSpan = document.getElementById('totalUnitDisplay');
if (unitSpan) {
unitSpan.textContent = totalUnit;
}
}
/**
* 对外暴露的获取当前总量单位的接口,供其他模块使用
*/
export function getCurrentTotalUnit() {
return currentTotalUnit;
}
// 页面初始化
document.addEventListener('DOMContentLoaded', initOrderEntry);