api_url = "http://49.233.3.200:6304/api/thridapi/"; $this->client_id = "ZD-004"; $this->client_secret = "0baa5927164710b9f800bf33546b6da3"; $this->container = ApplicationContext::getContainer(); $this->client = $this->container->get(Client::class); } /** * 获取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['result'])){ // 返回值为空 throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR)); } if (empty($response['result']['token'])){ // 返回值为空 throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR)); } } catch (GuzzleException $e) { throw new BusinessException($e->getMessage()); } $this->header = [ "Authorization" => "Bearer " . $response['result']['token'] ]; $this->redis->set("prescription_token",$response['result']['token'],60 * 60 * 1.5); return $response['result']['token']; } // 获取药品 public function getProd(int $page = 1,int $pageSize = 10){ $option = [ "json" => array( "page" => $page, "pageSize" => $pageSize, ), ]; try { $response = $this->httpRequest($this->api_url . $this->version . '/drug/syncDrugCatalogue', $option); if (empty($response['result'])){ // 返回值为空 throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR)); } return $response['result']; } catch (GuzzleException $e) { throw new BusinessException($e->getMessage()); } } /** * 获取商品库存 * @param string $product_platform_code 处方平台商品编码(此处使用第三方药店商品编码!) * @return array */ public function getProdStock(string $product_platform_code): array { $option = [ "json" => array( "pharmacyCode" => "JG-10009", "drugCode" => $product_platform_code, ), ]; try { $response = $this->httpRequest($this->api_url . $this->version . '/pharmacy/pharmacyInventory', $option); if (empty($response)){ // 返回值为空 throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR)); } 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 { if ($path != "http://49.233.3.200:6304/api/thridapi/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); } $response = $this->client->post($path, $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)); } if (empty($body['data'])){ // 返回值为空 if (!empty($body['message'])){ throw new BusinessException($body['message']); } throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR)); } return $body['data']; } }