This commit is contained in:
候学杰 2018-03-01 17:57:25 +08:00
commit e3f2a69a00
6 changed files with 1357 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
vendor/
.idea/

8
composer.json Normal file
View 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

File diff suppressed because it is too large Load Diff

61
src/Client.php Normal file
View 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);
}
}

View 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);
}
}

View 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);
});
}
}
}