Skip to content

Commit

Permalink
[work] 修改素材管理接口 (#2126)
Browse files Browse the repository at this point in the history
  • Loading branch information
moniang committed Jun 22, 2021
1 parent 899bf0b commit e3868a6
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 11 deletions.
74 changes: 63 additions & 11 deletions src/Work/Media/Client.php
Expand Up @@ -33,17 +33,7 @@ class Client extends BaseClient
*/
public function get(string $mediaId)
{
$response = $this->requestRaw('cgi-bin/media/get', 'GET', [
'query' => [
'media_id' => $mediaId,
],
]);

if (false !== stripos($response->getHeaderLine('Content-Type'), 'text/plain')) {
return $this->castResponseToType($response, $this->app['config']->get('response_type'));
}

return StreamResponse::buildFromPsrResponse($response);
return $this->getResources($mediaId, 'cgi-bin/media/get');
}

/**
Expand Down Expand Up @@ -118,4 +108,66 @@ public function upload(string $type, string $path, array $form = [])

return $this->httpUpload('cgi-bin/media/upload', $files, $form, compact('type'));
}

/**
* Upload permanently valid images
*
* @see https://work.weixin.qq.com/api/doc/90000/90135/90256
* @param string $path
* @param array $form
*
* @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function uploadImg(string $path, array $form = [])
{
$files = [
'media' => $path,
];

return $this->httpUpload('cgi-bin/media/uploadimg', $files, $form);
}


/**
* Get HD voice material
*
* @see https://work.weixin.qq.com/api/doc/90000/90135/90255
* @param string $mediaId
*
* @return array|\EasyWeChat\Kernel\Http\Response|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function getHdVoice(string $mediaId)
{
return $this->getResources($mediaId, 'cgi-bin/media/get/jssdk');
}

/**
* @param string $mediaId
* @param string $uri
*
* @return array|\EasyWeChat\Kernel\Http\Response|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
protected function getResources(string $mediaId, string $uri)
{
$response = $this->requestRaw($uri, 'GET', [
'query' => [
'media_id' => $mediaId,
],
]);

if (false !== stripos($response->getHeaderLine('Content-Type'), 'text/plain')) {
return $this->castResponseToType($response, $this->app['config']->get('response_type'));
}

return StreamResponse::buildFromPsrResponse($response);
}
}
46 changes: 46 additions & 0 deletions tests/Work/Media/ClientTest.php
Expand Up @@ -133,6 +133,26 @@ public function testUploadFile()
$this->assertSame('mock-result', $client->uploadFile('/foo/bar/file.txt', ['filename' => 'file.jpg']));
}

public function testUploadImg()
{
//无参
$client = $this->mockApiClient(Client::class);
$files = [
'media' => '/foo/bar/image.jpg',
];
$form = [];

$client->expects()->httpUpload('cgi-bin/media/uploadimg', $files, $form)->andReturn('mock-result');

$this->assertSame('mock-result', $client->uploadImg('/foo/bar/image.jpg'));

//有参
$client = $this->mockApiClient(Client::class, ['uploadImg']);
$client->expects()->uploadImg('/foo/bar/image.jpg', ['filename' => 'image.jpg'])->andReturn('mock-result');

$this->assertSame('mock-result', $client->uploadImg('/foo/bar/image.jpg', ['filename' => 'image.jpg']));
}

public function testUpload()
{
$client = $this->mockApiClient(Client::class);
Expand All @@ -149,4 +169,30 @@ public function testUpload()

$this->assertSame('mock-result', $client->upload('voice', '/foo/bar/voice.mp3', ['filename' => 'voice.mp3']));
}

public function testGetHdVoice()
{
$app = new ServiceContainer();
$client = $this->mockApiClient(Client::class, [], $app);

$mediaId = 'invalid-media-id';
$imageResponse = new \GuzzleHttp\Psr7\Response(200, ['content-type' => 'text/plain'], '{"error": "invalid media id hits."}');
$client->expects()->requestRaw('cgi-bin/media/get/jssdk', 'GET', [
'query' => [
'media_id' => $mediaId,
],
])->andReturn($imageResponse);

$this->assertSame(['error' => 'invalid media id hits.'], $client->getHdVoice($mediaId));

$mediaId = 'valid-media-id';
$imageResponse = new Response(200, [], 'valid data');
$client->expects()->requestRaw('cgi-bin/media/get/jssdk', 'GET', [
'query' => [
'media_id' => $mediaId,
],
])->andReturn($imageResponse);

$this->assertInstanceOf(StreamResponse::class, $client->getHdVoice($mediaId));
}
}

0 comments on commit e3868a6

Please sign in to comment.