forked from w7corp/easywechat
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
586 additions
and
276 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,38 +20,28 @@ | |
* @author never615 <[email protected]> | ||
* @copyright 2017 | ||
* | ||
* @see https://github.com/overtrue | ||
* @see http://overtrue.me | ||
* @see https://github.com/overtrue | ||
* @see http://overtrue.me | ||
*/ | ||
|
||
namespace EasyWeChat\CorpServer\Api; | ||
|
||
class BaseApi extends AbstractCorpServer | ||
{ | ||
/** | ||
* Get auth info api. | ||
* Get info (auth_corp_info,auth_info,auth_user_info ) api. | ||
*/ | ||
const GET_AUTH_INFO = 'https://api.weixin.qq.com/cgi-bin/component/api_query_auth'; | ||
const GET_INFO = 'https://qyapi.weixin.qq.com/cgi-bin/service/get_permanent_code'; | ||
|
||
/** | ||
* Get authorizer token api. | ||
*/ | ||
const GET_AUTHORIZER_TOKEN = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token'; | ||
const GET_AUTHORIZER_TOKEN = 'https://qyapi.weixin.qq.com/cgi-bin/service/get_corp_token'; | ||
|
||
/** | ||
* Get authorizer info api. | ||
*/ | ||
const GET_AUTHORIZER_INFO = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_info'; | ||
|
||
/** | ||
* Get authorizer options api. | ||
*/ | ||
const GET_AUTHORIZER_OPTION = 'https://api.weixin.qq.com/cgi-bin/component/api_get_authorizer_option'; | ||
|
||
/** | ||
* Set authorizer options api. | ||
*/ | ||
const SET_AUTHORIZER_OPTION = 'https://api.weixin.qq.com/cgi-bin/component/api_set_authorizer_option'; | ||
const GET_AUTH_INFO = 'https://qyapi.weixin.qq.com/cgi-bin/service/get_auth_info'; | ||
|
||
/** | ||
* Get authorization info. | ||
|
@@ -63,90 +53,51 @@ class BaseApi extends AbstractCorpServer | |
public function getAuthorizationInfo($authCode = null) | ||
{ | ||
$params = [ | ||
'component_appid' => $this->getClientId(), | ||
'authorization_code' => $authCode ?: $this->request->get('auth_code'), | ||
'suite_id' => $this->getClientId(), | ||
"auth_code" => $authCode, | ||
]; | ||
|
||
return $this->parseJSON('json', [self::GET_AUTH_INFO, $params]); | ||
} | ||
|
||
/** | ||
* Get authorizer token. | ||
* | ||
* It doesn't cache the authorizer-access-token. | ||
* So developers should NEVER call this method. | ||
* It'll called by: AuthorizerAccessToken::renewAccessToken() | ||
* | ||
* @param $appId | ||
* @param $refreshToken | ||
* | ||
* @return \EasyWeChat\Support\Collection | ||
*/ | ||
public function getAuthorizerToken($appId, $refreshToken) | ||
{ | ||
$params = [ | ||
'component_appid' => $this->getClientId(), | ||
'authorizer_appid' => $appId, | ||
'authorizer_refresh_token' => $refreshToken, | ||
]; | ||
|
||
return $this->parseJSON('json', [self::GET_AUTHORIZER_TOKEN, $params]); | ||
} | ||
|
||
/** | ||
* Get authorizer info. | ||
* | ||
* @param string $authorizerAppId | ||
* Get authorization info. | ||
* | ||
* @param $corpId | ||
* @param $permanentCode | ||
* @return \EasyWeChat\Support\Collection | ||
*/ | ||
public function getAuthorizerInfo($authorizerAppId) | ||
public function getAuthorizerInfo($corpId, $permanentCode) | ||
{ | ||
$params = [ | ||
'component_appid' => $this->getClientId(), | ||
'authorizer_appid' => $authorizerAppId, | ||
"suite_id" => $this->getClientId(), | ||
"auth_corpid" => $corpId, | ||
"permanent_code" => $permanentCode, | ||
]; | ||
|
||
return $this->parseJSON('json', [self::GET_AUTHORIZER_INFO, $params]); | ||
return $this->parseJSON('json', [self::GET_AUTH_INFO, $params]); | ||
} | ||
|
||
/** | ||
* Get options. | ||
* Get authorizer token. | ||
* | ||
* @param $authorizerAppId | ||
* @param $optionName | ||
* It doesn't cache the authorizer-access-token. | ||
* So developers should NEVER call this method. | ||
* It'll called by: AuthorizerAccessToken::renewAccessToken() | ||
* | ||
* @param $authCorpId | ||
* @param $permanentCode | ||
* @return \EasyWeChat\Support\Collection | ||
*/ | ||
public function getAuthorizerOption($authorizerAppId, $optionName) | ||
{ | ||
$params = [ | ||
'component_appid' => $this->getClientId(), | ||
'authorizer_appid' => $authorizerAppId, | ||
'option_name' => $optionName, | ||
]; | ||
|
||
return $this->parseJSON('json', [self::GET_AUTHORIZER_OPTION, $params]); | ||
} | ||
|
||
/** | ||
* Set authorizer option. | ||
* | ||
* @param $authorizerAppId | ||
* @param $optionName | ||
* @param $optionValue | ||
* | ||
* @return \EasyWeChat\Support\Collection | ||
*/ | ||
public function setAuthorizerOption($authorizerAppId, $optionName, $optionValue) | ||
public function getAuthorizerToken($authCorpId, $permanentCode) | ||
{ | ||
$params = [ | ||
'component_appid' => $this->getClientId(), | ||
'authorizer_appid' => $authorizerAppId, | ||
'option_name' => $optionName, | ||
'option_value' => $optionValue, | ||
"suite_id" => $this->getClientId(), | ||
"auth_corpid" => $authCorpId, | ||
"permanent_code" => $permanentCode, | ||
]; | ||
|
||
return $this->parseJSON('json', [self::SET_AUTHORIZER_OPTION, $params]); | ||
return $this->parseJSON('json', [self::GET_AUTHORIZER_TOKEN, $params]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the overtrue/wechat. | ||
* | ||
* (c) overtrue <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
/** | ||
* PreAuthorization.php. | ||
* | ||
* Part of Overtrue\WeChat. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author never615 <[email protected]> | ||
* @copyright 2017 | ||
* | ||
* @see https://github.com/overtrue | ||
* @see http://overtrue.me | ||
*/ | ||
|
||
namespace EasyWeChat\CorpServer\Api; | ||
|
||
use EasyWeChat\Exceptions\InvalidArgumentException; | ||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class PreAuthorization extends AbstractCorpServer | ||
{ | ||
/** | ||
* Create pre auth code url. | ||
*/ | ||
const CREATE_PRE_AUTH_CODE = 'https://qyapi.weixin.qq.com/cgi-bin/service/get_pre_auth_code'; | ||
|
||
/** | ||
* Pre auth link. | ||
*/ | ||
const PRE_AUTH_LINK = 'https://qy.weixin.qq.com/cgi-bin/loginpage?suite_id=%s&pre_auth_code=%s&redirect_uri=%s'; | ||
|
||
/** | ||
* Get pre auth code. | ||
* | ||
* @return string | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function getCode() | ||
{ | ||
$data = [ | ||
'suite_id' => $this->getClientId(), | ||
]; | ||
|
||
$result = $this->parseJSON('json', [self::CREATE_PRE_AUTH_CODE, $data]); | ||
|
||
if (empty($result['pre_auth_code'])) { | ||
throw new InvalidArgumentException('Invalid response.'); | ||
} | ||
|
||
return $result['pre_auth_code']; | ||
} | ||
|
||
/** | ||
* Redirect to WeChat PreAuthorization page. | ||
* | ||
* @param string $url | ||
* | ||
* @return \Symfony\Component\HttpFoundation\RedirectResponse | ||
*/ | ||
public function redirect($url) | ||
{ | ||
return new RedirectResponse( | ||
sprintf(self::PRE_AUTH_LINK, $this->getClientId(), $this->getCode(), urlencode($url)) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,53 +20,54 @@ | |
* @author never615 <[email protected]> | ||
* @copyright 2017 | ||
* | ||
* @see https://github.com/overtrue | ||
* @see http://overtrue.me | ||
* @see https://github.com/overtrue | ||
* @see http://overtrue.me | ||
*/ | ||
|
||
namespace EasyWeChat\CorpServer\Core; | ||
|
||
use EasyWeChat\Exceptions\HttpException; | ||
use EasyWeChat\Foundation\Core\AccessToken as BaseAccessToken; | ||
|
||
class AccessToken extends BaseAccessToken | ||
{ | ||
/** | ||
* VerifyTicket. | ||
* SuiteTicket. | ||
* | ||
* @var \EasyWeChat\CorpServer\Core\SuiteTicket | ||
* @var \EasyWeChat\CorpServer\Core\Ticket | ||
*/ | ||
protected $ticket; | ||
protected $suiteTicket; | ||
|
||
/** | ||
* API. | ||
*/ | ||
const API_TOKEN_GET = 'https://api.weixin.qq.com/cgi-bin/component/api_component_token'; | ||
const API_TOKEN_GET = 'https://qyapi.weixin.qq.com/cgi-bin/service/get_suite_token'; | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected $queryName = 'component_access_token'; | ||
protected $queryName = 'suite_access_token'; | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected $tokenJsonKey = 'component_access_token'; | ||
protected $tokenJsonKey = 'suite_access_token'; | ||
|
||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
protected $prefix = 'easywechat.open_platform.component_access_token.'; | ||
protected $prefix = 'easywechat.corp_server.suite_access_token.'; | ||
|
||
/** | ||
* Set VerifyTicket. | ||
* | ||
* @param \EasyWeChat\CorpServer\Core\SuiteTicket $ticket | ||
* Set SuiteTicket. | ||
* | ||
* @param \EasyWeChat\CorpServer\Core\Ticket $ticket | ||
* @return $this | ||
* | ||
*/ | ||
public function setTicket(Ticket $ticket) | ||
public function setSuiteTicket(Ticket $ticket) | ||
{ | ||
$this->ticket = $ticket; | ||
$this->suiteTicket = $ticket; | ||
|
||
return $this; | ||
} | ||
|
@@ -77,9 +78,32 @@ public function setTicket(Ticket $ticket) | |
public function requestFields(): array | ||
{ | ||
return [ | ||
'component_appid' => $this->getClientId(), | ||
'component_appsecret' => $this->getClientSecret(), | ||
'component_verify_ticket' => $this->ticket->getTicket(), | ||
'suite_id' => $this->getClientId(), | ||
'suite_secret' => $this->getClientSecret(), | ||
'suite_ticket' => $this->suiteTicket->getTicket(), | ||
]; | ||
} | ||
|
||
|
||
/** | ||
* Get the access token from WeChat server. | ||
* | ||
* @throws \EasyWeChat\Exceptions\HttpException | ||
* | ||
* @return array | ||
*/ | ||
public function getTokenFromServer() | ||
{ | ||
$http = $this->getHttp(); | ||
|
||
$result = $http->parseJSON($http->json(static::API_TOKEN_GET, $this->requestFields())); | ||
|
||
if (empty($result[$this->tokenJsonKey])) { | ||
throw new HttpException('Request AccessToken fail. response: '.json_encode($result, | ||
JSON_UNESCAPED_UNICODE)); | ||
} | ||
|
||
return $result; | ||
} | ||
|
||
} |
Oops, something went wrong.