init commit

This commit is contained in:
2026-03-17 09:56:00 +08:00
commit e2c8ae752d
6827 changed files with 1211784 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork;
use EasyWeChat\Kernel\ServiceContainer;
use EasyWeChat\OpenWork\Work\Application as Work;
/**
* Application.
*
* @author xiaomin <keacefull@gmail.com>
*
* @property \EasyWeChat\OpenWork\Server\Guard $server
* @property \EasyWeChat\OpenWork\Corp\Client $corp
* @property \EasyWeChat\OpenWork\Provider\Client $provider
* @property \EasyWeChat\OpenWork\SuiteAuth\AccessToken $suite_access_token
* @property \EasyWeChat\OpenWork\Auth\AccessToken $provider_access_token
* @property \EasyWeChat\OpenWork\SuiteAuth\SuiteTicket $suite_ticket
* @property \EasyWeChat\OpenWork\MiniProgram\Auth\Client $mini_program
*/
class Application extends ServiceContainer
{
/**
* @var array
*/
protected $providers = [
Auth\ServiceProvider::class,
SuiteAuth\ServiceProvider::class,
Server\ServiceProvider::class,
Corp\ServiceProvider::class,
Provider\ServiceProvider::class,
MiniProgram\ServiceProvider::class,
];
/**
* @var array
*/
protected $defaultConfig = [
// http://docs.guzzlephp.org/en/stable/request-options.html
'http' => [
'base_uri' => 'https://qyapi.weixin.qq.com/',
],
];
/**
* Creates the miniProgram application.
*/
public function miniProgram(): \EasyWeChat\Work\MiniProgram\Application
{
return new \EasyWeChat\Work\MiniProgram\Application($this->getConfig());
}
/**
* @param string $authCorpId 企业 corp_id
* @param string $permanentCode 企业永久授权码
*/
public function work(string $authCorpId, string $permanentCode): Work
{
return new Work($authCorpId, $permanentCode, $this);
}
/**
* @param string $method
* @param array $arguments
*
* @return mixed
*/
public function __call($method, $arguments)
{
return $this['base']->$method(...$arguments);
}
}

View File

@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\Auth;
use EasyWeChat\Kernel\AccessToken as BaseAccessToken;
/**
* AccessToken.
*
* @author xiaomin <keacefull@gmail.com>
*/
class AccessToken extends BaseAccessToken
{
protected $requestMethod = 'POST';
/**
* @var string
*/
protected $endpointToGetToken = 'cgi-bin/service/get_provider_token';
/**
* @var string
*/
protected $tokenKey = 'provider_access_token';
/**
* @var string
*/
protected $cachePrefix = 'easywechat.kernel.provider_access_token.';
/**
* Credential for get token.
*/
protected function getCredentials(): array
{
return [
'corpid' => $this->app['config']['corp_id'], //服务商的corpid
'provider_secret' => $this->app['config']['secret'],
];
}
}

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\Auth;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
/**
* ServiceProvider.
*
* @author xiaomin <keacefull@gmail.com>
*/
class ServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}.
*/
public function register(Container $app)
{
isset($app['provider_access_token']) || $app['provider_access_token'] = function ($app) {
return new AccessToken($app);
};
}
}

View File

@@ -0,0 +1,197 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\Corp;
use EasyWeChat\Kernel\BaseClient;
use EasyWeChat\Kernel\ServiceContainer;
/**
* Client.
*
* @author xiaomin <keacefull@gmail.com>
*/
class Client extends BaseClient
{
/**
* Client constructor.
* 三方接口有三个access_token这里用的是suite_access_token.
*/
public function __construct(ServiceContainer $app)
{
parent::__construct($app, $app['suite_access_token']);
}
/**
* 企业微信安装应用授权 url.
*
* @param string $preAuthCode 预授权码
* @param string $redirectUri 回调地址
*
* @return string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function getPreAuthorizationUrl(string $preAuthCode = '', string $redirectUri = '', string $state = '')
{
$redirectUri || $redirectUri = $this->app->config['redirect_uri_install'];
$preAuthCode || $preAuthCode = $this->getPreAuthCode()['pre_auth_code'];
$state || $state = rand();
$params = [
'suite_id' => $this->app['config']['suite_id'],
'redirect_uri' => $redirectUri,
'pre_auth_code' => $preAuthCode,
'state' => $state,
];
return 'https://open.work.weixin.qq.com/3rdapp/install?'.http_build_query($params);
}
/**
* 获取预授权码.
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function getPreAuthCode()
{
return $this->httpGet('cgi-bin/service/get_pre_auth_code');
}
/**
* 设置授权配置.
* 该接口可对某次授权进行配置.
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function setSession(string $preAuthCode, array $sessionInfo)
{
$params = [
'pre_auth_code' => $preAuthCode,
'session_info' => $sessionInfo,
];
return $this->httpPostJson('cgi-bin/service/set_session_info', $params);
}
/**
* 获取企业永久授权码.
*
* @param string $authCode 临时授权码会在授权成功时附加在redirect_uri中跳转回第三方服务商网站或通过回调推送给服务商。长度为64至512个字节
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getPermanentByCode(string $authCode)
{
$params = [
'auth_code' => $authCode,
];
return $this->httpPostJson('cgi-bin/service/get_permanent_code', $params);
}
/**
* 获取企业授权信息.
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getAuthorization(string $authCorpId, string $permanentCode)
{
$params = [
'auth_corpid' => $authCorpId,
'permanent_code' => $permanentCode,
];
return $this->httpPostJson('cgi-bin/service/get_auth_info', $params);
}
/**
* 获取应用的管理员列表.
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getManagers(string $authCorpId, string $agentId)
{
$params = [
'auth_corpid' => $authCorpId,
'agentid' => $agentId,
];
return $this->httpPostJson('cgi-bin/service/get_admin_list', $params);
}
/**
* 获取登录url.
*
* @return string
*/
public function getOAuthRedirectUrl(string $redirectUri = '', string $scope = 'snsapi_userinfo', string $state = null)
{
$redirectUri || $redirectUri = $this->app->config['redirect_uri_oauth'];
$state || $state = rand();
$params = [
'appid' => $this->app['config']['suite_id'],
'redirect_uri' => $redirectUri,
'response_type' => 'code',
'scope' => $scope,
'state' => $state,
];
return 'https://open.weixin.qq.com/connect/oauth2/authorize?'.http_build_query($params).'#wechat_redirect';
}
/**
* 第三方根据code获取企业成员信息.
*
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function getUserByCode(string $code)
{
$params = [
'code' => $code,
];
return $this->httpGet('cgi-bin/service/getuserinfo3rd', $params);
}
/**
* 第三方使用user_ticket获取成员详情.
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getUserByTicket(string $userTicket)
{
$params = [
'user_ticket' => $userTicket,
];
return $this->httpPostJson('cgi-bin/service/getuserdetail3rd', $params);
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\Corp;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
/**
* ServiceProvider.
*
* @author xiaomin <keacefull@gmail.com>
*/
class ServiceProvider implements ServiceProviderInterface
{
/**
* Registers services on the given container.
*
* This method should only be used to configure services and parameters.
* It should not get services.
*/
public function register(Container $app)
{
isset($app['corp']) || $app['corp'] = function ($app) {
return new Client($app);
};
}
}

View File

@@ -0,0 +1,46 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\MiniProgram;
use EasyWeChat\Kernel\BaseClient;
use EasyWeChat\Kernel\ServiceContainer;
/**
* Class Client.
*/
class Client extends BaseClient
{
/**
* Client constructor.
*/
public function __construct(ServiceContainer $app)
{
parent::__construct($app, $app['suite_access_token']);
}
/**
* Get session info by code.
*
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function session(string $code)
{
$params = [
'js_code' => $code,
'grant_type' => 'authorization_code',
];
return $this->httpGet('cgi-bin/service/miniprogram/jscode2session', $params);
}
}

View File

@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\MiniProgram;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
/**
* ServiceProvider.
*/
class ServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}.
*/
public function register(Container $app)
{
!isset($app['mini_program']) && $app['mini_program'] = function ($app) {
return new Client($app);
};
}
}

View File

@@ -0,0 +1,214 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\Provider;
use EasyWeChat\Kernel\BaseClient;
use EasyWeChat\Kernel\ServiceContainer;
/**
* Client.
*
* @author xiaomin <keacefull@gmail.com>
*/
class Client extends BaseClient
{
/**
* Client constructor.
*/
public function __construct(ServiceContainer $app)
{
parent::__construct($app, $app['provider_access_token']);
}
/**
* 单点登录 - 获取登录的地址.
*
* @return string
*/
public function getLoginUrl(string $redirectUri = '', string $userType = 'admin', string $state = '')
{
$redirectUri || $redirectUri = $this->app->config['redirect_uri_single'];
$state || $state = rand();
$params = [
'appid' => $this->app['config']['corp_id'],
'redirect_uri' => $redirectUri,
'usertype' => $userType,
'state' => $state,
];
return 'https://open.work.weixin.qq.com/wwopen/sso/3rd_qrConnect?'.http_build_query($params);
}
/**
* 单点登录 - 获取登录用户信息.
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getLoginInfo(string $authCode)
{
$params = [
'auth_code' => $authCode,
];
return $this->httpPostJson('cgi-bin/service/get_login_info', $params);
}
/**
* 获取注册定制化URL.
*
* @return string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
*/
public function getRegisterUri(string $registerCode = '')
{
if (!$registerCode) {
/** @var array $response */
$response = $this->detectAndCastResponseToType($this->getRegisterCode(), 'array');
$registerCode = $response['register_code'];
}
$params = ['register_code' => $registerCode];
return 'https://open.work.weixin.qq.com/3rdservice/wework/register?'.http_build_query($params);
}
/**
* 获取注册码.
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getRegisterCode(
string $corpName = '',
string $adminName = '',
string $adminMobile = '',
string $state = ''
) {
$params = [];
$params['template_id'] = $this->app['config']['reg_template_id'];
!empty($corpName) && $params['corp_name'] = $corpName;
!empty($adminName) && $params['admin_name'] = $adminName;
!empty($adminMobile) && $params['admin_mobile'] = $adminMobile;
!empty($state) && $params['state'] = $state;
return $this->httpPostJson('cgi-bin/service/get_register_code', $params);
}
/**
* 查询注册状态.
*
* Desc:该API用于查询企业注册状态企业注册成功返回注册信息.
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getRegisterInfo(string $registerCode)
{
$params = [
'register_code' => $registerCode,
];
return $this->httpPostJson('cgi-bin/service/get_register_info', $params);
}
/**
* 设置授权应用可见范围.
*
* Desc:调用该接口前提是开启通讯录迁移,收到授权成功通知后可调用。
* 企业注册初始化安装应用后,应用默认可见范围为根部门。
* 如需修改应用可见范围,服务商可以调用该接口设置授权应用的可见范围。
* 该接口只能使用注册完成回调事件或者查询注册状态返回的access_token。
* 调用设置通讯录同步完成后或者access_token超过30分钟失效即解除通讯录锁定状态则不能继续调用该接口。
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function setAgentScope(
string $accessToken,
string $agentId,
array $allowUser = [],
array $allowParty = [],
array $allowTag = []
) {
$params = [
'agentid' => $agentId,
'allow_user' => $allowUser,
'allow_party' => $allowParty,
'allow_tag' => $allowTag,
'access_token' => $accessToken,
];
return $this->httpGet('cgi-bin/agent/set_scope', $params);
}
/**
* 设置通讯录同步完成.
*
* Desc:该API用于设置通讯录同步完成解除通讯录锁定状态同时使通讯录迁移access_token失效。
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function contactSyncSuccess(string $accessToken)
{
$params = ['access_token' => $accessToken];
return $this->httpGet('cgi-bin/sync/contact_sync_success', $params);
}
/**
* 通讯录单个搜索
*
* @param string $queryWord
* @param $agentId
* @param int $offset
* @param int $limit
* @param int $queryType
* @param null $fullMatchField
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function searchContact(
string $queryWord,
$agentId,
int $offset = 0,
int $limit = 50,
int $queryType = 0,
$fullMatchField = null
) {
$params = [];
$params['auth_corpid'] = $this->app['config']['corp_id'];
$params['query_word'] = $queryWord;
$params['query_type'] = $queryType;
$params['agentid'] = $agentId;
$params['offset'] = $offset;
$params['limit'] = $limit;
!empty($fullMatchField) && $params['full_match_field'] = $fullMatchField;
return $this->httpPostJson('cgi-bin/service/contact/search', $params);
}
}

View File

@@ -0,0 +1,33 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\Provider;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
/**
* ServiceProvider.
*
* @author xiaomin <keacefull@gmail.com>
*/
class ServiceProvider implements ServiceProviderInterface
{
protected $app;
public function register(Container $app)
{
$this->app = $app;
isset($app['provider']) || $app['provider'] = function ($app) {
return new Client($app);
};
}
}

View File

@@ -0,0 +1,63 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\Server;
use EasyWeChat\Kernel\Encryptor;
use EasyWeChat\Kernel\ServerGuard;
/**
* Guard.
*
* @author xiaomin <keacefull@gmail.com>
*/
class Guard extends ServerGuard
{
/**
* @var bool
*/
protected $alwaysValidate = true;
/**
* @return $this
*/
public function validate()
{
return $this;
}
protected function shouldReturnRawResponse(): bool
{
return !is_null($this->app['request']->get('echostr'));
}
protected function isSafeMode(): bool
{
return true;
}
/**
* @return mixed
*
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
*/
protected function decryptMessage(array $message)
{
$encryptor = new Encryptor($message['ToUserName'], $this->app['config']->get('token'), $this->app['config']->get('aes_key'));
return $message = $encryptor->decrypt(
$message['Encrypt'],
$this->app['request']->get('msg_signature'),
$this->app['request']->get('nonce'),
$this->app['request']->get('timestamp')
);
}
}

View File

@@ -0,0 +1,60 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\Server\Handlers;
use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
use EasyWeChat\Kernel\Decorators\FinallyResult;
use EasyWeChat\Kernel\ServiceContainer;
/**
* EchoStrHandler.
*
* @author xiaomin <keacefull@gmail.com>
*/
class EchoStrHandler implements EventHandlerInterface
{
/**
* @var ServiceContainer
*/
protected $app;
/**
* EchoStrHandler constructor.
*/
public function __construct(ServiceContainer $app)
{
$this->app = $app;
}
/**
* @param mixed $payload
*
* @return FinallyResult|null
*/
public function handle($payload = null)
{
if ($decrypted = $this->app['request']->get('echostr')) {
$str = $this->app['encryptor_corp']->decrypt(
$decrypted,
$this->app['request']->get('msg_signature'),
$this->app['request']->get('nonce'),
$this->app['request']->get('timestamp')
);
return new FinallyResult($str);
}
//把SuiteTicket缓存起来
if (!empty($payload['SuiteTicket']) && !empty($payload['SuiteId']) && $this->app['config']['suite_id'] == $payload['SuiteId']) {
$this->app['suite_ticket']->setTicket($payload['SuiteTicket']);
}
}
}

View File

@@ -0,0 +1,56 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\Server;
use EasyWeChat\Kernel\Encryptor;
use EasyWeChat\OpenWork\Server\Handlers\EchoStrHandler;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
/**
* ServiceProvider.
*
* @author xiaomin <keacefull@gmail.com>
*/
class ServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}.
*/
public function register(Container $app)
{
//微信第三方在校验url是使用的是GET方式请求和corp_id进行加密
!isset($app['encryptor_corp']) && $app['encryptor_corp'] = function ($app) {
return new Encryptor(
$app['config']['corp_id'],
$app['config']['token'],
$app['config']['aes_key']
);
};
//微信第三方推送数据时使用的是suite_id进行加密
!isset($app['encryptor']) && $app['encryptor'] = function ($app) {
return new Encryptor(
$app['config']['suite_id'],
$app['config']['token'],
$app['config']['aes_key']
);
};
!isset($app['server']) && $app['server'] = function ($app) {
$guard = new Guard($app);
$guard->push(new EchoStrHandler($app));
return $guard;
};
}
}

View File

@@ -0,0 +1,54 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\SuiteAuth;
use EasyWeChat\Kernel\AccessToken as BaseAccessToken;
/**
* AccessToken.
*
* @author xiaomin <keacefull@gmail.com>
*/
class AccessToken extends BaseAccessToken
{
/**
* @var string
*/
protected $requestMethod = 'POST';
/**
* @var string
*/
protected $endpointToGetToken = 'cgi-bin/service/get_suite_token';
/**
* @var string
*/
protected $tokenKey = 'suite_access_token';
/**
* @var string
*/
protected $cachePrefix = 'easywechat.kernel.suite_access_token.';
/**
* Credential for get token.
*/
protected function getCredentials(): array
{
return [
'suite_id' => $this->app['config']['suite_id'],
'suite_secret' => $this->app['config']['suite_secret'],
'suite_ticket' => $this->app['suite_ticket']->getTicket(),
];
}
}

View File

@@ -0,0 +1,37 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\SuiteAuth;
use Pimple\Container;
use Pimple\ServiceProviderInterface;
/**
* ServiceProvider.
*
* @author xiaomin <keacefull@gmail.com>
*/
class ServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}.
*/
public function register(Container $app)
{
$app['suite_ticket'] = function ($app) {
return new SuiteTicket($app);
};
isset($app['suite_access_token']) || $app['suite_access_token'] = function ($app) {
return new AccessToken($app);
};
}
}

View File

@@ -0,0 +1,76 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\SuiteAuth;
use EasyWeChat\Kernel\Exceptions\RuntimeException;
use EasyWeChat\Kernel\Traits\InteractsWithCache;
use EasyWeChat\OpenWork\Application;
/**
* SuiteTicket.
*
* @author xiaomin <keacefull@gmail.com>
*/
class SuiteTicket
{
use InteractsWithCache;
/**
* @var Application
*/
protected $app;
/**
* SuiteTicket constructor.
*/
public function __construct(Application $app)
{
$this->app = $app;
}
/**
* @return $this
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function setTicket(string $ticket)
{
$this->getCache()->set($this->getCacheKey(), $ticket, 1800);
if (!$this->getCache()->has($this->getCacheKey())) {
throw new RuntimeException('Failed to cache suite ticket.');
}
return $this;
}
/**
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function getTicket(): string
{
if ($cached = $this->getCache()->get($this->getCacheKey())) {
return $cached;
}
throw new RuntimeException('Credential "suite_ticket" does not exist in cache.');
}
protected function getCacheKey(): string
{
return 'easywechat.open_work.suite_ticket.'.$this->app['config']['suite_id'];
}
}

View File

@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\Work;
use EasyWeChat\OpenWork\Application as OpenWork;
use EasyWeChat\OpenWork\Work\Auth\AccessToken;
use EasyWeChat\Work\Application as Work;
/**
* Application.
*
* @author xiaomin <keacefull@gmail.com>
*/
class Application extends Work
{
/**
* Application constructor.
*/
public function __construct(string $authCorpId, string $permanentCode, OpenWork $component, array $prepends = [])
{
parent::__construct($component->getConfig(), $prepends + [
'access_token' => function ($app) use ($authCorpId, $permanentCode, $component) {
return new AccessToken($app, $authCorpId, $permanentCode, $component);
},
]);
}
}

View File

@@ -0,0 +1,70 @@
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\OpenWork\Work\Auth;
use EasyWeChat\Kernel\AccessToken as BaseAccessToken;
use EasyWeChat\OpenWork\Application;
use Pimple\Container;
/**
* AccessToken.
*
* @author xiaomin <keacefull@gmail.com>
*/
class AccessToken extends BaseAccessToken
{
/**
* @var string
*/
protected $requestMethod = 'POST';
/**
* @var string 授权方企业ID
*/
protected $authCorpid;
/**
* @var string 授权方企业永久授权码通过get_permanent_code获取
*/
protected $permanentCode;
protected $component;
/**
* AccessToken constructor.
*/
public function __construct(Container $app, string $authCorpId, string $permanentCode, Application $component)
{
$this->authCorpid = $authCorpId;
$this->permanentCode = $permanentCode;
$this->component = $component;
parent::__construct($app);
}
/**
* Credential for get token.
*/
protected function getCredentials(): array
{
return [
'auth_corpid' => $this->authCorpid,
'permanent_code' => $this->permanentCode,
];
}
public function getEndpoint(): string
{
return 'cgi-bin/service/get_corp_token?'.http_build_query([
'suite_access_token' => $this->component['suite_access_token']->getToken()['suite_access_token'],
]);
}
}