Skip to content

Commit

Permalink
更新,简化代码
Browse files Browse the repository at this point in the history
  • Loading branch information
xutl committed Jan 31, 2018
1 parent ce426ff commit 9d5f281
Showing 1 changed file with 4 additions and 112 deletions.
116 changes: 4 additions & 112 deletions src/oauth/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace xutl\wechat\oauth;

use Yii;
use yii\authclient\OAuth2;
use xutl\authclient\WeChat;
use yii\authclient\OAuthToken;

/**
Expand All @@ -18,7 +18,7 @@
* @author Tongle Xu <xutongle@gmail.com>
* @since 1.0
*/
class OAuth extends OAuth2
class OAuth extends WeChat
{
/**
* @inheritdoc
Expand Down Expand Up @@ -48,25 +48,8 @@ class OAuth extends OAuth2
/**
* @inheritdoc
*/
protected function defaultName()
{
return 'wechat';
}

/**
* @inheritdoc
*/
protected function defaultTitle()
{
return Yii::t('app', 'Wechat');
}

/**
* 初始化组件
*/
public function init()
{
parent::init();
if (empty ($this->clientId)) {
$this->clientId = Yii::$app->wechat->appId;
}
Expand All @@ -76,6 +59,7 @@ public function init()
if ($this->scope === null) {
$this->scope = 'snsapi_userinfo';
}
parent::init();
}

/**
Expand All @@ -98,8 +82,8 @@ public function buildAuthUrl(array $params = [])
{
$defaultParams = [
'appid' => $this->clientId,
'redirect_uri' => $this->getReturnUrl(),
'response_type' => 'code',
'redirect_uri' => $this->getReturnUrl(),
];
if (!empty($this->scope)) {
$defaultParams['scope'] = $this->scope;
Expand All @@ -113,96 +97,4 @@ public function buildAuthUrl(array $params = [])
$authUrl = $this->composeUrl($this->authUrl, array_merge($defaultParams, $params));
return $authUrl . '#wechat_redirect';
}

/**
* Composes URL from base URL and GET params.
* @param string $url base URL.
* @param array $params GET params.
* @return string composed URL.
*/
protected function composeUrl($url, array $params = [])
{
if (!empty($params)) {
if (strpos($url, '?') === false) {
$url .= '?';
} else {
$url .= '&';
}
$url .= http_build_query($params, '', '&', PHP_QUERY_RFC3986);
}
return $url;
}

/**
* @param \yii\httpclient\Request $request HTTP request instance.
* @param OAuthToken $accessToken access token instance.
*/
public function applyAccessTokenToRequest($request, $accessToken)
{
$data = $request->getData();
$data['access_token'] = $accessToken->getToken();
$data['openid'] = $accessToken->getParam('openid');
$request->setData($data);
}

/**
* Applies client credentials (e.g. [[clientId]] and [[clientSecret]]) to the HTTP request instance.
* This method should be invoked before sending any HTTP request, which requires client credentials.
* @param \yii\httpclient\Request $request HTTP request instance.
* @since 2.1.3
*/
protected function applyClientCredentialsToRequest($request)
{
$request->addData([
'appid' => $this->clientId,
'secret' => $this->clientSecret,
]);
}

/**
* 返回OpenId
* @return mixed
*/
public function getOpenId()
{
return $this->getAccessToken()->getParam('openid');
}

/**
* @inheritdoc
*/
protected function initUserAttributes()
{
return $this->api('sns/userinfo', 'GET', [
//'lang'=>
]);
}

/**
* Gets new auth token to replace expired one.
* @param OAuthToken $token expired auth token.
* @return OAuthToken new auth token.
* @throws \yii\authclient\InvalidResponseException
*/
public function refreshAccessToken(OAuthToken $token)
{
$params = [
'grant_type' => 'refresh_token'
];
$params = array_merge($token->getParams(), $params);

$request = $this->createRequest()
->setMethod('POST')
->setUrl($this->refreshTokenUrl)
->setData($params);

$this->applyClientCredentialsToRequest($request);

$response = $this->sendRequest($request);

$token = $this->createToken(['params' => $response]);
$this->setAccessToken($token);

return $token;
}
}

0 comments on commit 9d5f281

Please sign in to comment.