香洲二院小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
4.7 KiB

<?php
declare(strict_types=1);
namespace App\Services\HealthRecordAuth;
use App\Utils\SM4;
use App\Utils\Traits\HttpRequest;
use Exception;
class Client
{
use HttpRequest;
//api接口地址
private string $api_url = 'https://yyzc.gdgov.cn/ebus/jiankangdangan/gdehr/dhccApi/codeAuthRec/save';
// 医院编号
private string $org_code;
// 医院名称
private string $org_name;
// App ID
private string $app_id;
private string $paas_id;
private string $paas_token;
// sm4 加密密钥
private string $sm4_encrypt;
/**
* Client constructor.
*/
public function __construct()
{
$this->org_code = env('HEALTH_RECORD_HOSPITAL_COST_CODE', '');
$this->org_name = env('HEALTH_RECORD_HOSPITAL_COST_NAME', '');
$this->app_id = env('HEALTH_RECORD_HOSPITAL_APP_ID', '');
$this->paas_id = env('HEALTH_RECORD_HOSPITAL_PAAS_ID', '');
$this->paas_token = env('HEALTH_RECORD_HOSPITAL_PAAS_TOKEN', '');
$this->sm4_encrypt = env('HEALTH_RECORD_HOSPITAL_SM4_ENCRYPT', '');
}
/**
* 获取请求header
* @return string[]
*/
private function getRequestHeaders(): array
{
$timestamp = time();
$nonceStr = $this->getNonceStr(11);
$sign = $this->getSign((string) $timestamp, $nonceStr);
return [
'x-tif-timestamp:'. $timestamp,
'x-tif-paasid:'. $this->paas_id,
'x-tif-nonce:'. $nonceStr,
'x-tif-signature:'. $sign
];
}
/**
* 获取随机字符串
* @param int $length
* @return string
*/
private function getNonceStr(int $length = 10): string
{
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
$str = '';
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
/**
* 生成签名
* @param string $timestamp
* @param string $nonceStr
* @return string
*/
private function getSign(string $timestamp, string $nonceStr): string
{
$signature = $timestamp. $this->paas_token. $nonceStr. $timestamp;
// 使用SHA256算法对字符串进行哈希,返回就是十六进制字符串,无需再转
$signature = hash('sha256', $signature);
// 转全大写
return strtoupper($signature);
}
/**
* 记录日志
* @param array $requestData
* @param mixed|NULL $responseData
* @return void
*/
private function recordLog(array $requestData, mixed $responseData = NULL): void
{
date_default_timezone_set("Asia/Shanghai");
$content = '[REQUEST TIME]'. date('Y-m-d H:i:s'). "\r\n".
'[REQUEST DATA]'. json_encode($requestData, JSON_UNESCAPED_UNICODE). "\r\n";
if (!empty($responseData)) {
$content .= '[RESPONSE DATA]'. json_encode($responseData, JSON_UNESCAPED_UNICODE). "\r\n";
}
$filePath = "log". DIRECTORY_SEPARATOR. 'HealthRecordAuthLog'. DIRECTORY_SEPARATOR. date('Ym'). DIRECTORY_SEPARATOR;
$fileName = date('d'). '.log';
!is_dir($filePath) && mkdir($filePath, 0755, true);
file_put_contents($filePath. $fileName, $content. "\r\n", FILE_APPEND);
}
/**
* api 请求方式 RESETFul
* @param array $data
* @param string $type
* @return bool|mixed|string
*/
private function apiRequest(array $data, string $type = 'POST'): mixed
{
$headers = $this->getRequestHeaders();
try {
$response = $this->request($type, $this->api_url, [
'headers' => $headers,
'json' => $data
]);
$response = json_decode($response, true);
$this->recordLog($data, $response);
return $response;
} catch (Exception $e) {
return false;
}
}
/**
* 申请授权
* @param string $patientName
* @param string $patientCardNo
* @param int $status
* @return bool|mixed|string
* @throws Exception
*/
public function applyAuth(string $patientName, string $patientCardNo, int $status = 0): mixed
{
$sm4 = new SM4($this->sm4_encrypt);
$patientCardNo = $sm4->encrypt($patientCardNo);
$data = [
'codeAuthRec' => [
'authDays' => 30, // 固定30天
'patientIdcard' => $patientCardNo,
'patientName' => $patientName,
'org_code' => $this->org_code,
'org_name' => $this->org_name,
'status' => $status,
'app_id' => $this->app_id
]
];
return $this->apiRequest($data);
}
}