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": {
"psr-4": {
"InternalApi\\": "src/"
}
},
"files": [
"helpers.php"
]
}
}

View File

@ -39,23 +39,13 @@ class Client
$client = new \GuzzleHttp\Client($config);
$params['appid'] = $config['appid'];
$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]);
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);
return false;
}
}

View File

@ -5,6 +5,7 @@ namespace InternalApi\Middleware;
use Closure;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Str;
use function InternalApi\sign;
class InternalApi
{
@ -34,22 +35,29 @@ class InternalApi
$params = $request->all();
if (empty($params['appid'])) {
$data = ['error' => '参数错误',];
$data = ['error' => 'require appid',];
return new JsonResponse($data, 403);
}
if (empty($params['timestamp'])) {
$data = ['error' => '签名已过有效期',];
$data = ['error' => 'require time',];
return new JsonResponse($data, 403);
} elseif (intval($params['timestamp']) + 60 < time()) {
$data = ['error' => '签名已过有效期',];
$data = ['error' => 'sign expired',];
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']) {
$data = [
'error' => '签名错误',
'error' => 'sign error',
];
return new JsonResponse($data, 403);
}
@ -57,13 +65,4 @@ class InternalApi
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);
}