loadConfig($configPath); return $class; } /** * */ public function loadConfig($config) { if (empty($config)) { return; } if (is_string($config)) { $config = require_once $config; } 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 * @throws */ public function push($url, $data = [], callable $callback = NULL) { return WxClient::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); } /** * @return bool|mixed|string * @throws \Exception */ protected function getAccessToken() { $data = WxClient::get('https://api.weixin.qq.com/cgi-bin/token', [ 'grant_type' => 'client_credential', 'appid' => $this->appid, 'secret' => $this->appsecret, ]); 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 = Help::toArray($result); if (!isset($data['sign'])) { return $data; } $sign = $data['sign']; unset($data['sign']); $_sign = Help::sign($data, $this->key, $this->sign_type); if ($sign != $_sign) { return FALSE; } return $data; } }