新增对接检测所,新增医生短信推送

This commit is contained in:
2023-08-15 15:23:24 +08:00
parent a13b55d90b
commit 19ae7e6bff
9 changed files with 416 additions and 27 deletions
+96
View File
@@ -0,0 +1,96 @@
<?php
namespace Extend\Detection;
use App\Constants\HttpEnumCode;
use App\Exception\BusinessException;
use App\Model\BasicDetectionOrgan;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Context\ApplicationContext;
use Psr\Container\ContainerInterface;
use \Hyperf\Config\config;
/**
* 对接检测所
*/
class Base
{
#[Inject]
protected ContainerInterface $container;
#[Inject]
protected Client $client;
protected string $app_id;
protected string $secret; // 秘钥
protected string $request_url; // 请求地址
public function __construct(string $detection_organ_id)
{
$this->container = ApplicationContext::getContainer();
$this->client = $this->container->get(Client::class);
// 获取检测机构数据
$params = array();
$params['detection_organ_id'] = $detection_organ_id;
$basic_detection_organ = BasicDetectionOrgan::getOne($params);
if (!empty($basic_detection_organ)){
if (!empty($basic_detection_organ['app_id'])){
$this->app_id = $basic_detection_organ['app_id'];
}
if (!empty($basic_detection_organ['app_secret'])){
$this->secret = $basic_detection_organ['app_secret'];
}
$app_env = config("app_env","dev");
if ($app_env == "dev"){
if (!empty($basic_detection_organ['request_dev_url'])){
$this->request_url = $basic_detection_organ['request_dev_url'];
}
}else{
if (!empty($basic_detection_organ['request_prod_url'])){
$this->request_url = $basic_detection_organ['request_prod_url'];
}
}
}
}
/**
* 请求封装
* @param string $sign
* @param array $arg 请求参数
* @return array
* @throws GuzzleException
*/
protected function httpRequest(string $sign,array $arg = []): array
{
$option = [
"headers" => [
"source" => $sign
]
];
if (!empty($option)){
$arg = array_merge($arg,$option);
}
$response = $this->client->post($this->request_url, $arg);
if ($response->getStatusCode() != '200'){
// 请求失败
throw new BusinessException($response->getBody()->getContents());
}
$body = json_decode($response->getBody(),true);
if (empty($body)){
// 返回值为空
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
}
return $body;
}
}
+177
View File
@@ -0,0 +1,177 @@
<?php
namespace Extend\Detection;
use App\Constants\HttpEnumCode;
use App\Exception\BusinessException;
use App\Model\DetectionProject;
use App\Model\DetectionProjectPurpose;
use App\Model\Hospital;
use App\Model\OrderDetectionCase;
use App\Model\PatientFamily;
use App\Model\UserDoctor;
use App\Model\UserLocation;
use App\Utils\Log;
use Extend\Alibaba\Oss;
use GuzzleHttp\Exception\GuzzleException;
/**
* 微远检测所
*/
class Wy extends Base
{
/**
* 获取签名
* @return string
*/
public function getSign(): string
{
if (empty($this->app_id) || empty($this->secret) || empty($this->request_url)){
throw new BusinessException("缺少签名数据");
}
$time = date('Y-m-d');
return md5($this->app_id . $this->secret . $time);
}
/**
* 上报数据
* @param array|object $order_detection
*/
public function report(array|object $order_detection)
{
// 获取检测订单病例数据
$params = array();
$params['order_detection_id'] = $order_detection['order_detection_id'];
$order_detection_case = OrderDetectionCase::getOne($params);
if (empty($order_detection_case)){
throw new BusinessException("病例数据错误");
}
// 获取家庭成员数据
$params = array();
$params['family_id'] = $order_detection['family_id'];
$patient_family = PatientFamily::getOne($params);
if (empty($patient_family)){
throw new BusinessException("患者家庭成员数据错误");
}
// 获取患者地址
$params = array();
$params['user_id'] = $order_detection['user_id'];
$user_location = UserLocation::getOne($params);
if (!empty($user_location)){
$address = $user_location['province'] . $user_location['city'] . $user_location['county'] . $user_location['address'];
}
// 获取医生数据
$params = array();
$params['doctor_id'] = $order_detection['doctor_id'];
$user_doctor = UserDoctor::getOne($params);
if (empty($user_doctor)){
throw new BusinessException("医生数据错误");
}
// 获取医院数据
$params = array();
$params['hospital_id'] = $user_doctor['hospital_id'];
$hospital = Hospital::getOne($params);
if (empty($hospital)){
throw new BusinessException("医院数据错误");
}
// 获取检测项目数据
$params = array();
$params['detection_project_id'] = $order_detection['detection_project_id'];
$detection_project = DetectionProject::getOne($params);
if (empty($detection_project)){
throw new BusinessException("检测数据错误");
}
// 获取检测用途数据
$params = array();
$params['purpose_id'] = $order_detection['purpose_id'];
$detection_project_purpose = DetectionProjectPurpose::getOne($params);
if (empty($detection_project_purpose)){
throw new BusinessException("检测数据错误");
}
// 处理检测管图片
$oss = new Oss();
$pics = [];
$detection_pics = explode(',',$order_detection['detection_pic']);
foreach ($detection_pics as $key => $value){
// 去除用户签名图片第一个/ oss不识别
$value = substr($value, 1, strlen($value) - 1);
$pic = $oss->getObjectToRAM($value);
if (empty($pic)){
throw new BusinessException("检测管图片错误");
}
$pic = base64_encode($pic);
if (!$pic) {
throw new BusinessException("检测管图片下载失败");
}
$pics[$key]['pictureNo'] = $key + 1; // 图片序号
$pics[$key]['pictureName'] = "检测管图片"; // 图片名称
$pics[$key]['pictureInfo'] = $pic; // 对图片进行base64后的编码
}
// 获取签名
$sign = $this->getSign();
$arg = array();
$arg['appId'] = $this->app_id;
$arg['orderCode'] = $order_detection['detection_no']; // 订单编号
$arg['orderTime'] = date('Y-m-d H:i:s',strtotime($order_detection['created_at'])); // 订单时间
$arg['userPatientRelationships'] = $patient_family['relation']; // 操作用户与患者关系(1:本人 2:父母 3:爱人 4:子女 5:亲戚 6:其他 )
$arg['patientName'] = $order_detection['patient_name']; // 患者姓名
$arg['patientSex'] = $order_detection['patient_sex']; // 患者性别(0:未知 1:男 2:女)
$arg['patientNationality'] = $order_detection_case['nation_name']; // 患者民族
$arg['patientPastMedicalHistory'] = $order_detection_case['detection_disease_class_names']; // 患者既往病史
$arg['patientCardNo'] = $patient_family['id_number']; // 患者身份证号
$arg['patientPhone'] = $patient_family['mobile']; // 患者手机号
$arg['patientAddress'] = $address ?? ""; // 患者住址(非必填)
$arg['detectBarCode'] = $order_detection['detection_bar_code']; // 检测条形码
$arg['pictureOfDetectionTube'] = $pics; // 检测管图片数组
$arg['hospitalName'] = $hospital['hospital_name']; // 医院名称(送检单位)
$arg['doctorName'] = $user_doctor['user_name']; // 医生名称
$arg['projectName'] = $detection_project['detection_project_name']; // 检测项目名称
$arg['projectPurpose'] = $detection_project_purpose['purpose_name']; // 检测项目用途名称
$arg['payTime'] = $order_detection['pay_time']; // 支付时间
$arg['payAmount'] = $order_detection['payment_amount_total']; // 支付金额
$arg['reportUrl'] = $this->request_url; // 推送报告的回调地址
$option = [
"json" => $arg
];
try {
$response = $this->httpRequest($sign,$option);
if (empty($response)){
// 返回值错误为空
throw new BusinessException("");
}
if (!isset($response['code'])){
throw new BusinessException("上报数据返回值错误");
}
if ($response['code'] != 1){
if (!isset($response['msg'])){
throw new BusinessException("上报数据返回值错误");
}
if (empty($response['msg'])){
throw new BusinessException("上报数据返回值错误");
}
throw new BusinessException($response['msg']);
}
} catch (GuzzleException $e) {
throw new BusinessException($e->getMessage());
}
}
}