Skip to content

Commit

Permalink
添加企业微信获取应用的 jsapi_ticket 接口 (#1694)
Browse files Browse the repository at this point in the history
  • Loading branch information
her-cat authored and overtrue committed Oct 8, 2019
1 parent 511f0fe commit 0c65cac
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Work/Application.php
Expand Up @@ -30,7 +30,7 @@
* @property \EasyWeChat\Work\User\Client $user
* @property \EasyWeChat\Work\User\TagClient $tag
* @property \EasyWeChat\Work\Server\ServiceProvider $server
* @property \EasyWeChat\BasicService\Jssdk\Client $jssdk
* @property \EasyWeChat\Work\Jssdk\Client $jssdk
* @property \Overtrue\Socialite\Providers\WeWorkProvider $oauth
* @property \EasyWeChat\Work\Invoice\Client $invoice
* @property \EasyWeChat\Work\Chat\Client $chat
Expand Down
36 changes: 36 additions & 0 deletions src/Work/Jssdk/Client.php
Expand Up @@ -12,6 +12,7 @@
namespace EasyWeChat\Work\Jssdk;

use EasyWeChat\BasicService\Jssdk\Client as BaseClient;
use EasyWeChat\Kernel\Exceptions\RuntimeException;

/**
* Class Client.
Expand All @@ -29,4 +30,39 @@ protected function getAppId()
{
return $this->app['config']->get('corp_id');
}

/**
* @param bool $refresh
* @param string $type
*
* @return array|\EasyWeChat\Kernel\Support\Collection|mixed|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws RuntimeException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
public function getAgentTicket(bool $refresh = false, string $type = 'agent_config')
{
$cacheKey = sprintf('easywechat.work.jssdk.ticket.%s.%s', $type, $this->getAppId());

if (!$refresh && $this->getCache()->has($cacheKey)) {
return $this->getCache()->get($cacheKey);
}

/** @var array<string, mixed> $result */
$result = $this->castResponseToType(
$this->requestRaw('cgi-bin/ticket/get', 'GET', ['query' => ['type' => $type]]),
'array'
);

$this->getCache()->set($cacheKey, $result, $result['expires_in'] - 500);

if (!$this->getCache()->has($cacheKey)) {
throw new RuntimeException('Failed to cache jssdk ticket.');
}

return $result;
}
}
48 changes: 48 additions & 0 deletions tests/Work/Jssdk/ClientTest.php
Expand Up @@ -11,6 +11,7 @@

namespace EasyWeChat\Tests\Work\Jssdk;

use EasyWeChat\Kernel\Http\Response;
use EasyWeChat\Kernel\ServiceContainer;
use EasyWeChat\Tests\TestCase;
use EasyWeChat\Work\Application;
Expand Down Expand Up @@ -62,4 +63,51 @@ public function testGetTicket()

$this->assertSame($ticket, $client->getTicket(true));
}

public function testGetAgentTicket()
{
$app = new ServiceContainer([
'corp_id' => 'mock-corp-id',
]);

$client = $this->mockApiClient(Client::class, ['getCache'], $app);
$cache = \Mockery::mock(CacheInterface::class);

$ticket = [
'ticket' => 'mock-ticket',
'expires_in' => 7200,
];
$cacheKey = 'easywechat.work.jssdk.ticket.agent_config.mock-corp-id';

// no refresh and cached
$cache->expects()->has($cacheKey)->andReturn(true);
$cache->expects()->get($cacheKey)->andReturn($ticket);
$client->allows()->getCache()->andReturn($cache);

$this->assertSame($ticket, $client->getAgentTicket());

$response = new Response(200, [], json_encode($ticket));

// no refresh and no cached
$cache->expects()->has($cacheKey)->twice()->andReturn(false, true);
$cache->expects()->get($cacheKey)->never();
$cache->expects()->set($cacheKey, $ticket, $ticket['expires_in'] - 500);

$client->expects()
->requestRaw('cgi-bin/ticket/get', 'GET', ['query' => ['type' => 'agent_config']])
->andReturn($response);

$this->assertSame($ticket, $client->getAgentTicket());

// with refresh and cached
$cache->expects()->has($cacheKey)->andReturn(true);
$cache->expects()->get($cacheKey)->never();
$cache->expects()->set($cacheKey, $ticket, $ticket['expires_in'] - 500);

$client->expects()
->requestRaw('cgi-bin/ticket/get', 'GET', ['query' => ['type' => 'agent_config']])
->andReturn($response);

$this->assertSame($ticket, $client->getAgentTicket(true));
}
}

0 comments on commit 0c65cac

Please sign in to comment.