90 lines
2.3 KiB
PHP
90 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace Extend\Kuaidi100;
|
|
|
|
use App\Constants\HttpEnumCode;
|
|
use App\Exception\BusinessException;
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
use Hyperf\Utils\ApplicationContext;
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
use Psr\Container\ContainerInterface;
|
|
use Psr\Container\NotFoundExceptionInterface;
|
|
|
|
/**
|
|
* 快递100对接
|
|
*/
|
|
class Kuaidi
|
|
{
|
|
#[Inject]
|
|
protected ContainerInterface $container;
|
|
|
|
#[Inject]
|
|
protected Client $client;
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->container = ApplicationContext::getContainer();
|
|
$this->client = $this->container->get(Client::class);
|
|
}
|
|
|
|
/**
|
|
* 封装公共请求
|
|
* @param string $path
|
|
* @param array $arg
|
|
* @return mixed
|
|
* @throws GuzzleException
|
|
*/
|
|
protected function httpRequest(string $path, string $arg = []): mixed
|
|
{
|
|
$option = [
|
|
"json" => [
|
|
"schema" => "json",
|
|
"param" => json_encode($params, JSON_UNESCAPED_UNICODE),
|
|
],
|
|
];
|
|
|
|
$arg = array_merge($arg, $option);
|
|
|
|
$response = $this->client->post($path, $arg);
|
|
|
|
if ($response->getStatusCode() != '200') {
|
|
// 请求失败
|
|
throw new BusinessException($response->getBody()->getContents());
|
|
}
|
|
$body = json_decode($response->getBody(), true);
|
|
dump($body);
|
|
if (empty($body)) {
|
|
// 返回值为空
|
|
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
|
}
|
|
|
|
if ($body['result_code'] != 0) {
|
|
// 请求失败
|
|
if (!empty($body['result_msg'])) {
|
|
throw new BusinessException($body['result_msg']);
|
|
}
|
|
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
|
}
|
|
|
|
return $body['body'];
|
|
}
|
|
|
|
/**
|
|
* 获取签名
|
|
* @param string $params
|
|
* @return string
|
|
*/
|
|
protected function getSign(string $params): string
|
|
{
|
|
return md5($params . config("kuaidi100.key") . config("kuaidi100.customer"));
|
|
}
|
|
|
|
public function
|
|
|
|
} |