修改发送手机号
This commit is contained in:
@@ -2,10 +2,168 @@
|
||||
|
||||
namespace Extend\RegulatoryPlatform;
|
||||
|
||||
use App\Constants\HttpEnumCode;
|
||||
use App\Exception\BusinessException;
|
||||
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 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 = config('regulatory_platform.api_url');
|
||||
$this->client_id = config('regulatory_platform.client_id');
|
||||
$this->client_secret = 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));
|
||||
}
|
||||
|
||||
$this->redis->set("regulatory_platform_access_token", $data['accessToken'], 60 * 60 * 24 * 6);
|
||||
|
||||
return $data['accessToken'];
|
||||
} catch (GuzzleException $e) {
|
||||
throw new BusinessException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上报 网络咨询(网络门诊)服务
|
||||
* @param array $arg
|
||||
* @return mixed
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function uploadConsult(array $arg){
|
||||
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();
|
||||
}
|
||||
|
||||
$arg['accessToken'] = $access_token;
|
||||
$arg['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['data'];
|
||||
} catch (GuzzleException $e) {
|
||||
throw new BusinessException($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 请求封装
|
||||
* @param string $path
|
||||
* @param array $option
|
||||
* @return array
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
protected function httpRequest(string $path, array $arg = []): array
|
||||
{
|
||||
$option = [
|
||||
"verify" => false
|
||||
];
|
||||
if (!empty($option)) {
|
||||
$arg = array_merge($arg, $option);
|
||||
}
|
||||
|
||||
$response = $this->client->post($path, $arg);
|
||||
|
||||
if ($response->getStatusCode() != '200') {
|
||||
// 请求失败
|
||||
throw new BusinessException($response->getBody()->getContents());
|
||||
}
|
||||
|
||||
$body = json_decode($response->getBody(), true);
|
||||
dump($body);
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user