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.
payment/app/Helper/Efps/AbstractApi.php

77 lines
2.3 KiB
PHTML

2 years ago
<?php
namespace App\Helper\Efps;
2 years ago
use App\Helper\Efps\Request\AbstractRequest;
2 years ago
use App\Helper\Log;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Hyperf\Guzzle\CoroutineHandler;
abstract class AbstractApi
{
protected static $client;
1 year ago
protected static $env = 'prod';
2 years ago
public static function getConfig($key)
{
$config = Config::get(self::$env);
return $config ? ($config[$key] ?? null) : null;
}
public static function request($uri, $params, $sign) {
2 years ago
try {
1 year ago
Log::info('baseUrl:' . self::getConfig('baseUrl'), [], 'efps');
2 years ago
Log::info('url:' . $uri, [], 'efps');
1 year ago
Log::info('headers:', self::getXEfpsHeaders($sign), 'efps');
2 years ago
$response = self::getClient()->post($uri, [
'json' => $params,
'headers' => self::getXEfpsHeaders($sign),
2 years ago
]);
2 years ago
Log::info('request:', $params, 'efps');
2 years ago
$body = (string)$response->getBody();
2 years ago
Log::info('response:' . $body, [], 'efps');
2 years ago
$result = json_decode($body, true);
if (empty($result)) {
$result = [
2 years ago
'returnCode' => '9999',
'returnMsg' => '返回数据异常',
2 years ago
];
}
return new Result($result);
2 years ago
} catch (\Exception $e) {
Log::error('error:' . $e->getMessage(), [], 'efps');
2 years ago
return new Result([
2 years ago
'returnCode' => '9998',
'returnMsg' => '网络错误',
2 years ago
]);
}
}
protected static function getClient(): Client {
if (!self::$client) {
self::$client = new Client([
2 years ago
'base_uri' => self::getConfig('baseUrl'),
2 years ago
'handler' => HandlerStack::create(new CoroutineHandler()),
1 year ago
'timeout' => 20,
2 years ago
'swoole' => [
1 year ago
'timeout' => 20,
2 years ago
'socket_buffer_size' => 1024 * 1024 * 2,
],
]);
}
return self::$client;
}
2 years ago
protected static function getXEfpsHeaders($sign)
2 years ago
{
return [
2 years ago
'x-efps-sign-no' => self::getConfig('signNo'),
'x-efps-sign-type' => 'SHA256withRSA',
2 years ago
'x-efps-sign' => $sign,
2 years ago
'x-efps-timestamp' => date('YmdHis')
2 years ago
];
2 years ago
}
}