<?php
namespace common\sdks\brandDoctor;
use common\libs\CryptoTools;
class BrandDoctorSdk
{
public static function getInstance()
{
if (static::$_instance === null) {
return new static;
}
return static::$_instance;
}
protected function __construct()
{
$this->domain = \Yii::$app->params['api']['brandDoctor']['url'];
$this->appkey = \Yii::$app->params['api']['brandDoctor']['appkey'];
$this->appid = \Yii::$app->params['api']['brandDoctor']['appid'];
$this->os = \Yii::$app->params['api']['brandDoctor']['os'];
$this->version = \Yii::$app->params['api']['brandDoctor']['version'];
//设置私钥
CryptoTools::setKey($this->appkey);
//基础参数拼接
$this->baseParams = [
'appid' => $this->appid,
'noncestr' => $this->getRandChar(6),
'os' => $this->os,
'version' => $this->version,
'time' => time(),
];
}
/**
* 路由映射
*/
private $routeMap = [
'get-patient-list' => 'account/interrogation/patient-list', //获取就诊人列表
'get-patient-detail' => 'account/interrogation/get-patient', //获取就诊人详情
'save-patient' => 'account/interrogation/submit-patient', //添加、编辑就诊人信息
'del-patient' => 'account/interrogation/del-patient',//删除就诊人
];
/**
* 删除就诊人信息(不参与业务)
*/
public function deletePatient($uc_login_key = '', $patient_id = 0)
{
//请求参数
$params = [];
$params['uc_login_key'] = $uc_login_key;
$params['patient_id'] = $patient_id;
//非必填参数
$params['is_card'] = 1;//是否检验实名认证 0 不需要 1 需要
//获取签名
$data = CryptoTools::getEncryptArray($params);
$this->baseParams['data'] = $data['data'];
$url = $this->domain. $this->routeMap['del-patient'] . '?' . http_build_query($this->baseParams);
$result = $this->requestUrl($url, $this->baseParams);
if(is_array($result) && $result['code'] == 200){
return $result;
}else{
$errMsg = is_array($result) && isset($result['msg']) ? $result['msg'] : '';
YiiLog::warning("删除就诊人失败! ErrMsg:" . $errMsg, 'brandDoctor-sdk');
return $result;
}
}
/**
* 就诊人添加、编辑
*/
public function savePatient($uc_login_key = '', $formData = [], $patient_id = 0)
{
//将关系key => value位置调换
$brandDoctorRelationMap = array_flip(MedicalEscortOrder::$relationMap);
if (array_key_exists($formData['relationship'], $brandDoctorRelationMap)) {
$relationCode = $brandDoctorRelationMap[$formData['relationship']];
} else {
$relationCode = 0;
}
//请求参数
$params = [];
$params['uc_login_key'] = $uc_login_key;
$params['realname'] = $formData['realname'];
$params['id_card'] = $formData['id_card'];
$params['tel'] = $formData['tel'];
$params['relationship'] = $relationCode;
$params['platform'] = 2;//默认参数,隶属华电
//编辑时追加就诊人id
if ($patient_id != 0) {
$params['patient_id'] = $patient_id;
}
//获取签名
$data = CryptoTools::getEncryptArray($params);
$this->baseParams['data'] = $data['data'];
$url = $this->domain . $this->routeMap['save-patient'] . '?' . http_build_query($this->baseParams);
$result = $this->requestUrl($url, $this->baseParams);
if(is_array($result) && $result['code'] == 200){
return $result;
}else{
$errMsg = is_array($result) && isset($result['msg']) ? $result['msg'] : '';
YiiLog::warning("更新就诊人信息失败! ErrMsg:" . $errMsg, 'brandDoctor-sdk');
return $result;
}
}
/**
* 获取就诊人详情
*/
public function getPatientDetail($uc_login_key = '', $patient_id = 0)
{
//请求参数
$params = [];
$params['uc_login_key'] = $uc_login_key;
$params['patient_id'] = $patient_id;
//非必填参数
$params['is_card'] = 1;//是否检验实名认证 0 不需要 1 需要
//获取签名
$data = CryptoTools::getEncryptArray($params);
$this->baseParams['data'] = $data['data'];
$url = $this->domain. $this->routeMap['get-patient-detail'] . '?' . http_build_query($this->baseParams);
$result = $this->requestUrl($url);
if(is_array($result) && $result['code'] == 200){
return $result;
}else{
$errMsg = is_array($result) && isset($result['msg']) ? $result['msg'] : '';
YiiLog::warning("获取就诊人详情失败! ErrMsg:" . $errMsg, 'brandDoctor-sdk');
return $result;
}
}
/**
* 获取就诊人列表
*/
public function getPatientList($uc_login_key = '', $page = 1, $pagesize = 20)
{
//请求参数
$params = [];
$params['uc_login_key'] = $uc_login_key;
$params['page'] = $page;
$params['pagesize'] = $pagesize;
//获取签名
$data = CryptoTools::getEncryptArray($params);
$this->baseParams['data'] = $data['data'];
$url = $this->domain . $this->routeMap['get-patient-list'] . '?' . http_build_query($this->baseParams);
$result = $this->requestUrl($url);
if(is_array($result) && $result['code'] == 200){
return $result;
}else{
$errMsg = is_array($result) && isset($result['msg']) ? $result['msg'] : '';
YiiLog::warning("获取就诊人列表失败! ErrMsg:" . $errMsg, 'brandDoctor-sdk');
return $result;
}
}
/**
* 请求URL 有params为post请求无则get请求
*/
public function requestUrl($url, $params = [], $timeout = 10)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if (!empty($params)) {
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
}
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$this->result = curl_exec($ch);
if (curl_errno($ch)) {
$this->curlError(curl_error($ch), curl_errno($ch));
}
curl_close($ch);
$data = json_decode($this->result, true);
//解密数据并重新赋值data
if (!empty($data['data'])) {
$data['data'] = CryptoTools::AES256ECBDecrypt($data['data']);
}
return $data;
}
}
05-12