mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKit.git
synced 2026-01-15 00:35:08 +08:00
fix(httpRequest): 修改http请求
This commit is contained in:
@@ -49,4 +49,12 @@ return [
|
||||
), // 强烈建议按此格式划分
|
||||
],
|
||||
|
||||
// account http请求配置
|
||||
'http_request' => [
|
||||
'account' => [
|
||||
'api_base_uri' => env('API_BASE_URI', ''),
|
||||
'rpc_base_uri' => env('RPC_BASE_URI', ''),
|
||||
],
|
||||
],
|
||||
|
||||
];
|
||||
@@ -2,24 +2,26 @@
|
||||
|
||||
namespace Singularity\HDK\Account\Services\Http;
|
||||
|
||||
use Hyperf\Contract\TranslatorInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Guzzle\ClientFactory;
|
||||
use Singularity\HDK\Utils\Service\HttpRequestService;
|
||||
|
||||
class UserApi extends HttpRequestService
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var ClientFactory
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
private ClientFactory $clientFactory;
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$options = [
|
||||
'base_uri' => 'http://test-accountx.luxcreo.cn/cn/api/v1/'
|
||||
];
|
||||
$this->client = $this->clientFactory->create($options);
|
||||
parent::__construct(
|
||||
[
|
||||
'Accept-Language' => $this->translator->getLocale()
|
||||
],
|
||||
config('common.http_request.account.api_base_uri')
|
||||
);
|
||||
}
|
||||
|
||||
public function sendCode($params)
|
||||
|
||||
@@ -3,24 +3,28 @@
|
||||
namespace Singularity\HDK\Account\Services\Http;
|
||||
|
||||
|
||||
use Hyperf\Contract\TranslatorInterface;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Guzzle\ClientFactory;
|
||||
use Singularity\HDK\Utils\Service\HttpRequestService;
|
||||
|
||||
class UserRpc extends HttpRequestService
|
||||
{
|
||||
/**
|
||||
* @Inject
|
||||
* @var ClientFactory
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
private ClientFactory $clientFactory;
|
||||
private TranslatorInterface $translator;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$options = [
|
||||
'base_uri' => 'http://test-accountx.luxcreo.cn/cn/api/rpc/'
|
||||
];
|
||||
$this->client = $this->clientFactory->create($options);
|
||||
parent::__construct(
|
||||
[
|
||||
'headers' => [
|
||||
'Accept-Language' => $this->translator->getLocale()
|
||||
]
|
||||
],
|
||||
config('common.http_request.account.rpc_base_uri')
|
||||
);
|
||||
}
|
||||
|
||||
public function alive($name)
|
||||
|
||||
@@ -2,7 +2,9 @@
|
||||
|
||||
namespace Singularity\HDK\Utils\Service;
|
||||
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Cookie\CookieJar;
|
||||
use GuzzleHttp\Exception\ClientException;
|
||||
use GuzzleHttp\Exception\ServerException;
|
||||
|
||||
@@ -11,8 +13,32 @@ use GuzzleHttp\Exception\ServerException;
|
||||
*/
|
||||
class HttpRequestService
|
||||
{
|
||||
/**
|
||||
* @var \GuzzleHttp\Client
|
||||
*/
|
||||
protected Client $client;
|
||||
|
||||
protected function __construct(array $headers = [], string $base_uri = '', array $cookie = [])
|
||||
{
|
||||
$options = [
|
||||
'timeout' => 20,
|
||||
'headers' => $headers
|
||||
];
|
||||
if (!empty($base_uri)) {
|
||||
$options['base_uri'] = $base_uri;
|
||||
}
|
||||
if (!empty($cookie)) {
|
||||
$options['cookies'] = CookieJar::fromArray(
|
||||
[
|
||||
$this->cookie['key'] => $this->cookie['value']
|
||||
],
|
||||
parse_url($base_uri)['host']
|
||||
);
|
||||
}
|
||||
var_dump($options);
|
||||
$this->client = new Client($options);
|
||||
}
|
||||
|
||||
protected function get($uri)
|
||||
{
|
||||
return $this->bar($uri);
|
||||
@@ -31,7 +57,7 @@ class HttpRequestService
|
||||
} catch (ClientException|ServerException $e) {
|
||||
$response = $e->getResponse();
|
||||
$result = $this->jsonToArray($response);
|
||||
$result['httpStatus'] = $response->getStatusCode();
|
||||
$result['httpCode'] = $response->getStatusCode();
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
||||
Reference in New Issue
Block a user