Skip to content

Commit

Permalink
Get token from EasyWeChat AccessToken class. (#53)
Browse files Browse the repository at this point in the history
* Get token from EasyWeChat AccessToken class.

* StyleCI.
  • Loading branch information
mingyoung authored and overtrue committed May 16, 2017
1 parent 2040343 commit 234b305
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions src/Providers/WeChatOpenPlatformProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,26 @@ class WeChatOpenPlatformProvider extends WeChatProvider
/**
* Component AppId.
*
* @deprecated 2.0 Will be removed in the future
*
* @var string
*/
protected $componentAppId;

/**
* Component Access Token.
*
* @deprecated 2.0 Will be removed in the future
*
* @var string
*/
protected $componentAccessToken;

/**
* @var \EasyWeChat\OpenPlatform\AccessToken|array
*/
protected $credentials;

/**
* {@inheritdoc}.
*/
Expand All @@ -43,24 +52,27 @@ class WeChatOpenPlatformProvider extends WeChatProvider
* Create a new provider instance.
* (Overriding).
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $clientId
* @param array $componentCredits
* @param string|null $redirectUrl
* @param \Symfony\Component\HttpFoundation\Request $request
* @param string $clientId
* @param \EasyWeChat\OpenPlatform\AccessToken|array $credentials
* @param string|null $redirectUrl
*/
public function __construct(Request $request, $clientId, array $componentCredentials, $redirectUrl = null)
public function __construct(Request $request, $clientId, $credentials, $redirectUrl = null)
{
parent::__construct($request, $clientId, null, $redirectUrl);

list($this->componentAppId, $this->componentAccessToken) = $componentCredentials;
$this->credentials = $credentials;
if (is_array($credentials)) {
list($this->componentAppId, $this->componentAccessToken) = $credentials;
}
}

/**
* {@inheritdoc}.
*/
public function getCodeFields($state = null)
{
$this->with(['component_appid' => $this->componentAppId]);
$this->with(['component_appid' => $this->componentAppId()]);

return parent::getCodeFields($state);
}
Expand All @@ -80,10 +92,30 @@ protected function getTokenFields($code)
{
return [
'appid' => $this->clientId,
'component_appid' => $this->componentAppId,
'component_access_token' => $this->componentAccessToken,
'component_appid' => $this->componentAppId(),
'component_access_token' => $this->componentAccessToken(),
'code' => $code,
'grant_type' => 'authorization_code',
];
}

/**
* Get component app id.
*
* @return string
*/
protected function componentAppId()
{
return $this->componentAppId ?: $this->credentials->getAppId();
}

/**
* Get component access token.
*
* @return string
*/
protected function componentAccessToken()
{
return $this->componentAccessToken ?: $this->credentials->getToken();
}
}

0 comments on commit 234b305

Please sign in to comment.