| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- <?php
- namespace addons\WangDianTong\common\expand\sdk\wangdian;
- use addons\WangDianTong\common\enums\WangDianTongEnum;
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\ClientException;
- use Psr\Http\Message\ResponseInterface;
- use yii\helpers\Json;
- abstract class AbstractRequest
- {
- /**
- * @var bool 是否为旗舰版
- */
- protected $is_flagship = false;
- /**
- * @var string
- */
- protected $route = '';
- /**
- * @var array
- */
- protected $config = [];
- protected $method = 'post';
- /**
- * @var string
- */
- protected $error = '';
- /**
- * @var ResponseInterface
- */
- protected $response;
- /**
- * @var array
- */
- protected $data = [];
- /**
- * @var Client
- */
- protected $client;
- /**
- * @var array
- */
- protected $headers = [];
- public function __construct(array $config)
- {
- $this->config = $config;
- $url = $this->is_flagship ? WangDianTongEnum::FS_BASE_URLS[(int)$this->config['env']] : WangDianTongEnum::BASE_URLS[(int)$this->config['env']];
- $this->client = new Client([
- 'base_uri' => $url,
- 'headers' => $this->headers,
- 'timeout' => WangDianTongEnum::TIMEOUT
- ]);
- }
- /**
- * @param ModelInterface|null $model
- * @return $this
- */
- public function send(ModelInterface $model = null)
- {
- return $this->is_flagship ? $this->flagshipSend($model) : $this->enterpriseSend($model);
- }
- protected function enterpriseSend(ModelInterface $model = null)
- {
- try {
- $this->data = !empty($model) ? array_merge($this->data, $model->toArray()) : $this->data;
- // 生成签名
- $this->data['sign'] = $this->makeSign();
- try {
- $params = $this->method == 'get' ?
- ['query' => $this->data] :
- ['form_params' => $this->data];
- $this->response = $this->client->request($this->method, $this->route, $params);
- } catch (ClientException $e) {
- $ret = Json::decode($e->getResponse()->getBody()->getContents());
- $this->error = $ret['msg'] ?? $e->getMessage();
- }
- } catch (\Throwable $e) {
- $this->error = $e->getMessage();
- }
- return $this;
- }
- protected function flagshipSend(ModelInterface $model = null)
- {
- try {
- $body = [];
- if (!empty($model)) {
- $body = $model->toArray();
- // 公共参数
- $global_params = ['page_no', 'page_size', 'calc_total'];
- foreach ($global_params as $param) {
- if (isset($body[$param])) {
- $this->data[$param] = $body[$param];
- unset($body[$param]);
- }
- }
- $body = $model->sort();
- }
- $this->data['body'] = json_encode($body, JSON_UNESCAPED_UNICODE);
- $this->data = array_merge($this->data, $this->flagshipGlobalParams());
- // 生成签名
- $this->data['sign'] = $this->flagshipMakeSign();
- try {
- unset($this->data['body']);
- $this->response = $this->client->post("/openapi?" . http_build_query($this->data), [
- 'timeout' => 30,
- 'body' => json_encode($body, JSON_UNESCAPED_UNICODE),
- 'headers' => [
- 'Content-Type: application/json;charset=UTF-8'
- ],
- // 'debug' => true
- ]);
- } catch (ClientException $e) {
- // echo $e->getResponse()->getBody()->getContents();
- $ret = Json::decode($e->getResponse()->getBody()->getContents());
- $this->error = $ret['msg'] ?? $e->getMessage();
- }
- } catch (\Throwable $e) {
- $this->error = $e->getMessage();
- }
- return $this;
- }
- /**
- * @return mixed|null
- * @throws \Exception
- */
- public function toArray()
- {
- try {
- $res = $this->response->getBody()->getContents();
- return Json::decode($res);
- } catch (\Exception $e) {
- throw new \Exception($e->getMessage());
- }
- }
- /**
- * @return ResponseInterface
- */
- public function getResponse()
- {
- return $this->response;
- }
- /**
- * @return array
- */
- public function getData(): array
- {
- return $this->data;
- }
- protected function flagshipMakeSign()
- {
- date_default_timezone_set("Asia/Shanghai");
- $this->data['sid'] = $this->config['app_sid'];
- $this->data['key'] = $this->config['app_key'];
- $this->data['timestamp'] = time() - 1325347200;
- ksort($this->data);
- $arr = [];
- $tmp = explode(':', $this->config['app_secret_key']);
- $arr[] = $tmp[0] ?? '';
- foreach($this->data as $key => $val)
- {
- if($key == 'sign') continue;
- $arr[] = $key;
- $arr[] = $val;
- }
- $arr[] = $tmp[0] ?? '';
- return md5(implode('', $arr));
- }
- /**
- * @return string
- */
- protected function makeSign(): string
- {
- try {
- $this->data['sid'] = $this->config['app_sid'];
- $this->data['appkey'] = $this->config['app_key'];
- $this->data['timestamp'] = time();
- ksort($this->data);
- $arr = array();
- foreach($this->data as $key => $val)
- {
- if($key == 'sign') continue;
- if(count($arr))
- $arr[] = ';';
- $arr[] = sprintf("%02d", iconv_strlen($key, 'UTF-8'));
- $arr[] = '-';
- $arr[] = $key;
- $arr[] = ':';
- $arr[] = sprintf("%04d", iconv_strlen($val, 'UTF-8'));
- $arr[] = '-';
- $arr[] = $val;
- }
- return md5(implode('', $arr) . $this->config['app_secret_key']);
- } catch (\Exception $e) {
- }
- return '';
- }
- /**
- * @return $this
- */
- public function post()
- {
- $this->method = 'post';
- return $this;
- }
- /**
- * @return $this
- */
- public function get()
- {
- $this->method = 'get';
- return $this;
- }
- /**
- * @return string
- */
- public function getError()
- {
- return $this->error;
- }
- public function __set($name, $value)
- {
- $this->data[$name] = $value;
- }
- public function __get($name)
- {
- return $this->data[$name] ?? null;
- }
- /**
- * 旗舰版公共参数
- * @return array
- */
- protected function flagshipGlobalParams()
- {
- $tmp = explode(':', $this->config['app_secret_key']);
- return [
- 'salt' => $tmp[1] ?? '',
- 'method' => $this->route,
- 'v' => '1.0',
- ];
- }
- }
|