AbstractRequest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace addons\WechatVideoShop\common\expand\sdk\weixin;
  3. use addons\WechatVideoShop\common\helper\WechatAccessTokenHelper;
  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 = 'get';
  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. /**
  67. * @param ModelInterface|null $model
  68. * @return $this
  69. * @throws GuzzleException
  70. */
  71. public function send(ModelInterface $model = null)
  72. {
  73. $client = new Client([
  74. 'base_uri' => 'https://api.weixin.qq.com',
  75. 'headers' => $this->headers,
  76. 'timeout' => 30
  77. ]);
  78. if (!empty($model)) {
  79. $this->paramsModel = $model;
  80. if (in_array($this->params_key, ['multipart'])) {
  81. foreach ($model->toArray() as $name => $value) {
  82. $this->request_option[$this->params_key][] = [
  83. "name" => $name,
  84. "contents" => file_get_contents($value),
  85. "filename" => md5($value . time()),
  86. ];
  87. }
  88. } else {
  89. $this->request_option[$this->params_key] = empty($model->toArray()) && $this->params_key == 'json' ? new \stdClass() : $model->toArray();
  90. }
  91. // $this->request_option['debug'] = true;
  92. }
  93. if (!empty($this->query)) {
  94. $this->request_option['query'] = array_merge($this->request_option['query'] ?? [], $this->query);
  95. }
  96. try {
  97. $this->response = $client->request($this->method, $this->route, $this->request_option);
  98. } catch (ClientException $e) {
  99. $ret = Json::decode($e->getResponse()->getBody()->getContents());
  100. $this->error = $ret['msg'] ?? $e->getMessage();
  101. }
  102. return $this;
  103. }
  104. /**
  105. * @return $this
  106. * @throws GuzzleException
  107. */
  108. public function accessToken()
  109. {
  110. $this->request_option = [
  111. 'query' => [
  112. 'access_token' => WechatAccessTokenHelper::getAccessToken($this->config), // 这里需要取获取token
  113. ]
  114. ];
  115. return $this;
  116. }
  117. /**
  118. * @return ResponseInterface
  119. */
  120. public function getResponse()
  121. {
  122. return $this->response;
  123. }
  124. /**
  125. * @return array
  126. */
  127. public function toArray()
  128. {
  129. if (empty($this->responseData)) {
  130. mb_internal_encoding("UTF-8");
  131. $this->rawContent = $this->response->getBody()->getContents();
  132. $this->rawContent = mb_convert_encoding($this->rawContent, "UTF-8");
  133. $this->responseData = Json::decode($this->rawContent);
  134. }
  135. return $this->responseData;
  136. }
  137. /**
  138. * @return mixed|string
  139. */
  140. public function getRawContent()
  141. {
  142. return $this->rawContent;
  143. }
  144. /**
  145. * @return mixed|string
  146. */
  147. public function getError()
  148. {
  149. return $this->error;
  150. }
  151. /**
  152. * 响应错误
  153. * @return string
  154. */
  155. public function respError()
  156. {
  157. if (empty($this->respError)) {
  158. if (empty($this->paramsModel)) {
  159. $this->respError = 'params model is empty!';
  160. } else {
  161. $this->respError = $this->paramsModel->valid($this->toArray());
  162. }
  163. }
  164. return $this->respError;
  165. }
  166. /**
  167. * @param array $headers
  168. * @return $this
  169. */
  170. public function withHeaders(array $headers)
  171. {
  172. $this->headers = $headers;
  173. return $this;
  174. }
  175. /**
  176. * @param array $params
  177. * @return $this
  178. */
  179. public function withQuery(array $params)
  180. {
  181. $this->query = $params;
  182. return $this;
  183. }
  184. }