This repository has been archived by the owner on May 8, 2021. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3179228
Showing
21 changed files
with
1,106 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the mingyoung/wechat-playground. | ||
* | ||
* (c) 张铭阳 <mingyoungcheung@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace WeChatPlayground\EasyWeChat; | ||
|
||
use EasyWeChat\Kernel\BaseClient; | ||
|
||
class Client extends BaseClient | ||
{ | ||
/** | ||
* @param string $method | ||
* @param string $uri | ||
* @param array $options | ||
* | ||
* @return \WeChatPlayground\EasyWeChat\Response | ||
*/ | ||
public function perform($method, $uri, $options = []) | ||
{ | ||
switch ($method) { | ||
case 'GET': | ||
return $this->makesHttpRequest($uri, 'GET', ['query' => $options['query']]); | ||
case 'POST': | ||
return $this->makesHttpRequest($uri, 'POST', ['json' => $options['json']]); | ||
case 'UPLOAD': | ||
$multipart = []; | ||
|
||
foreach ($options['multipart'] as $name => $path) { | ||
$multipart[] = [ | ||
'name' => $name, | ||
'contents' => fopen($path, 'r'), | ||
]; | ||
} | ||
|
||
return $this->makesHttpRequest($uri, 'POST', ['query' => $options['query'], 'multipart' => $multipart]); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $uri | ||
* @param string $method | ||
* @param array $options | ||
* | ||
* @return \WeChatPlayground\EasyWeChat\Response | ||
*/ | ||
protected function makesHttpRequest($uri, $method, $options = []) | ||
{ | ||
return new Response( | ||
$this->requestRaw($uri, $method, array_merge(['on_stats' => $statable = new Statable()], $options)), $statable | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the mingyoung/wechat-playground. | ||
* | ||
* (c) 张铭阳 <mingyoungcheung@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace WeChatPlayground\EasyWeChat; | ||
|
||
use EasyWeChat\OfficialAccount\Application; | ||
use WeChatPlayground\Playground; | ||
|
||
class OfficialAccount extends Application | ||
{ | ||
/** | ||
* @var \Illuminate\Foundation\Application | ||
*/ | ||
protected $laravel; | ||
|
||
/** | ||
* @param \Illuminate\Foundation\Application $laravel | ||
*/ | ||
public function __construct($laravel) | ||
{ | ||
$this->laravel = $laravel; | ||
|
||
parent::__construct($this->getPlaygroundConfig()); | ||
|
||
$this->register(new PlaygroundServiceProvider()); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function getPlaygroundConfig() | ||
{ | ||
$resolved = Playground::usingApp()->__invoke($this->laravel['request']); | ||
|
||
return [ | ||
'app_id' => $resolved['config']['app_id'], | ||
'secret' => $resolved['config']['secret'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the mingyoung/wechat-playground. | ||
* | ||
* (c) 张铭阳 <mingyoungcheung@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace WeChatPlayground\EasyWeChat; | ||
|
||
use Pimple\Container; | ||
use Pimple\ServiceProviderInterface; | ||
|
||
class PlaygroundServiceProvider implements ServiceProviderInterface | ||
{ | ||
/** | ||
* {@inheritdoc}. | ||
*/ | ||
public function register(Container $app) | ||
{ | ||
$app['playground'] = function ($app) { | ||
return new Client($app); | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the mingyoung/wechat-playground. | ||
* | ||
* (c) 张铭阳 <mingyoungcheung@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace WeChatPlayground\EasyWeChat; | ||
|
||
use EasyWeChat\Kernel\Support\File; | ||
use GuzzleHttp\Psr7\Uri; | ||
use Illuminate\Contracts\Support\Responsable; | ||
|
||
class Response implements Responsable | ||
{ | ||
/** | ||
* @var \EasyWeChat\Kernel\Http\Response | ||
*/ | ||
protected $response; | ||
|
||
/** | ||
* @var \WeChatPlayground\EasyWeChat\Statable | ||
*/ | ||
protected $statable; | ||
|
||
/** | ||
* @param \EasyWeChat\Kernel\Http\Response $response | ||
* @param \WeChatPlayground\EasyWeChat\Statable $statable | ||
*/ | ||
public function __construct($response, Statable $statable) | ||
{ | ||
$this->response = $response; | ||
$this->statable = $statable; | ||
} | ||
|
||
/** | ||
* Create an HTTP response that represents the object. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* | ||
* @return \Illuminate\Http\Response | ||
*/ | ||
public function toResponse($request) | ||
{ | ||
$uri = $this->statable->stats->getEffectiveUri(); | ||
|
||
return response()->json([ | ||
'payload' => $this->getPayload(), | ||
'response' => $this->getResponse(), | ||
'details' => [ | ||
['AppId' => app(OfficialAccount::class)['config']['app_id']], | ||
['Method' => $this->statable->stats->getRequest()->getMethod()], | ||
['API' => Uri::composeComponents($uri->getScheme(), $uri->getAuthority(), $uri->getPath(), $this->getQuery($uri), $uri->getFragment())], | ||
['Duration' => $this->statable->stats->getTransferTime().' 秒'], | ||
], | ||
]); | ||
} | ||
|
||
/** | ||
* @param \GuzzleHttp\Psr7\Uri $uri | ||
* | ||
* @return string | ||
*/ | ||
protected function getQuery($uri) | ||
{ | ||
parse_str($uri->getQuery(), $query); | ||
if (isset($query['access_token'])) { | ||
$query['access_token'] = 'ACCESS_TOKEN'; | ||
} | ||
|
||
return http_build_query($query); | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function getPayload() | ||
{ | ||
$body = $this->statable->stats->getRequest()->getBody(); | ||
|
||
return json_decode($body, true) ?? []; | ||
} | ||
|
||
/** | ||
* @return array | ||
*/ | ||
protected function getResponse() | ||
{ | ||
$response = $this->response; | ||
|
||
$array = json_decode($response, true); | ||
|
||
if (JSON_ERROR_NONE === json_last_error()) { | ||
return ['type' => 'json', 'content' => $array]; | ||
} | ||
|
||
return [ | ||
'type' => 'stream', | ||
'extension' => File::getStreamExt($response), | ||
'content' => base64_encode($response), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the mingyoung/wechat-playground. | ||
* | ||
* (c) 张铭阳 <mingyoungcheung@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace WeChatPlayground\EasyWeChat; | ||
|
||
use GuzzleHttp\TransferStats; | ||
|
||
class Statable | ||
{ | ||
/** | ||
* @var \GuzzleHttp\TransferStats | ||
*/ | ||
public $stats; | ||
|
||
/** | ||
* @param \GuzzleHttp\TransferStats $stats | ||
*/ | ||
public function __invoke(TransferStats $stats) | ||
{ | ||
$this->stats = $stats; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the mingyoung/wechat-playground. | ||
* | ||
* (c) 张铭阳 <mingyoungcheung@gmail.com> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace WeChatPlayground\Exceptions; | ||
|
||
class RuntimeException extends \RuntimeException | ||
{ | ||
} |
Oops, something went wrong.