From 2fbdfa83245f2a66e49975a5c19b7a29566161ac Mon Sep 17 00:00:00 2001 From: jianyan74 Date: Wed, 31 Jul 2019 15:16:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=90=AF=E5=8A=A8=E6=97=B6?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E6=9B=BF=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 3 +++ src/Wechat.php | 29 ++++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cf1504d..a3d0aa2 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,9 @@ composer require jianyan74/yii2-easy-wechat 'userOptions' => [], // 用户身份类参数 'sessionParam' => 'wechatUser', // 微信用户信息将存储在会话在这个密钥 'returnUrlParam' => '_wechatReturnUrl', // returnUrl 存储在会话中 + 'rebinds' => [ // 自定义服务模块 + // 'cache' => 'common\components\Cache', + ] ], // ... ] diff --git a/src/Wechat.php b/src/Wechat.php index a81b625..62f6813 100644 --- a/src/Wechat.php +++ b/src/Wechat.php @@ -38,6 +38,11 @@ class Wechat extends Component */ public $returnUrlParam = '_wechatReturnUrl'; + /** + * @var array + */ + public $rebinds = []; + /** * 微信SDK * @@ -157,6 +162,7 @@ public function getApp() { if (!self::$_app instanceof \EasyWeChat\OfficialAccount\Application) { self::$_app = Factory::officialAccount(Yii::$app->params['wechatConfig']); + !empty($this->rebinds) && self::$_app = $this->rebind(self::$_app); } return self::$_app; @@ -171,6 +177,7 @@ public function getPayment() { if (!self::$_payment instanceof \EasyWeChat\Payment\Application) { self::$_payment = Factory::payment(Yii::$app->params['wechatPaymentConfig']); + !empty($this->rebinds) && self::$_payment = $this->rebind(self::$_payment); } return self::$_payment; @@ -185,6 +192,7 @@ public function getMiniProgram() { if (!self::$_miniProgram instanceof \EasyWeChat\MiniProgram\Application) { self::$_miniProgram = Factory::miniProgram(Yii::$app->params['wechatMiniProgramConfig']); + !empty($this->rebinds) && self::$_miniProgram = $this->rebind(self::$_miniProgram); } return self::$_miniProgram; @@ -199,6 +207,7 @@ public function getOpenPlatform() { if (!self::$_openPlatform instanceof \EasyWeChat\OpenPlatform\Application) { self::$_openPlatform = Factory::openPlatform(Yii::$app->params['wechatOpenPlatformConfig']); + !empty($this->rebinds) && self::$_openPlatform = $this->rebind(self::$_openPlatform); } return self::$_openPlatform; @@ -213,6 +222,7 @@ public function getWork() { if (!self::$_work instanceof \EasyWeChat\Work\Application) { self::$_work = Factory::work(Yii::$app->params['wechatWorkConfig']); + !empty($this->rebinds) && self::$_work = $this->rebind(self::$_work); } return self::$_work; @@ -227,11 +237,24 @@ public function getOpenWork() { if (!self::$_openWork instanceof \EasyWeChat\OpenWork\Application) { self::$_openWork = Factory::openWork(Yii::$app->params['wechatOpenWorkConfig']); + !empty($this->rebinds) && self::$_openWork = $this->rebind(self::$_openWork); } return self::$_openWork; } + /** + * @param $app + */ + public function rebind($app) + { + foreach ($this->rebinds as $key => $class) { + $app->rebind($key, new $class()); + } + + return $app; + } + /** * 获取微信身份信息 * @@ -262,11 +285,7 @@ public function __get($name) try { return parent::__get($name); } catch (\Exception $e) { - if ($this->getApp()->$name) { - return $this->app->$name; - } else { - throw $e->getPrevious(); - } + throw $e->getPrevious(); } }