owned this note
owned this note
Published
Linked with GitHub
# FHIR 開發範例程式碼
[TOC]
### 實作初診基本資料表單登錄
#### 參照連結
**範例表單**:[衛福部初診病患基本資料卡空白表格](https://www.ccd.mohw.gov.tw/public/forms/e831aad3547e45eae064a58922202bd5.pdf)
#### 範例網頁表單(index.html)
```htmlembedded=
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<script src="./fhir.js"></script>
<title>Hello World!</title>
</head>
<body>
<h1>病患初診資料表</h1>
<form action="">
<label for="">姓名</label>
<input type="text" id="name">
<br><br>
<label for="">出生日期</label>
<input type="date" id="birthDate">
<br><br>
<label for="">性別</label>
<input type="radio" name="gender" id="gender_male" value="male">男
<input type="radio" name="gender" id="gender_female" value="female">女
<br><br>
<label for="">身分證字號</label>
<input type="text" id="uid">
<br><br>
<label for="">連絡電話(公)</label>
<input type="text" id="phone_work">
<br><br>
<label for="">連絡電話(宅)</label>
<input type="text" id="phone_home">
<br><br>
<label for="">連絡電話(手機)</label>
<input type="text" id="phone_mobile">
<br><br>
<label for="">聯絡地址</label>
<input type="text" id="address">
<br><br>
<label for="">電子信箱</label>
<input type="text" id="mail">
<br><br>
<label for="">緊急聯絡人-姓名</label>
<input type="text" id="contact_name">
<br><br>
<label for="">緊急聯絡人-關係</label>
<input type="text" id="contact_relationship">
<br><br>
<label for="">緊急聯絡人-聯絡電話</label>
<input type="text" id="contact_phone">
<br><br>
<button onclick="uploadFhirData()">送出</button>
</form>
</body>
</html>
```
#### 範例程式碼(fhir.js)
```javascript=
function convertToFhir(data){
const fhirData = {
resourceType: "Patient",
identifier: [{
use: "official",
type: {
"system": "http://terminology.hl7.org/CodeSystem/v2-0203",
"code": "NI",
"display": "身分證字號"
},
value: data.uid
}],
name: [{
use: "official",
text: data.name
}],
birthDate: data.birthDate,
gender: data.gender,
telecom: [
{
system: "phone",
value: data.phone_home,
use: "home"
},
{
system: "phone",
value: data.phone_work,
use: "work"
}, {
system: "phone",
value: data.phone_mobile,
use: "mobile"
}, {
system: "email",
value: data.mail,
use: "home"
}
],
address: data.address,
contact: [{
relationship: [{
text: data.contact_relationship
}],
name: {
use: "official",
text: data.contact_name
},
telecom: [{
system: "phone",
value: data.contact_phone,
use: "home"
}]
}],
};
return fhirData;
}
function uploadFhirData(){
// 從表單抓取資料
const data = {
name: document.getElementById("name").value, // 姓名
birthDate: document.getElementById("birthDate").value, // 生日
gender: document.getElementById("gender_male").checked ? "male" : "female", // 性別
uid: document.getElementById("uid").value, // 身份證字號
phone_work: document.getElementById("phone_work").value, // 連絡電話(公)
phone_home: document.getElementById("phone_home").value, // 連絡電話 (宅)
phone_mobile: document.getElementById("phone_mobile").value, // 連絡電話 (手機)
address: document.getElementById("address").value, // 聯絡地址
mail: document.getElementById("mail").value, // 電子信箱
contact_name: document.getElementById("contact_name").value, // 緊急聯絡人-姓名
contact_relationship: document.getElementById("contact_relationship").value, // 緊急聯絡人-關係
contact_phone: document.getElementById("contact_phone").value // 連絡電話
}
console.log(convertToFhir(data));
}
```
### 以 node.js 操作 FHIR Server
#### 範例病人資料(payload.json)
```json=
{
"resourceType": "Patient",
"name": [
{
"use": "official",
"text": "王大明",
"family": "王",
"given": [
"大明"
]
}
],
"gender": "male",
"telecom": [
{
"system": "phone",
"value": "0912345678",
"use": "mobile"
}
],
"birthDate": "1995-01-01",
"address": [
{
"use": "home",
"type": "physical",
"text": "高雄市小港區大馬路999號",
"line": [
"大馬路999號"
],
"city": "高雄市",
"district": "小港區",
"postalCode": "812",
"country": "TW"
}
]
}
```
#### 範例程式碼(main.js )
```javascript=
const axios = require('axios'); // 引入 axios
const util = require('util');
const payload = require('./payload.json'); // 引入 payload
const baseURL = "https://hapi.fhir.tw/baseDstu3"; // 設定 Base URL
axios.post(`${baseURL}/Patient`, payload) // 傳送 POST 請求
.then(res => {
console.log("傳送成功" + util.inspect(res.data, {
depth: null
}));
}).catch(err => {
console.log("發生錯誤" + util.inspect(err, {
depth: null
}));
});
```
#### 設定 node.js 環境
+ 創建空資料夾,然後 npm init
```bash=
$ npm init
```
+ 安裝 dependencies
```bash=
$ npm i -S axios
```
+ 新增 payload.json,並寫入範例病人資料
+ 新增 main.js,並寫入範例程式碼
+ 執行程式
```bash=
$ node main.js
```