Node.js 數(shù)據(jù)格式同時支持JSON和HL7
一個正常的HL7格式的數(shù)據(jù)大概長這樣:
MSH|^~\&|HIS|SFPH|NIS|SFPH|20140626114850.755|N|ZK~S^Z01^ZKS_Z01|NISGetHisDept-20140626114850755|P|2.7
Z01|0|部門ID|部門名稱|部門性質(zhì)|實(shí)有床位|開放床位數(shù)|預(yù)繳金下限颜武,第一次預(yù)繳金的下限|催款下限,病區(qū)提示催款的下限|記賬下限市咽,病區(qū)病人實(shí)際余額的下限|允許欠費(fèi)上限|對應(yīng)門診科室|對應(yīng)住院科室|病區(qū)急診床位數(shù)|科室共享床位數(shù)|拼音首碼|五筆首碼|操作人|操作時間
經(jīng)過一番閱讀HL7的標(biāo)準(zhǔn),成功地總結(jié)出HL7就是一堆段頭領(lǐng)著一幫段信息纵寝,一級隔斷用|
舆绎,二級隔斷用^
,三級隔斷用~
秸歧,別的就隨意了厨姚。具體內(nèi)部消息格式自己定義。于是键菱,在當(dāng)下階段谬墙,先把HL7與JSON格式之間的互相轉(zhuǎn)換做出來就好,以后遇到正式的業(yè)務(wù)邏輯再做相應(yīng)更改经备。
let hl7Body = {};
let hl72Json = (hl7Str) => {
let payLoadArray = hl7Str.split('\r\n');
for (let countX = 0; countX < payLoadArray.length; countX++) {
hl7Body[payLoadArray[countX].substring(0, 3)] = payLoadArray[countX].substring(4).split('|');
for (let countY = 0; countY < hl7Body[payLoadArray[countX].substring(0, 3)].length; countY++) {
// console.log(hl7Body[payLoadArray[countX].substring(0, 3)][countY] + ':' + hl7Body[payLoadArray[countX].substring(0, 3)][countY].indexOf('^'));
if (hl7Body[payLoadArray[countX].substring(0, 3)][countY].indexOf('^') !== -1 && ((payLoadArray[countX].substring(0, 3) !== 'MSH') || (countY !== 0))) {
hl7Body[payLoadArray[countX].substring(0, 3)][countY] = hl7Body[payLoadArray[countX].substring(0, 3)][countY].split('^');
if (Array.isArray(hl7Body[payLoadArray[countX].substring(0, 3)][countY])) {
for (let countZ = 0; countZ < hl7Body[payLoadArray[countX].substring(0, 3)][countY].length; countZ++) {
if (hl7Body[payLoadArray[countX].substring(0, 3)][countY][countZ].indexOf('~') !== -1) {
hl7Body[payLoadArray[countX].substring(0, 3)][countY][countZ] = hl7Body[payLoadArray[countX].substring(0, 3)][countY][countZ].split('~');
}
}
}
}
}
}
setTimeout(() => {
console.log(hl7Body);
// console.log(hl7Body.MSH);
json2Hl7(hl7Body);
}, 500);
}
let json2Hl7 = (jsonObj) => {
let hl7Str = '';
for (let x in jsonObj) {
hl7Str = hl7Str + '\r\n' + x + '|' + getResult(jsonObj[x], 1);
}
setTimeout(() => {
// console.log(hl7Str.substring(2));
}, 500)
}
let getResult = (inputParam, level) => {
let tmpStr = '';
switch (level) {
case 1:
if (Array.isArray(inputParam)) {
let tmpCount = 0;
for (let tmpCell in inputParam) {
if (++tmpCount < inputParam.length) {
tmpStr = tmpStr + getResult(inputParam[tmpCell], 2) + '|';
} else {
return tmpStr + getResult(inputParam[tmpCell], 2);
}
}
} else {
return inputParam;
}
break;
case 2:
if (Array.isArray(inputParam)) {
let tmpCount = 0;
for (let tmpCell in inputParam) {
if (++tmpCount < inputParam.length) {
tmpStr = tmpStr + getResult(inputParam[tmpCell], 3) + '^';
} else {
return tmpStr + getResult(inputParam[tmpCell], 3);
}
}
} else {
return inputParam;
}
break;
case 3:
if (Array.isArray(inputParam)) {
let tmpCount = 0;
for (let tmpCell in inputParam) {
if (++tmpCount < inputParam.length) {
tmpStr = tmpStr + getResult(inputParam[tmpCell], 4) + '~';
} else {
return tmpStr + getResult(inputParam[tmpCell], 4);
}
}
} else {
return inputParam;
}
break;
default:
return inputParam;
}
}
module.exports = {
hl72Json: hl72Json,
json2Hl7: json2Hl7
}
調(diào)用如下:
let fs = require('fs');
let path = require('path');
let handler = require('./main_sim_4');
// -----------------------------------------------------------------------
function test(filename) {
var filepath = path.normalize(path.join(__dirname, filename));
fs.readFile(filepath, 'utf8', function (err, xmlStr) {
let jsonBody = handler.hl72Json(xmlStr);
// console.log('======================')
//console.log(jsonBody);
//let xml = handler.json2xml(jsonBody);
//console.log(xml);
});
};
test('hl7payload2.txt');