init($configPath); } /** * */ public function init($configPath) { $config = require $configPath; if (empty($config)) { return; } foreach ($config as $key => $val) { if (!property_exists($this, $key)) { continue; } $this->$key = $val; } } /** * @param $url * @param array $data * @param callable|null $callback * @return Result */ public function push($url, $data = [], callable $callback = NULL) { return http::post($url, $data, $callback); } /** * @param int $length * @return string * * 随机字符串 */ public function random($length = 20) { $res = []; $str = 'abcdefghijklmnopqrstuvwxyz'; $str .= strtoupper($str) . '1234567890'; for ($i = 0; $i < $length; $i++) { $rand = substr($str, rand(0, strlen($str) - 2), 1); if (empty($rand)) { $rand = substr($str, strlen($str) - 3, 1); } array_push($res, $rand); } return $this->nonce_str = implode($res); } /** * @param array $data * @return string */ protected function toXml(array $data) { $xml = ""; foreach ($data as $key => $val) { if (is_numeric($val)) { $xml .= "<" . $key . ">" . $val . ""; } else { $xml .= "<" . $key . ">"; } } $xml .= ""; return $xml; } /** * @return string */ public function sign(array $array) { ksort($array, SORT_STRING); $string = ''; foreach ($array as $key => $val) { if (empty($string)) { $string = $key . '=' . $val; } else { $string .= '&' . $key . '=' . $val; } } $string .= '&key=' . $this->key; // var_dump($string); if ($this->sign_type == 'MD5') { return strtoupper(md5($string)); } else { return hash('sha256', $string); } } /** * @param $xml * @return mixed */ public function toArray($xml) { if (!is_null($json = json_decode($xml, TRUE))) { return $json; } if (is_array($json)) { return $json; } $data = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); return json_decode(json_encode($data), TRUE); } /** * @return bool|mixed|string * @throws \Exception */ protected function getAccessToken() { $data = http::get('https://api.weixin.qq.com/cgi-bin/token', [ 'grant_type' => 'client_credential', 'appid' => $this->app_id, 'secret' => $this->app_secret, ]); if (!$data->isResultsOK()) { throw new \Exception($data->getMessage()); } return $data->getData('access_token'); } /** * @param $data * @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 = $this->toArray($result); if (!isset($data['sign'])) { return $data; } $sign = $data['sign']; unset($data['sign']); $_sign = $this->sign($data); if ($sign != $_sign) { return FALSE; } return $data; } public static $OK = 0; public static $IllegalAesKey = -41001; public static $IllegalIv = -41002; public static $IllegalBuffer = -41003; public static $DecodeBase64Error = -41004; public static function d($code) { $messages = [ static::$OK => '', static::$IllegalAesKey => '', static::$IllegalIv => '', static::$IllegalBuffer => '', static::$DecodeBase64Error => '', ]; return $messages[$code] ?? static::$DecodeBase64Error; } /** * @param $encryptedData * @param $iv * @param $data * @return int */ public static function decode($encryptedData, $iv, $sessionKey, &$data) { if (strlen($sessionKey) != 24) { return self::$IllegalAesKey; } $aesKey = base64_decode($sessionKey); if (strlen($iv) != 24) { return self::$IllegalIv; } $aesIV = base64_decode($iv); $aesCipher = base64_decode($encryptedData); $result = openssl_decrypt($aesCipher, "AES-128-CBC", $aesKey, 1, $aesIV); $dataObj = json_decode($result); if ($dataObj == NULL) { return self::$IllegalBuffer; } if ($dataObj->watermark->appid != static::$app_id) { return self::$IllegalBuffer; } $data = $dataObj; return self::$OK; } }