AbstractRequest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace addons\QiShangTong\common\expand\sdk\qishangtong;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Exception\ClientException;
  5. use GuzzleHttp\Exception\GuzzleException;
  6. use Psr\Http\Message\ResponseInterface;
  7. use yii\helpers\Json;
  8. abstract class AbstractRequest
  9. {
  10. protected $config = [];
  11. protected $method = 'post';
  12. protected $route = '';
  13. // 默认为表单格式,可选参数 form_params | json | multipart | body | query | cookies | allow_redirects
  14. protected $params_key = 'form_params';
  15. // 请求错误信息
  16. protected $error = '';
  17. // 请求参数
  18. protected $request_option = [];
  19. /**
  20. * @var ResponseInterface
  21. */
  22. protected $response;
  23. /**
  24. * @var ModelInterface
  25. */
  26. protected $paramsModel;
  27. // 响应数据
  28. protected $responseData = [];
  29. // 响应错误信息
  30. protected $respError = '';
  31. // 原始响应内容
  32. protected $rawContent = '';
  33. /**
  34. * @var array
  35. */
  36. protected $headers = [];
  37. /**
  38. * 额外的query参数
  39. * @var array
  40. */
  41. protected $query = [];
  42. /**
  43. * @param array $config
  44. */
  45. public function __construct(array $config = [])
  46. {
  47. $this->config = $config;
  48. }
  49. /**
  50. * @return $this
  51. */
  52. public function get()
  53. {
  54. $this->method = 'get';
  55. return $this;
  56. }
  57. /**
  58. * @return $this
  59. */
  60. public function post()
  61. {
  62. $this->method = 'post';
  63. return $this;
  64. }
  65. protected function getHost()
  66. {
  67. // if (true) return 'http://test-open.dongguajie.com';
  68. return 'http://openapi.dongguajie.com';
  69. }
  70. /**
  71. * @param ModelInterface|null $model
  72. * @return $this
  73. * @throws GuzzleException
  74. */
  75. public function send(ModelInterface $model = null)
  76. {
  77. $client = new Client([
  78. 'base_uri' => $this->getHost(),
  79. 'headers' => $this->headers,
  80. 'timeout' => 30
  81. ]);
  82. if (!empty($model)) {
  83. $this->paramsModel = $model;
  84. if (in_array($this->params_key, ['multipart'])) {
  85. foreach ($model->toArray() as $name => $value) {
  86. $this->request_option[$this->params_key][] = [
  87. "name" => $name,
  88. "contents" => file_get_contents($value),
  89. "filename" => md5($value . time()),
  90. ];
  91. }
  92. } else {
  93. $this->request_option[$this->params_key] = empty($model->toArray()) && $this->params_key == 'json' ? new \stdClass() : $model->toArray();
  94. }
  95. // $this->request_option['debug'] = true;
  96. }
  97. if (!empty($this->query)) {
  98. $this->request_option['query'] = array_merge($this->request_option['query'] ?? [], $this->query);
  99. }
  100. try {
  101. $this->response = $client->request($this->method, $this->route, $this->request_option);
  102. } catch (ClientException $e) {
  103. $ret = Json::decode($e->getResponse()->getBody()->getContents());
  104. $this->error = $ret['msg'] ?? $e->getMessage();
  105. }
  106. return $this;
  107. }
  108. /**
  109. * @return ResponseInterface
  110. */
  111. public function getResponse()
  112. {
  113. return $this->response;
  114. }
  115. /**
  116. * @return array
  117. */
  118. public function toArray()
  119. {
  120. if (empty($this->responseData)) {
  121. mb_internal_encoding("UTF-8");
  122. $this->rawContent = $this->response->getBody()->getContents();
  123. $this->rawContent = mb_convert_encoding($this->rawContent, "UTF-8");
  124. $this->responseData = Json::decode($this->rawContent);
  125. }
  126. return $this->responseData;
  127. }
  128. /**
  129. * @return mixed|string
  130. */
  131. public function getRawContent()
  132. {
  133. return $this->rawContent;
  134. }
  135. /**
  136. * @return mixed|string
  137. */
  138. public function getError()
  139. {
  140. return $this->error;
  141. }
  142. /**
  143. * 响应错误
  144. * @return string
  145. */
  146. public function respError()
  147. {
  148. if (empty($this->respError)) {
  149. if (empty($this->paramsModel)) {
  150. $this->respError = 'params model is empty!';
  151. } else {
  152. $this->respError = $this->paramsModel->valid($this->toArray());
  153. }
  154. }
  155. return $this->respError;
  156. }
  157. /**
  158. * @param array $headers
  159. * @return $this
  160. */
  161. public function withHeaders(array $headers)
  162. {
  163. $this->headers = $headers;
  164. return $this;
  165. }
  166. /**
  167. * @param array $params
  168. * @return $this
  169. */
  170. public function withQuery(array $params)
  171. {
  172. $this->query = $params;
  173. return $this;
  174. }
  175. /**
  176. * @return string
  177. */
  178. public function getRoute(): string
  179. {
  180. return $this->route;
  181. }
  182. }