Files
BE_IBL/application/controllers/his/Patient.php
2026-04-15 15:23:57 +07:00

125 lines
3.4 KiB
PHP

<?php
class Patient extends MY_Controller
{
function __construct()
{
parent::__construct();
$this->HisEndPoint = "http://34.101.132.130:8080/query";
$this->Timeout = 10;
$this->token = "";
$this->userName = "adiet";
$this->password = "qwerty";
}
function get_token() {
if (! file_exists("/tmp/his-token.json")) {
$this->login($this->userName,$this->password);
} else {
$jtoken = file_get_contents("/tmp/his-token.json");
$token = json_decode($jtoken,true);
$expired = $token["expired"];
echo "Expired : $expired" . "|" . time() . "\n";
}
}
function login($username,$password,$debug = "") {
list($is_ok,$enc_password)= $this->gql(
"mutation",
"passwordEncrypt",
["password" => "String!"],
["password" => "qwerty"],
[],
"x"
);
if(! $is_ok) {
if ($debug != "") echo "Error : $password => $enc_password\n";
return false;
}
list($is_ok,$tokenResponse)= $this->gql(
"mutation",
"userLogin",
["userName" => "String!", "password" => "String!"],
["userName" => "adiet", "password" => $enc_password],
["userID","token","expired"],
"x"
);
if(!$is_ok) return false;
file_put_contents("/tmp/his-token.json",json_encode($tokenResponse));
return true;
}
function gql($type, $name, $param, $variable, $return = [],
$debug = "")
{
$prm_gql= "";
$prm_body = "";
foreach($param as $k => $v) {
if ($prm_gql!= "") $prm_gql .= ",";
$prm_gql.= '$' . $k . ":" . $v;
if ($prm_body != "") $prm_body .= ",";
$prm_body .= $k . ":$" . $k;
}
$body = [];
if ($return == []) {
$body["query"] = "{$type}($prm_gql){{$name}($prm_body)}";
} else {
$s_return = implode(" ", $return);
$body["query"] = "{$type}($prm_gql){{$name}($prm_body){ $s_return}}";
}
$body["variables"] = $variable;
$gql_body = json_encode($body);
if($debug != "") {
echo "Debug GQL: $gql_body \n";
}
$resp =$this->post($gql_body);
$j_resp = json_decode($resp,true);
if ($j_resp !== false) {
if ($j_resp["data"] != null) {
$result = $j_resp["data"][$name];
if ($debug != "") {
print_r($result);
}
return [true,$result];
}
}
return [false,$resp];
}
function test()
{
$body = $this->gql(
"mutation",
"passwordEncrypt",
["password" => "String!"],
["password" => "qwerty"],
[],
"xx"
);
}
function post($data,$token = "")
{
$ch = curl_init($this->HisEndPoint);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->Timeout / 2);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->Timeout);
if ($token == "") {
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Content-Length: " . strlen($data),
]);
} else {
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json",
"Content-Length: " . strlen($data),
"Authorization: Bearer $token",
]);
}
$result = curl_exec($ch);
if (curl_error($ch) != "") {
return "ERROR SIMRS HIS API {$this->HisEndPoint} : " . curl_error($ch) . "\n";
}
curl_close($ch);
return $result;
}
}