Skip to content

Commit

Permalink
优化初始化
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyan74 committed Jul 2, 2019
1 parent f1110db commit bc3f690
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 76 deletions.
13 changes: 4 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down Expand Up @@ -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
]);

Expand Down
71 changes: 24 additions & 47 deletions src/Wechat.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?php

namespace jianyan\easywechat;

use Yii;
use EasyWeChat\Factory;
use yii\base\Component;
use EasyWeChat\Factory;

/**
* Class Wechat
Expand All @@ -16,9 +17,6 @@
* @property \EasyWeChat\OpenPlatform\Application $openPlatform 微信开放平台(第三方平台)实例
* @property \EasyWeChat\Work\Application $work 企业微信实例
* @property \EasyWeChat\OpenWork\Application $openWork 企业微信开放平台实例
*
* @property bool $isWechat 检查客户端是否是微信浏览器
* @property WechatUser $user 获取微信身份信息
*/
class Wechat extends Component
{
Expand Down Expand Up @@ -93,13 +91,10 @@ class Wechat extends Component
*/
public function authorizeRequired()
{
if(Yii::$app->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());
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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']);
}

Expand All @@ -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']);
}

Expand All @@ -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']);
}

Expand All @@ -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']);
}

Expand All @@ -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']);
}

Expand All @@ -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']);
}

Expand All @@ -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);
Expand All @@ -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();
}
}
Expand Down
22 changes: 2 additions & 20 deletions src/WechatUser.php
Original file line number Diff line number Diff line change
@@ -1,33 +1,24 @@
<?php

namespace jianyan\easywechat;

use yii\base\Component;

/**
* 微信授权用户的个人信息
*
* Class WechatUser
* @package jianyan\easywechat
* @property string $openId 微信授权用户的唯一标识(openid)
* @see https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842 返回的JSON数据包的格式参考第四步
* @package jianyan74\easywechat
*/
class WechatUser extends Component
{
/**
* 用户的唯一标识(openid)
*
* @var string
*/
public $id;
/**
* 用户昵称
*
* @var string
*/
public $nickname;
/**
* 用户昵称
*
* @var string
*/
public $name;
Expand All @@ -36,20 +27,14 @@ class WechatUser extends Component
*/
public $email;
/**
* 用户头像, 最后一个数值代表正方形头像大小(有0,46,64,96,132数值可选, 0代表640*640正方形头像)
*
* @var string
*/
public $avatar;
/**
* 原数据, 微信授权返回的JSON数据包
*
* @var array
*/
public $original;
/**
* 微信授权获取用户信息token
*
* @var \Overtrue\Socialite\AccessToken
*/
public $token;
Expand All @@ -58,10 +43,7 @@ class WechatUser extends Component
*/
public $provider;


/**
* 微信授权用户的唯一标识(openid)
*
* @return string
*/
public function getOpenId()
Expand Down

0 comments on commit bc3f690

Please sign in to comment.