初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,192 @@
|
||||
<?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 TuzhiEasyWeChat\BasicService\Media;
|
||||
|
||||
use TuzhiEasyWeChat\Kernel\BaseClient;
|
||||
use TuzhiEasyWeChat\Kernel\Exceptions\InvalidArgumentException;
|
||||
use TuzhiEasyWeChat\Kernel\Http\StreamResponse;
|
||||
|
||||
/**
|
||||
* Class Client.
|
||||
*
|
||||
* @author overtrue <i@overtrue.me>
|
||||
*/
|
||||
class Client extends BaseClient
|
||||
{
|
||||
/**
|
||||
* Allow media type.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $allowTypes = ['image', 'voice', 'video', 'thumb'];
|
||||
|
||||
/**
|
||||
* Upload image.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
|
||||
*
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function uploadImage($path)
|
||||
{
|
||||
return $this->upload('image', $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload video.
|
||||
*
|
||||
* @param string $path
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
|
||||
*
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function uploadVideo($path)
|
||||
{
|
||||
return $this->upload('video', $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
|
||||
*
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function uploadVoice($path)
|
||||
{
|
||||
return $this->upload('voice', $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
|
||||
*
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function uploadThumb($path)
|
||||
{
|
||||
return $this->upload('thumb', $path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Upload temporary material.
|
||||
*
|
||||
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
|
||||
*
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function upload(string $type, string $path)
|
||||
{
|
||||
if (!file_exists($path) || !is_readable($path)) {
|
||||
throw new InvalidArgumentException(sprintf("File does not exist, or the file is unreadable: '%s'", $path));
|
||||
}
|
||||
|
||||
if (!in_array($type, $this->allowTypes, true)) {
|
||||
throw new InvalidArgumentException(sprintf("Unsupported media type: '%s'", $type));
|
||||
}
|
||||
|
||||
return $this->httpUpload('cgi-bin/media/upload', ['media' => $path], ['type' => $type]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|\TuzhiEasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
||||
*
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidArgumentException
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function uploadVideoForBroadcasting(string $path, string $title, string $description)
|
||||
{
|
||||
$response = $this->uploadVideo($path);
|
||||
/** @var array $arrayResponse */
|
||||
$arrayResponse = $this->detectAndCastResponseToType($response, 'array');
|
||||
|
||||
if (!empty($arrayResponse['media_id'])) {
|
||||
return $this->createVideoForBroadcasting($arrayResponse['media_id'], $title, $description);
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
|
||||
*
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function createVideoForBroadcasting(string $mediaId, string $title, string $description)
|
||||
{
|
||||
return $this->httpPostJson('cgi-bin/media/uploadvideo', [
|
||||
'media_id' => $mediaId,
|
||||
'title' => $title,
|
||||
'description' => $description,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch item from WeChat server.
|
||||
*
|
||||
* @return \TuzhiEasyWeChat\Kernel\Http\StreamResponse|\Psr\Http\Message\ResponseInterface|\TuzhiEasyWeChat\Kernel\Support\Collection|array|object|string
|
||||
*
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function get(string $mediaId)
|
||||
{
|
||||
$response = $this->requestRaw('cgi-bin/media/get', 'GET', [
|
||||
'query' => [
|
||||
'media_id' => $mediaId,
|
||||
],
|
||||
]);
|
||||
|
||||
if (false !== stripos($response->getHeaderLine('Content-disposition'), 'attachment')) {
|
||||
return StreamResponse::buildFromPsrResponse($response);
|
||||
}
|
||||
|
||||
return $this->castResponseToType($response, $this->app['config']->get('response_type'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|\TuzhiEasyWeChat\Kernel\Http\Response|\TuzhiEasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
|
||||
*
|
||||
* @throws \TuzhiEasyWeChat\Kernel\Exceptions\InvalidConfigException
|
||||
* @throws \GuzzleHttp\Exception\GuzzleException
|
||||
*/
|
||||
public function getJssdkMedia(string $mediaId)
|
||||
{
|
||||
$response = $this->requestRaw('cgi-bin/media/get/jssdk', 'GET', [
|
||||
'query' => [
|
||||
'media_id' => $mediaId,
|
||||
],
|
||||
]);
|
||||
|
||||
if (false !== stripos($response->getHeaderLine('Content-disposition'), 'attachment')) {
|
||||
return StreamResponse::buildFromPsrResponse($response);
|
||||
}
|
||||
|
||||
return $this->castResponseToType($response, $this->app['config']->get('response_type'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
/**
|
||||
* ServiceProvider.php.
|
||||
*
|
||||
* This file is part of the 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 TuzhiEasyWeChat\BasicService\Media;
|
||||
|
||||
use Pimple\Container;
|
||||
use Pimple\ServiceProviderInterface;
|
||||
|
||||
/**
|
||||
* Class ServiceProvider.
|
||||
*
|
||||
* @author overtrue <i@overtrue.me>
|
||||
*/
|
||||
class ServiceProvider implements ServiceProviderInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}.
|
||||
*/
|
||||
public function register(Container $app)
|
||||
{
|
||||
$app['media'] = function ($app) {
|
||||
return new Client($app);
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user