From e329401a0ae15b08405492d46a70edfaaa23c0d7 Mon Sep 17 00:00:00 2001 From: as2252258 Date: Thu, 19 Jul 2018 18:48:36 +0800 Subject: [PATCH] add --- wx/Account.php | 2 +- wx/Base.php | 31 ++++++++++++++++++++++++------- wx/Recharge.php | 2 +- wx/Result.php | 2 +- wx/Template.php | 2 +- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/wx/Account.php b/wx/Account.php index 0cfc2ca..49ada91 100644 --- a/wx/Account.php +++ b/wx/Account.php @@ -15,7 +15,7 @@ class Account extends Base */ public function login($code) { - return http::get('https://api.weixin.qq.com/sns/jscode2session', [ + return Http::get('sns/jscode2session', [ 'appid' => $this->app_id, 'secret' => $this->app_secret, 'js_code' => $code, diff --git a/wx/Base.php b/wx/Base.php index 23c55a2..f4a41bd 100644 --- a/wx/Base.php +++ b/wx/Base.php @@ -105,20 +105,37 @@ abstract class Base */ public $key = ''; + /** @var static */ + protected static $base; + /** - * Base constructor. + * @param $method + * @param $configPath + * @param mixed ...$params + * @return mixed + * @throws \Exception */ - public function __construct($configPath) + public static function call($method, $configPath, ...$params) { - $this->init($configPath); + if (!static::$base instanceof Base) { + static::$base = new static(); + } + $class = static::$base; + $class->loadConfig($configPath); + + if (!method_exists($class, $method)) { + throw new \Exception('未知的执行方法!'); + } + return call_user_func([$class, $method], ...$params); } + /** * */ - public function init($configPath) + public function loadConfig($configPath) { - $config = require $configPath; + $config = require realpath($configPath); if (empty($config)) { return; } @@ -138,7 +155,7 @@ abstract class Base */ public function push($url, $data = [], callable $callback = NULL) { - return http::post($url, $data, $callback); + return Http::post($url, $data, $callback); } /** @@ -229,7 +246,7 @@ abstract class Base */ protected function getAccessToken() { - $data = http::get('https://api.weixin.qq.com/cgi-bin/token', [ + $data = Http::get('https://api.weixin.qq.com/cgi-bin/token', [ 'grant_type' => 'client_credential', 'appid' => $this->app_id, 'secret' => $this->app_secret, diff --git a/wx/Recharge.php b/wx/Recharge.php index 63bf4bb..a5c0ad5 100644 --- a/wx/Recharge.php +++ b/wx/Recharge.php @@ -33,7 +33,7 @@ class Recharge extends Base $this->money = $money; $this->orderNo = $orderNo; $this->data['openid'] = $openId; - return http::post($this->createPayUrl(), $this->builder(), + return Http::post($this->createPayUrl(), $this->builder(), function ($result, $body) use ($_this) { $data = $_this->toArray($result); if(isset($data['sign'])){ diff --git a/wx/Result.php b/wx/Result.php index 5509144..0614cff 100644 --- a/wx/Result.php +++ b/wx/Result.php @@ -77,7 +77,7 @@ class Result * @param $key * @param $data * @return $this - * @throws Exception + * @throws \Exception */ public function setAttr($key, $data) { diff --git a/wx/Template.php b/wx/Template.php index 4fbcf86..1671f34 100644 --- a/wx/Template.php +++ b/wx/Template.php @@ -24,7 +24,7 @@ class Template extends Base { $url = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token=' . $access; $postBody = json_encode($postBody); - return http::post($url, $postBody, NULL, ['content-type' => 'application/json']) + return Http::post($url, $postBody, NULL, ['content-type' => 'application/json']) ->append('postBody', $postBody); } } \ No newline at end of file