You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.3 KiB
PHP
77 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace App\Helper\Efps;
|
|
|
|
use App\Helper\Efps\Request\AbstractRequest;
|
|
use App\Helper\Log;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\HandlerStack;
|
|
use Hyperf\Guzzle\CoroutineHandler;
|
|
|
|
abstract class AbstractApi
|
|
{
|
|
protected static $client;
|
|
|
|
protected static $env = 'prod';
|
|
|
|
public static function getConfig($key)
|
|
{
|
|
$config = Config::get(self::$env);
|
|
return $config ? ($config[$key] ?? null) : null;
|
|
}
|
|
|
|
public static function request($uri, $params, $sign) {
|
|
try {
|
|
Log::info('baseUrl:' . self::getConfig('baseUrl'), [], 'efps');
|
|
Log::info('url:' . $uri, [], 'efps');
|
|
Log::info('headers:', self::getXEfpsHeaders($sign), 'efps');
|
|
$response = self::getClient()->post($uri, [
|
|
'json' => $params,
|
|
'headers' => self::getXEfpsHeaders($sign),
|
|
]);
|
|
Log::info('request:', $params, 'efps');
|
|
$body = (string)$response->getBody();
|
|
Log::info('response:' . $body, [], 'efps');
|
|
$result = json_decode($body, true);
|
|
if (empty($result)) {
|
|
$result = [
|
|
'returnCode' => '9999',
|
|
'returnMsg' => '返回数据异常',
|
|
];
|
|
}
|
|
return new Result($result);
|
|
} catch (\Exception $e) {
|
|
Log::error('error:' . $e->getMessage(), [], 'efps');
|
|
return new Result([
|
|
'returnCode' => '9998',
|
|
'returnMsg' => '网络错误',
|
|
]);
|
|
}
|
|
|
|
}
|
|
|
|
protected static function getClient(): Client {
|
|
if (!self::$client) {
|
|
self::$client = new Client([
|
|
'base_uri' => self::getConfig('baseUrl'),
|
|
'handler' => HandlerStack::create(new CoroutineHandler()),
|
|
'timeout' => 20,
|
|
'swoole' => [
|
|
'timeout' => 20,
|
|
'socket_buffer_size' => 1024 * 1024 * 2,
|
|
],
|
|
]);
|
|
}
|
|
return self::$client;
|
|
}
|
|
|
|
protected static function getXEfpsHeaders($sign)
|
|
{
|
|
return [
|
|
'x-efps-sign-no' => self::getConfig('signNo'),
|
|
'x-efps-sign-type' => 'SHA256withRSA',
|
|
'x-efps-sign' => $sign,
|
|
'x-efps-timestamp' => date('YmdHis')
|
|
];
|
|
}
|
|
} |