Skip to content

Commit

Permalink
第三方企业授权应用功能完成
Browse files Browse the repository at this point in the history
  • Loading branch information
never615 committed May 22, 2017
1 parent 6ebb659 commit 820b407
Show file tree
Hide file tree
Showing 19 changed files with 586 additions and 276 deletions.
18 changes: 16 additions & 2 deletions src/CorpServer/Api/AbstractCorpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ abstract class AbstractCorpServer extends AbstractAPI
*/
protected $request;


/**
* AbstractOpenPlatform constructor.
* AbstractCorpServer constructor.
*
* @param \EasyWeChat\CorpServer\Core\AccessToken $accessToken
* @param \Symfony\Component\HttpFoundation\Request $request
Expand All @@ -52,13 +53,26 @@ public function __construct(AccessToken $accessToken, Request $request)
$this->request = $request;
}


/**
* Get OpenPlatform AppId.
* Get CropServer SuiteId.
*
* @return string
*/
public function getClientId()
{
return $this->getAccessToken()->getClientId();
}

/**
* Get CropSErver SuiteSecret
*
* @return string
*/
public function getClientSecret()
{
return $this->getAccessToken()->getClientSecret();
}


}
103 changes: 27 additions & 76 deletions src/CorpServer/Api/BaseApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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]);
}
}
79 changes: 79 additions & 0 deletions src/CorpServer/Api/PreAuthorization.php
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))
);
}
}
58 changes: 41 additions & 17 deletions src/CorpServer/Core/AccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}

}
Loading

0 comments on commit 820b407

Please sign in to comment.