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 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(); } $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; } 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(); } $arg['accessToken'] = $access_token; $arg['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 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); } Log::getInstance()->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); 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; } }