317 lines
11 KiB
PHP
317 lines
11 KiB
PHP
<?php
|
||
|
||
namespace Extend\Prescription;
|
||
|
||
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\Utils\ApplicationContext;
|
||
use Psr\Container\ContainerExceptionInterface;
|
||
use Psr\Container\ContainerInterface;
|
||
use Psr\Container\NotFoundExceptionInterface;
|
||
|
||
class Prescription
|
||
{
|
||
#[Inject]
|
||
protected ContainerInterface $container;
|
||
|
||
#[Inject]
|
||
protected Client $client;
|
||
|
||
#[Inject]
|
||
protected Redis $redis;
|
||
|
||
protected string $api_url;
|
||
protected string $client_id;
|
||
protected string $client_secret;
|
||
protected string $pharmacy_code;
|
||
protected array $header;
|
||
public string $version = "v1";
|
||
|
||
public function __construct(){
|
||
// 请求地址
|
||
$this->api_url = config('prescription_platform.api_url');
|
||
$this->client_id = config('prescription_platform.client_id');
|
||
$this->client_secret = config('prescription_platform.client_secret');
|
||
$this->pharmacy_code = config('prescription_platform.pharmacy_code');
|
||
$this->container = ApplicationContext::getContainer();
|
||
$this->client = make(Client::class, [
|
||
'config' => [
|
||
'timeout' => 5,
|
||
],
|
||
]);
|
||
}
|
||
|
||
/**
|
||
* 获取token接口
|
||
* @return string token数据
|
||
*/
|
||
protected function getToken(): string
|
||
{
|
||
$option = [
|
||
"json" => array(
|
||
"clientId" => $this->client_id,
|
||
"clientSecret" => $this->client_secret,
|
||
)
|
||
];
|
||
|
||
try {
|
||
$response = $this->httpRequest($this->api_url . $this->version . '/user_thrid/token', $option);
|
||
if (empty($response['data'])){
|
||
// 返回值为空
|
||
if (!empty($response['message'])){
|
||
throw new BusinessException($response['message']);
|
||
}
|
||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||
}
|
||
|
||
if (empty($response['data']['result'])){
|
||
// 返回值为空
|
||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||
}
|
||
|
||
if (empty($response['data']['result']['token'])){
|
||
// 返回值为空
|
||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||
}
|
||
|
||
} catch (GuzzleException $e) {
|
||
throw new BusinessException($e->getMessage());
|
||
}
|
||
|
||
$this->header = [
|
||
"Authorization" => "Bearer " . $response['data']['result']['token']
|
||
];
|
||
|
||
$this->redis->set("prescription_token",$response['data']['result']['token'],60 * 60 * 1.5);
|
||
return $response['data']['result']['token'];
|
||
}
|
||
|
||
/**
|
||
* 获取药品
|
||
* @param int $page
|
||
* @param int $pageSize
|
||
* @return mixed
|
||
*/
|
||
public function getProd(int $page = 1,int $pageSize = 10): mixed
|
||
{
|
||
$option = [
|
||
"json" => array(
|
||
"page" => $page,
|
||
"pageSize" => $pageSize,
|
||
),
|
||
];
|
||
|
||
// Log::getInstance()->info("处方平台获取药品请求数据:" . json_encode($option,JSON_UNESCAPED_UNICODE));
|
||
try {
|
||
$response = $this->httpRequest($this->api_url . $this->version . '/drug/syncDrugCatalogue', $option);
|
||
if (empty($response['data'])){
|
||
// 返回值为空
|
||
if (!empty($response['message'])){
|
||
Log::getInstance()->error("处方平台获取药品返回数据为空:" . json_encode($response,JSON_UNESCAPED_UNICODE));
|
||
throw new BusinessException($response['message']);
|
||
}
|
||
Log::getInstance()->error("处方平台获取药品返回数据为空,无错误信息");
|
||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||
}
|
||
|
||
if (empty($response['data']['result'])){
|
||
// 返回值为空
|
||
// Log::getInstance()->error("处方平台获取药品返回result为空:" . json_encode($response,JSON_UNESCAPED_UNICODE));
|
||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||
}
|
||
|
||
// Log::getInstance()->info("处方平台获取药品返回数据:" . json_encode($response,JSON_UNESCAPED_UNICODE));
|
||
return $response['data']['result'];
|
||
} catch (GuzzleException $e) {
|
||
Log::getInstance()->error("处方平台获取药品请求异常:" . $e->getMessage());
|
||
throw new BusinessException($e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取商品库存
|
||
* @param string $product_platform_code 处方平台商品编码(此处使用第三方药店商品编码!)
|
||
* @return array
|
||
*/
|
||
public function getProdStock(string $product_platform_code): array
|
||
{
|
||
$option = [
|
||
"json" => array(
|
||
"pharmacyCode" => $this->pharmacy_code,
|
||
"drugCode" => $product_platform_code,
|
||
),
|
||
];
|
||
|
||
try {
|
||
$response = $this->httpRequest($this->api_url . $this->version . '/pharmacy/pharmacyInventory', $option);
|
||
if (empty($response['data'])){
|
||
// 返回值为空
|
||
if (!empty($response['message'])){
|
||
throw new BusinessException($response['message']);
|
||
}
|
||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||
}
|
||
|
||
return $response['data'];
|
||
} catch (GuzzleException $e) {
|
||
throw new BusinessException($e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取运费
|
||
* @return array
|
||
*/
|
||
public function getLogisticsFee(): array
|
||
{
|
||
$option = [
|
||
"json" => array(
|
||
"pharmacyCode" => $this->pharmacy_code,
|
||
),
|
||
];
|
||
|
||
try {
|
||
$response = $this->httpRequest($this->api_url . $this->version . '/pharmacy/transportationExpenses', $option);
|
||
if (empty($response['data'])){
|
||
// 返回值为空
|
||
if (!empty($response['resultDesc'])){
|
||
throw new BusinessException($response['resultDesc']);
|
||
}
|
||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||
}
|
||
|
||
return $response['data'];
|
||
} catch (\Throwable $e) {
|
||
throw new BusinessException($e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 上报处方
|
||
* @param array $arg
|
||
* @return array
|
||
* @throws ContainerExceptionInterface
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
public function reportPrescription(array $arg): array
|
||
{
|
||
$option = [
|
||
"json" => $arg
|
||
];
|
||
|
||
// Log::getInstance()->info("处方平台上报数据:" . json_encode($option,JSON_UNESCAPED_UNICODE));
|
||
try {
|
||
$response = $this->httpRequest($this->api_url . $this->version . '/preOrder/receivePreOrder', $option);
|
||
if (empty($response)){
|
||
// 返回值错误为空
|
||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||
}
|
||
// Log::getInstance()->info("处方平台返回数据:" . json_encode($response,JSON_UNESCAPED_UNICODE));
|
||
return $response;
|
||
} catch (GuzzleException $e) {
|
||
throw new BusinessException($e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 获取处方订单
|
||
* @param string $order_product_no 商品订单号
|
||
* @param string $prescription_code 处方单号
|
||
* @return array
|
||
*/
|
||
public function getPrescription(string $order_product_no,string $prescription_code): array
|
||
{
|
||
$terminal_code = config('prescription_platform.terminal_code');
|
||
$option = [
|
||
"json" => [
|
||
"terminalCode" => $terminal_code,
|
||
"orderNo" => $order_product_no,
|
||
"prescriptionNo" => $prescription_code,
|
||
]
|
||
];
|
||
|
||
try {
|
||
$response = $this->httpRequest($this->api_url . $this->version . '/prescription/searchPresStatus', $option);
|
||
if (empty($response)){
|
||
// 返回值错误为空
|
||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||
}
|
||
|
||
if (empty($response['data'])){
|
||
// 返回值为空
|
||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||
}
|
||
|
||
if (empty($response['data']['result'])){
|
||
// 返回值为空
|
||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||
}
|
||
|
||
return $response['data']['result'];
|
||
} catch (GuzzleException $e) {
|
||
throw new BusinessException($e->getMessage());
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 请求封装
|
||
* @param string $path
|
||
* @param array $arg
|
||
* @return array
|
||
* @throws ContainerExceptionInterface
|
||
* @throws GuzzleException
|
||
* @throws NotFoundExceptionInterface
|
||
*/
|
||
protected function httpRequest(string $path,array $arg = []): array
|
||
{
|
||
if ($path != $this->api_url . "v1/user_thrid/token"){
|
||
$this->redis = $this->container->get(Redis::class);
|
||
|
||
$access_token = $this->redis->get("prescription_token");
|
||
if (empty($access_token)){
|
||
$access_token = $this->getToken();
|
||
}
|
||
}
|
||
|
||
if (!empty($access_token)){
|
||
$option = [
|
||
"headers" => [
|
||
"Authorization" => "Bearer " . $access_token
|
||
]
|
||
];
|
||
}
|
||
|
||
if (!empty($option)){
|
||
$arg = array_merge($arg,$option);
|
||
}
|
||
|
||
// 记录请求日志(隐藏敏感信息)
|
||
$logArg = $arg;
|
||
|
||
// Log::getInstance()->info("处方平台HTTP请求:" . $path . ",请求参数:" . json_encode($logArg,JSON_UNESCAPED_UNICODE));
|
||
|
||
$response = $this->client->post($path, $arg);
|
||
|
||
if ($response->getStatusCode() != '200'){
|
||
// 请求失败
|
||
$errorBody = $response->getBody()->getContents();
|
||
// Log::getInstance()->error("处方平台HTTP请求失败,状态码:" . $response->getStatusCode() . ",错误信息:" . $errorBody);
|
||
throw new BusinessException($errorBody);
|
||
}
|
||
|
||
$body = json_decode($response->getBody(),true);
|
||
if (empty($body)){
|
||
// 返回值为空
|
||
// Log::getInstance()->error("处方平台HTTP请求返回值为空,路径:" . $path);
|
||
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
||
}
|
||
|
||
// Log::getInstance()->info("处方平台HTTP请求成功,路径:" . $path . ",返回数据:" . json_encode($body,JSON_UNESCAPED_UNICODE));
|
||
return $body;
|
||
}
|
||
} |