This commit is contained in:
候学杰 2018-03-15 18:29:40 +08:00
parent b21c0c605f
commit c06885775e
4 changed files with 37 additions and 28 deletions

View File

@ -8,6 +8,9 @@
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"InternalApi\\": "src/" "InternalApi\\": "src/"
} },
"files": [
"helpers.php"
]
} }
} }

View File

@ -39,23 +39,13 @@ class Client
$client = new \GuzzleHttp\Client($config); $client = new \GuzzleHttp\Client($config);
$params['appid'] = $config['appid']; $params['appid'] = $config['appid'];
$params['timestamp'] = time(); $params['timestamp'] = time();
$params['sign'] = $this->sign($params); $key = $this->config[$this->currentApp]['secret'];
$params['sign'] = sign($params, $key);
$resp = $client->post($uri, ['form_params' => $params]); $resp = $client->post($uri, ['form_params' => $params]);
if ($resp->getStatusCode() == 200) { if ($resp->getStatusCode() == 200) {
return \GuzzleHttp\json_decode($resp->getBody(), true); return \GuzzleHttp\json_decode($resp->getBody(), true);
} else {
throw new \Exception('request failed');
} }
} return false;
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

@ -5,6 +5,7 @@ namespace InternalApi\Middleware;
use Closure; use Closure;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use function InternalApi\sign;
class InternalApi class InternalApi
{ {
@ -34,22 +35,29 @@ class InternalApi
$params = $request->all(); $params = $request->all();
if (empty($params['appid'])) { if (empty($params['appid'])) {
$data = ['error' => '参数错误',]; $data = ['error' => 'require appid',];
return new JsonResponse($data, 403); return new JsonResponse($data, 403);
} }
if (empty($params['timestamp'])) { if (empty($params['timestamp'])) {
$data = ['error' => '签名已过有效期',]; $data = ['error' => 'require time',];
return new JsonResponse($data, 403); return new JsonResponse($data, 403);
} elseif (intval($params['timestamp']) + 60 < time()) { } elseif (intval($params['timestamp']) + 60 < time()) {
$data = ['error' => '签名已过有效期',]; $data = ['error' => 'sign expired',];
return new JsonResponse($data, 403); return new JsonResponse($data, 403);
} }
$sign = $this->sign($params); $key = config('internal_api.server.' . $params['appid']);
if (empty($key)) {
$data = ['error' => 'config error',];
return new JsonResponse($data, 403);
}
$sign = sign($params, $key);
if ($sign != $params['sign']) { if ($sign != $params['sign']) {
$data = [ $data = [
'error' => '签名错误', 'error' => 'sign error',
]; ];
return new JsonResponse($data, 403); return new JsonResponse($data, 403);
} }
@ -57,13 +65,4 @@ class InternalApi
return $next($request); return $next($request);
} }
protected function sign($params)
{
$key = config('internal_api.server.' . $params['appid']);
unset($params['sign']);
ksort($params);
$str = http_build_query($params, null, '&');
return md5($str . $key);
}
} }

17
src/helpers.php Normal file
View File

@ -0,0 +1,17 @@
<?php
namespace InternalApi;
/**
* 签名
* @param $params
* @param $key
* @return string
*/
function sign($params, $key)
{
unset($params['sign']);
ksort($params);
$str = http_build_query($params, null, '&');
return md5($str . $key);
}