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', ]; } }