$code, 'message' => $message]); } /** * @param $code */ public function setErrorCode($code) { $this->errorCode = $code; } /** * @param $message */ public function setErrorMessage($message) { $this->errorMsg = $message; } /** * @return int */ public function getErrorCode() { return $this->errorCode; } /** * @return string */ public function getErrorMessage() { return $this->errorMsg; } /** * Miniprogarampage constructor. */ public function __construct() { } /** * @param Config $config * @return mixed */ public static function getInstance(Config $config) { if (static::$instance === null) { static::$instance = new static(); } static::$instance->config = $config; static::$instance->request->setAgent($config->getAgent()); static::$instance->request->setUseSwoole($config->isUsrSwoole()); $request = static::$instance->request; if ($request->getIsSSL()) { $request->addHeader('ssl_cert_file', $config->getSslCert()); $request->addHeader('ssl_key_file', $config->getSslKey()); } return static::$instance; } /** * @param Config $config * @return $this */ public function initConfig($config) { $this->config = $config; $this->request = HttpClient::NewRequest(); $this->request->setIsSSL(true); $this->request->setData(null); $this->request->setUseSwoole($config->isUsrSwoole()); $this->request->setAgent($config->getAgent()); if (!empty($config->getSslCert())) { $this->request->addHeader('ssl_cert_file', $config->getSslCert()); } if (!empty($config->getSslKey())) { $this->request->addHeader('ssl_key_file', $config->getSslKey()); } return $this; } /** * @return \wchat\common\Config */ public function getConfig() { return $this->config; } /** * @return bool|mixed|string * @throws \Exception */ protected function getAccessToken() { $access = $this->config->getAccessToken(); if (!empty($access)) { return $access; } $this->request->setMethod(HttpClient::GET); $data = $this->request->get('/cgi-bin/token', [ 'grant_type' => 'client_credential', 'appid' => $this->config->getAppid(), 'secret' => $this->config->getAppsecret(), ]); if (!$data->isResultsOK()) { throw new \Exception($data->getMessage()); } $access = $data->getData('access_token'); $this->config->setAccessToken($access); return $access; } /** * @param $data * @param $body * @return mixed * @throws \Exception */ protected function buildResult($data, $body = NULL) { $data = $this->checkSign($data); if (!$data) { $return['code'] = -1; $return['message'] = '签名错误.'; } else { if (isset($data['return_code'])) { if ($data['return_code'] == 'FAIL') { $return['code'] = -1; $return['message'] = $data['return_msg']; } else { $return['code'] = 0; $return['data'] = $data; $return['data']['postBody'] = $body; } } else { if ($data['errcode'] == 'FAIL') { $return['code'] = -1; $return['message'] = $data['errmsg']; } else { $return['code'] = 0; $return['data'] = $data; $return['data']['postBody'] = $body; } } } return $return; } /** * @param $result * @return mixed * @throws \Exception */ protected function checkSign($result) { $data = Help::toArray($result); if (!isset($data['sign'])) { return $data; } $sign = $data['sign']; unset($data['sign']); $key = $this->config->getKey(); $sign_type = $this->config->getSignType(); $_sign = Help::sign($data, $key, $sign_type); if ($sign != $_sign) { return FALSE; } return $data; } }