init
This commit is contained in:
commit
e3f2a69a00
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
vendor/
|
||||
.idea/
|
8
composer.json
Normal file
8
composer.json
Normal file
|
@ -0,0 +1,8 @@
|
|||
{
|
||||
"name": "pd_arch/inner_api",
|
||||
"type": "library",
|
||||
"require": {
|
||||
"guzzlehttp/guzzle": "^6.3",
|
||||
"illuminate/http": "^5.6"
|
||||
}
|
||||
}
|
1194
composer.lock
generated
Normal file
1194
composer.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
61
src/Client.php
Normal file
61
src/Client.php
Normal file
|
@ -0,0 +1,61 @@
|
|||
<?php
|
||||
|
||||
namespace InternalApi;
|
||||
|
||||
class Client
|
||||
{
|
||||
|
||||
protected $currentApp;
|
||||
protected $config;
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $app
|
||||
* @return $this
|
||||
*/
|
||||
public function app($app)
|
||||
{
|
||||
if (isset($this->config[$app]))
|
||||
$this->currentApp = $app;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用api,如果状态码不为200则抛出异常
|
||||
* @param $uri
|
||||
* @param $params
|
||||
* @return mixed
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function call($uri, $params)
|
||||
{
|
||||
$config = array_merge(['timeout' => 1],
|
||||
$this->config[$this->currentApp]);
|
||||
unset($config['secret']);
|
||||
$client = new \GuzzleHttp\Client($config);
|
||||
$params['appid'] = $this->currentApp;
|
||||
$params['timestamp'] = time();
|
||||
$params['sign'] = $this->sign($params);
|
||||
$resp = $client->post($uri, ['form_params' => $params]);
|
||||
if ($resp->getStatusCode() == 200) {
|
||||
return \GuzzleHttp\json_decode($resp->getBody(), true);
|
||||
} else {
|
||||
throw new \Exception('request failed');
|
||||
}
|
||||
}
|
||||
|
||||
protected function sign($params)
|
||||
{
|
||||
$key = $this->config[$this->currentApp]['secret'];
|
||||
|
||||
unset($params['sign']);
|
||||
ksort($params);
|
||||
$str = http_build_query($params, null, '&');
|
||||
return md5($str . $key);
|
||||
}
|
||||
|
||||
}
|
65
src/Middleware/InternalApi.php
Normal file
65
src/Middleware/InternalApi.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
namespace InternalApi\Middleware;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class InternalApi
|
||||
{
|
||||
|
||||
/**
|
||||
* Handle an incoming request.
|
||||
*
|
||||
* @param \Illuminate\Http\Request $request
|
||||
* @param \Closure $next
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
$ip = $request->getClientIp();
|
||||
|
||||
if (!Str::startsWith($ip, [
|
||||
'127.0.0.', '192.168.', '10.0.'
|
||||
])) {
|
||||
return new JsonResponse('', 404);
|
||||
}
|
||||
|
||||
$params = $request->all();
|
||||
|
||||
if (empty($params['appid'])) {
|
||||
$data = ['error' => '参数错误',];
|
||||
return new JsonResponse($data, 403);
|
||||
}
|
||||
|
||||
if (empty($params['timestamp'])) {
|
||||
$data = ['error' => '签名已过有效期',];
|
||||
return new JsonResponse($data, 403);
|
||||
} elseif (intval($params['timestamp']) + 60 < time()) {
|
||||
$data = ['error' => '签名已过有效期',];
|
||||
return new JsonResponse($data, 403);
|
||||
}
|
||||
|
||||
$sign = $this->sign($params);
|
||||
if ($sign != $params['sign']) {
|
||||
$data = [
|
||||
'error' => '签名错误',
|
||||
];
|
||||
return new JsonResponse($data, 403);
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
|
||||
protected function sign($params)
|
||||
{
|
||||
$app = $params['appid'];
|
||||
$key = config('internal_api.server.' . $app);
|
||||
unset($params['sign']);
|
||||
ksort($params);
|
||||
$str = http_build_query($params, null, '&');
|
||||
return md5($str . $key);
|
||||
}
|
||||
|
||||
}
|
27
src/ServiceProvider/InternalApiServiceProvider.php
Normal file
27
src/ServiceProvider/InternalApiServiceProvider.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace InternalApi\ServiceProvider;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class InternalApiServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Register any application services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->app->configure('internal_api');
|
||||
$this->app->singleton('internal.api', function () {
|
||||
return new Client(config('internal_api.client'));
|
||||
});
|
||||
foreach (config('internal_api.client') as $appid => $config) {
|
||||
$this->app->singleton('internal.api.' . $appid, function () use ($appid) {
|
||||
return $this->app['internal.api']->app($appid);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user