264 lines
8.0 KiB
PHP
264 lines
8.0 KiB
PHP
<?php
|
|
|
|
namespace Extend\RegulatoryPlatform;
|
|
|
|
use App\Constants\HttpEnumCode;
|
|
use App\Exception\BusinessException;
|
|
use App\Utils\Log;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Hyperf\Redis\Redis;
|
|
use Hyperf\Context\ApplicationContext;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\ContainerInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
/**
|
|
* 四川省互联网医疗服务监管平台
|
|
*/
|
|
class regulatoryPlatform
|
|
{
|
|
#[Inject]
|
|
protected ContainerInterface $container;
|
|
|
|
#[Inject]
|
|
protected Client $client;
|
|
|
|
#[Inject]
|
|
protected Redis $redis;
|
|
|
|
protected string $api_url;
|
|
protected string $client_id;
|
|
protected string $client_secret;
|
|
|
|
public function __construct()
|
|
{
|
|
// 请求地址
|
|
$this->api_url = \Hyperf\Config\config('regulatory_platform.api_url');
|
|
$this->client_id = \Hyperf\Config\config('regulatory_platform.client_id');
|
|
$this->client_secret = \Hyperf\Config\config('regulatory_platform.client_secret');
|
|
$this->container = ApplicationContext::getContainer();
|
|
$this->client = $this->container->get(Client::class);
|
|
}
|
|
|
|
/**
|
|
* 获取请求token
|
|
* @return string
|
|
*/
|
|
public function getAccessToken(): string
|
|
{
|
|
// 获取token
|
|
$option = [
|
|
"json" => array(
|
|
"clientId" => $this->client_id,
|
|
"appSecret" => $this->client_secret,
|
|
),
|
|
];
|
|
|
|
try {
|
|
$response = $this->httpRequest($this->api_url . 'wjw/third/oauth/getAccessToken', $option);
|
|
if (isset($response['status'])) {
|
|
if ($response['status'] != 0) {
|
|
if (!empty($response['message'])) {
|
|
throw new BusinessException($response['message']);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (empty($response['data'])) {
|
|
// 返回值为空
|
|
if (!empty($response['message'])) {
|
|
throw new BusinessException($response['message']);
|
|
}
|
|
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
|
}
|
|
|
|
$data = json_decode($response['data'], true);
|
|
if (empty($data['accessToken'])) {
|
|
// 返回值为空
|
|
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
|
}
|
|
|
|
// 默认为6天
|
|
$expires_in = 60 * 60 * 24 * 6;
|
|
if (!empty($data['expiresIn'])) {
|
|
if ($data['expiresIn'] > 100){
|
|
$expires_in = $data['expiresIn'];
|
|
}
|
|
}
|
|
|
|
$this->redis->set("regulatory_platform_access_token", $data['accessToken'],$expires_in);
|
|
|
|
return $data['accessToken'];
|
|
} catch (GuzzleException $e) {
|
|
throw new BusinessException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 上报 网络咨询(网络门诊)服务
|
|
* @param array $arg
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function uploadConsult(array $arg): array
|
|
{
|
|
try {
|
|
$this->redis = $this->container->get(Redis::class);
|
|
|
|
$access_token = $this->redis->get("regulatory_platform_access_token");
|
|
if (empty($access_token)) {
|
|
$access_token = $this->getAccessToken();
|
|
}
|
|
|
|
foreach ($arg as &$item){
|
|
$item['accessToken'] = $access_token;
|
|
$item['clientId'] = $this->client_id;
|
|
}
|
|
|
|
$option = [
|
|
"json" => $arg
|
|
];
|
|
|
|
$response = $this->httpRequest($this->api_url . '/wjw/upload/uploadConsult', $option);
|
|
if (isset($response['status'])) {
|
|
if ($response['status'] != 0) {
|
|
if (!empty($response['message'])) {
|
|
throw new BusinessException($response['message']);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $response;
|
|
} catch (GuzzleException $e) {
|
|
throw new BusinessException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 上报 网络复诊服务
|
|
* @param array $arg
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function uploadFurtherConsult(array $arg): array
|
|
{
|
|
try {
|
|
$this->redis = $this->container->get(Redis::class);
|
|
|
|
$access_token = $this->redis->get("regulatory_platform_access_token");
|
|
if (empty($access_token)) {
|
|
$access_token = $this->getAccessToken();
|
|
}
|
|
|
|
foreach ($arg as &$item){
|
|
$item['accessToken'] = $access_token;
|
|
$item['clientId'] = $this->client_id;
|
|
}
|
|
|
|
$option = [
|
|
"json" => $arg
|
|
];
|
|
|
|
$response = $this->httpRequest($this->api_url . '/wjw/upload/uploadFurtherConsult', $option);
|
|
if (isset($response['status'])) {
|
|
if ($response['status'] != 0) {
|
|
if (!empty($response['message'])) {
|
|
throw new BusinessException($response['message']);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $response;
|
|
} catch (GuzzleException $e) {
|
|
throw new BusinessException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 上报 电子处方服务
|
|
* @param array $arg
|
|
* @return array
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function uploadRecipe(array $arg): array
|
|
{
|
|
try {
|
|
$this->redis = $this->container->get(Redis::class);
|
|
|
|
$access_token = $this->redis->get("regulatory_platform_access_token");
|
|
if (empty($access_token)) {
|
|
$access_token = $this->getAccessToken();
|
|
}
|
|
|
|
foreach ($arg as &$item){
|
|
$item['accessToken'] = $access_token;
|
|
$item['clientId'] = $this->client_id;
|
|
}
|
|
|
|
$option = [
|
|
"json" => $arg
|
|
];
|
|
|
|
$response = $this->httpRequest($this->api_url . '/wjw/upload/uploadRecipe', $option);
|
|
if (isset($response['status'])) {
|
|
if ($response['status'] != 0) {
|
|
if (!empty($response['message'])) {
|
|
throw new BusinessException($response['message']);
|
|
}
|
|
}
|
|
}
|
|
|
|
return $response;
|
|
} catch (GuzzleException $e) {
|
|
throw new BusinessException($e->getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 请求封装
|
|
* @param string $path
|
|
* @param array $arg
|
|
* @return array
|
|
* @throws GuzzleException
|
|
*/
|
|
protected function httpRequest(string $path, array $arg = []): array
|
|
{
|
|
$option = [
|
|
"verify" => false
|
|
];
|
|
if (!empty($option)) {
|
|
$arg = array_merge($arg, $option);
|
|
}
|
|
|
|
Log::getInstance("regulatoryPlatform-httpRequest")->info(json_encode($arg,JSON_UNESCAPED_UNICODE));
|
|
$response = $this->client->post($path, $arg);
|
|
|
|
if ($response->getStatusCode() != '200') {
|
|
// 请求失败
|
|
throw new BusinessException($response->getBody()->getContents());
|
|
}
|
|
|
|
$body = json_decode($response->getBody(), true);
|
|
Log::getInstance("regulatoryPlatform-httpRequest")->info(json_encode($body,JSON_UNESCAPED_UNICODE));
|
|
if (empty($body)) {
|
|
// 返回值为空
|
|
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
|
}
|
|
|
|
// 特殊情况下会返回携带code的数据
|
|
if (isset($body['code'])) {
|
|
if (isset($body['message'])) {
|
|
throw new BusinessException($body['message']);
|
|
}
|
|
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
|
}
|
|
|
|
return $body;
|
|
}
|
|
} |