diff --git a/README.md b/README.md index 6ca5f7b..cf1504d 100644 --- a/README.md +++ b/README.md @@ -70,12 +70,10 @@ composer require jianyan74/yii2-easy-wechat 微信网页授权+获取当前用户信息 ```php -if(Yii::$app->wechat->isWechat && !Yii::$app->wechat->isAuthorized()) -{ +if (Yii::$app->wechat->isWechat && !Yii::$app->wechat->isAuthorized()) { return Yii::$app->wechat->authorizeRequired()->send(); } - // 获取微信当前用户信息方法一 Yii::$app->session->get('wechatUser') @@ -129,18 +127,15 @@ $orderData = [ // 生成支付配置 $payment = Yii::$app->wechat->payment; $result = $payment->order->unify($orderData); -if ($result['return_code'] == 'SUCCESS') -{ +if ($result['return_code'] == 'SUCCESS') { $prepayId = $result['prepay_id']; $config = $payment->jssdk->sdkConfig($prepayId); -} -else -{ +} else { throw new yii\base\ErrorException('微信支付异常, 请稍后再试'); } return $this->render('wxpay', [ - 'jssdk' => $app->jssdk, // $app通过上面的获取实例来获取 + 'jssdk' => $payment->jssdk, // $app通过上面的获取实例来获取 'config' => $config ]); diff --git a/src/Wechat.php b/src/Wechat.php index 9bd7ea2..a81b625 100644 --- a/src/Wechat.php +++ b/src/Wechat.php @@ -1,9 +1,10 @@ request->get('code')) - { + if (Yii::$app->request->get('code')) { // callback and authorize return $this->authorize($this->app->oauth->user()); - } - else - { + } else { // redirect to wechat authorize page $this->setReturnUrl(Yii::$app->request->getUrl()); return Yii::$app->response->redirect($this->app->oauth->redirect()->getTargetUrl()); @@ -142,14 +137,10 @@ public function setReturnUrl($url) public function getReturnUrl($defaultUrl = null) { $url = Yii::$app->session->get($this->returnUrlParam, $defaultUrl); - if (is_array($url)) - { - if (isset($url[0])) - { + if (is_array($url)) { + if (isset($url[0])) { return Yii::$app->getUrlManager()->createUrl($url); - } - else - { + } else { $url = null; } } @@ -164,8 +155,7 @@ public function getReturnUrl($defaultUrl = null) */ public function getApp() { - if (!self::$_app instanceof Factory) - { + if (!self::$_app instanceof \EasyWeChat\OfficialAccount\Application) { self::$_app = Factory::officialAccount(Yii::$app->params['wechatConfig']); } @@ -175,12 +165,11 @@ public function getApp() /** * 获取 EasyWeChat 微信支付实例 * - * @return Factory + * @return Factory|\EasyWeChat\Payment\Application */ public function getPayment() { - if (!self::$_payment instanceof Factory) - { + if (!self::$_payment instanceof \EasyWeChat\Payment\Application) { self::$_payment = Factory::payment(Yii::$app->params['wechatPaymentConfig']); } @@ -190,12 +179,11 @@ public function getPayment() /** * 获取 EasyWeChat 微信小程序实例 * - * @return Factory + * @return Factory|\EasyWeChat\MiniProgram\Application */ public function getMiniProgram() { - if (!self::$_miniProgram instanceof Factory) - { + if (!self::$_miniProgram instanceof \EasyWeChat\MiniProgram\Application) { self::$_miniProgram = Factory::miniProgram(Yii::$app->params['wechatMiniProgramConfig']); } @@ -205,12 +193,11 @@ public function getMiniProgram() /** * 获取 EasyWeChat 微信第三方开放平台实例 * - * @return Factory + * @return Factory|\EasyWeChat\OpenPlatform\Application */ public function getOpenPlatform() { - if (!self::$_openPlatform instanceof Factory) - { + if (!self::$_openPlatform instanceof \EasyWeChat\OpenPlatform\Application) { self::$_openPlatform = Factory::openPlatform(Yii::$app->params['wechatOpenPlatformConfig']); } @@ -220,12 +207,11 @@ public function getOpenPlatform() /** * 获取 EasyWeChat 企业微信实例 * - * @return Factory + * @return Factory|\EasyWeChat\Work\Application */ public function getWork() { - if (!self::$_work instanceof Factory) - { + if (!self::$_work instanceof \EasyWeChat\Work\Application) { self::$_work = Factory::work(Yii::$app->params['wechatWorkConfig']); } @@ -235,12 +221,11 @@ public function getWork() /** * 获取 EasyWeChat 企业微信开放平台实例 * - * @return Factory + * @return Factory|\EasyWeChat\OpenWork\Application */ public function getOpenWork() { - if (!self::$_openWork instanceof Factory) - { + if (!self::$_openWork instanceof \EasyWeChat\OpenWork\Application) { self::$_openWork = Factory::openWork(Yii::$app->params['wechatOpenWorkConfig']); } @@ -254,13 +239,11 @@ public function getOpenWork() */ public function getUser() { - if (!$this->isAuthorized()) - { + if (!$this->isAuthorized()) { return new WechatUser(); } - if (! self::$_user instanceof WechatUser) - { + if (!self::$_user instanceof WechatUser) { $userInfo = Yii::$app->session->get($this->sessionParam); $config = $userInfo ? json_decode($userInfo, true) : []; self::$_user = new WechatUser($config); @@ -276,18 +259,12 @@ public function getUser() */ public function __get($name) { - try - { + try { return parent::__get($name); - } - catch (\Exception $e) - { - if($this->getApp()->$name) - { + } catch (\Exception $e) { + if ($this->getApp()->$name) { return $this->app->$name; - } - else - { + } else { throw $e->getPrevious(); } } diff --git a/src/WechatUser.php b/src/WechatUser.php index 7eebead..8a0f0f4 100644 --- a/src/WechatUser.php +++ b/src/WechatUser.php @@ -1,33 +1,24 @@