AbstractRequest.php 5.0 KB

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