117 lines
3.4 KiB
PHP
117 lines
3.4 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;
|
|
|
|
protected string $domain_name;
|
|
|
|
/**
|
|
* @throws ContainerExceptionInterface
|
|
* @throws NotFoundExceptionInterface
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->container = ApplicationContext::getContainer();
|
|
$this->client = $this->container->get(Client::class);
|
|
|
|
$app_env = config('app_env','dev');
|
|
if ($app_env == "prod"){
|
|
$this->domain_name = env('DOMAIN_NAME_PROD','https://prod.hospital.applets.igandanyiyuan.com/');
|
|
}else{
|
|
$this->domain_name = env('DOMAIN_NAME_DEV','https://dev.hospital.applets.igandanyiyuan.com/');
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* 获取签名
|
|
* @param string $params
|
|
* @return string
|
|
*/
|
|
protected function getSign(string $params): string
|
|
{
|
|
return md5($params . config("kuaidi100.key") . config("kuaidi100.customer"));
|
|
}
|
|
|
|
|
|
/**
|
|
* 订阅快递推送
|
|
* @param string $logistics_no 快递号
|
|
* @param string $logistics_company_code 快递公司编码
|
|
* @param string $phone 手机号
|
|
* @throws GuzzleException
|
|
*/
|
|
public function subscribe(string $logistics_no,string $logistics_company_code,string $phone = ""){
|
|
$param = [
|
|
'company' => $logistics_company_code, // 快递公司编码
|
|
'number' => $logistics_no, // 快递单号
|
|
'key' => config("kuaidi100.key"), // 客户授权key
|
|
'parameters' => array (
|
|
'callbackurl' => $this->domain_name . 'callback/logistics', // 回调地址
|
|
'resultv2' => '1', // 行政区域解析
|
|
'phone' => $phone ?: "", // 手机号
|
|
'salt' => config('kuaidi100.salt'), // 签名用字符串
|
|
)
|
|
];
|
|
|
|
$option = [
|
|
"form_params" => [
|
|
"schema" => "json",
|
|
"param" => json_encode($param, JSON_UNESCAPED_UNICODE),
|
|
],
|
|
"verify" => false
|
|
];
|
|
|
|
$path = "https://poll.kuaidi100.com/poll";
|
|
|
|
$response = $this->client->post($path, $option);
|
|
|
|
if ($response->getStatusCode() != '200') {
|
|
// 请求失败
|
|
throw new BusinessException($response->getBody()->getContents());
|
|
}
|
|
$body = json_decode($response->getBody(), true);
|
|
|
|
if (empty($body)) {
|
|
// 返回值为空
|
|
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
|
}
|
|
|
|
if ($body['returnCode'] != 200) {
|
|
// 重复订阅-不做处理
|
|
if ($body['returnCode'] == 501){
|
|
return $body;
|
|
}
|
|
|
|
// 请求失败
|
|
if (!empty($body['message'])) {
|
|
throw new BusinessException($body['message']);
|
|
}
|
|
throw new BusinessException(HttpEnumCode::getMessage(HttpEnumCode::SERVER_ERROR));
|
|
}
|
|
|
|
return $body;
|
|
}
|
|
|
|
|
|
} |