| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- namespace addons\WechatVideoShop\common\expand\sdk\weixin;
- use addons\WechatVideoShop\common\helper\WechatAccessTokenHelper;
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\ClientException;
- use GuzzleHttp\Exception\GuzzleException;
- use Psr\Http\Message\ResponseInterface;
- use yii\helpers\Json;
- abstract class AbstractRequest
- {
- protected $config = [];
- protected $method = 'get';
- protected $route = '';
- // 默认为表单格式,可选参数 form_params | json | multipart | body | query | cookies | allow_redirects
- protected $params_key = 'json';
- // 请求错误信息
- protected $error = '';
- // 请求参数
- protected $request_option = [];
- /**
- * @var ResponseInterface
- */
- protected $response;
- /**
- * @var ModelInterface
- */
- protected $paramsModel;
- // 响应数据
- protected $responseData = [];
- // 响应错误信息
- protected $respError = '';
- // 原始响应内容
- protected $rawContent = '';
- /**
- * @var array
- */
- protected $headers = [];
- /**
- * 额外的query参数
- * @var array
- */
- protected $query = [];
- /**
- * @param array $config
- */
- public function __construct(array $config)
- {
- $this->config = $config;
- }
- /**
- * @return $this
- */
- public function get()
- {
- $this->method = 'get';
- return $this;
- }
- /**
- * @return $this
- */
- public function post()
- {
- $this->method = 'post';
- return $this;
- }
- /**
- * @param ModelInterface|null $model
- * @return $this
- * @throws GuzzleException
- */
- public function send(ModelInterface $model = null)
- {
- $client = new Client([
- 'base_uri' => 'https://api.weixin.qq.com',
- 'headers' => $this->headers,
- 'timeout' => 30
- ]);
- if (!empty($model)) {
- $this->paramsModel = $model;
- if (in_array($this->params_key, ['multipart'])) {
- foreach ($model->toArray() as $name => $value) {
- $this->request_option[$this->params_key][] = [
- "name" => $name,
- "contents" => file_get_contents($value),
- "filename" => md5($value . time()),
- ];
- }
- } else {
- $this->request_option[$this->params_key] = empty($model->toArray()) && $this->params_key == 'json' ? new \stdClass() : $model->toArray();
- }
- // $this->request_option['debug'] = true;
- }
- if (!empty($this->query)) {
- $this->request_option['query'] = array_merge($this->request_option['query'] ?? [], $this->query);
- }
- try {
- $this->response = $client->request($this->method, $this->route, $this->request_option);
- } catch (ClientException $e) {
- $ret = Json::decode($e->getResponse()->getBody()->getContents());
- $this->error = $ret['msg'] ?? $e->getMessage();
- }
- return $this;
- }
- /**
- * @return $this
- * @throws GuzzleException
- */
- public function accessToken()
- {
- $this->request_option = [
- 'query' => [
- 'access_token' => WechatAccessTokenHelper::getAccessToken($this->config), // 这里需要取获取token
- ]
- ];
- return $this;
- }
- /**
- * @return ResponseInterface
- */
- public function getResponse()
- {
- return $this->response;
- }
- /**
- * @return array
- */
- public function toArray()
- {
- if (empty($this->responseData)) {
- mb_internal_encoding("UTF-8");
- $this->rawContent = $this->response->getBody()->getContents();
- $this->rawContent = mb_convert_encoding($this->rawContent, "UTF-8");
- $this->responseData = Json::decode($this->rawContent);
- }
- return $this->responseData;
- }
- /**
- * @return mixed|string
- */
- public function getRawContent()
- {
- return $this->rawContent;
- }
- /**
- * @return mixed|string
- */
- public function getError()
- {
- return $this->error;
- }
- /**
- * 响应错误
- * @return string
- */
- public function respError()
- {
- if (empty($this->respError)) {
- if (empty($this->paramsModel)) {
- $this->respError = 'params model is empty!';
- } else {
- $this->respError = $this->paramsModel->valid($this->toArray());
- }
- }
- return $this->respError;
- }
- /**
- * @param array $headers
- * @return $this
- */
- public function withHeaders(array $headers)
- {
- $this->headers = $headers;
- return $this;
- }
- /**
- * @param array $params
- * @return $this
- */
- public function withQuery(array $params)
- {
- $this->query = $params;
- return $this;
- }
- }
|