增加回调逻辑,PHP版本,其他优化
This commit is contained in:
@ -11,89 +11,82 @@ namespace Ykxiao\Dmmes\OrderActions;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Ykxiao\Dmmes\Exceptions\HttpException;
|
||||
use Ykxiao\Dmmes\Exceptions\InvalidArgumentException;
|
||||
use function in_array;
|
||||
use function strtolower;
|
||||
|
||||
class Order
|
||||
{
|
||||
protected const API_URL = 'https://api.dev.dwoodauto.com/api/v3/';
|
||||
protected const DEFAULT_FORMAT = 'json';
|
||||
protected const DEFAULT_TYPE = 'base';
|
||||
|
||||
protected $guzzleOptions = [];
|
||||
protected $apiUrl = 'https://api.dev.dwoodauto.com/api/v3/';
|
||||
protected $data = [];
|
||||
protected $httpClient;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $secretKey;
|
||||
/**
|
||||
* @var mixed|string
|
||||
*/
|
||||
private $type;
|
||||
/**
|
||||
* @var mixed|string
|
||||
*/
|
||||
private $format;
|
||||
protected $secretKey;
|
||||
protected $callback;
|
||||
|
||||
public function __construct($secretKey, $type = 'base', $format = 'json')
|
||||
public function __construct($secretKey, $callback = '')
|
||||
{
|
||||
$this->secretKey = $secretKey;
|
||||
$this->type = $type;
|
||||
$this->format = $format;
|
||||
$this->httpClient = new Client($this->guzzleOptions);
|
||||
$this->callback = $callback;
|
||||
}
|
||||
|
||||
public function getHttpClient()
|
||||
public function getHttpClient(): Client
|
||||
{
|
||||
return new Client($this->guzzleOptions);
|
||||
}
|
||||
|
||||
public function setGuzzleOptions(array $options)
|
||||
{
|
||||
$this->guzzleOptions = $options;
|
||||
return $this->httpClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求参数签名
|
||||
* @param $params
|
||||
* @param $api
|
||||
* @return mixed|string
|
||||
* @throws GuzzleException
|
||||
* @throws HttpException
|
||||
* @throws InvalidArgumentException
|
||||
* @return string
|
||||
*/
|
||||
public function baseFun($params, $api)
|
||||
private function generateSignature($params): string
|
||||
{
|
||||
$url = $this->apiUrl . $api;
|
||||
return (new Sign())->generateHmacSignature($this->secretKey, $params);
|
||||
}
|
||||
|
||||
if (!in_array(strtolower($this->format), ['xml', 'json'])) {
|
||||
throw new InvalidArgumentException('Invalid response format: ' . $this->format);
|
||||
}
|
||||
|
||||
if (!in_array(strtolower($this->type), ['base', 'all'])) {
|
||||
throw new InvalidArgumentException('Invalid type value(base/all): ' . $this->type);
|
||||
}
|
||||
/**
|
||||
* 接口请求
|
||||
* @param $api
|
||||
* @param $data
|
||||
* @return mixed|string
|
||||
* @throws HttpException|GuzzleException
|
||||
*/
|
||||
private function sendRequest($api, $data)
|
||||
{
|
||||
$url = self::API_URL . $api;
|
||||
|
||||
// 附加参数
|
||||
$params['times'] = time();
|
||||
$params['output'] = $this->format;
|
||||
$params['extensions'] = $this->type;
|
||||
$params = array_merge([
|
||||
'times' => time(),
|
||||
'output' => self::DEFAULT_FORMAT,
|
||||
'extensions' => self::DEFAULT_TYPE,
|
||||
], $data);
|
||||
|
||||
$signature = (new Sign())->generateHmacSignature($this->secretKey, $params);
|
||||
$signature = $this->generateSignature($params);
|
||||
|
||||
// 请求参数
|
||||
$body = [
|
||||
'signature' => $signature,
|
||||
'data' => $params
|
||||
];
|
||||
if ($this->callback != '') {
|
||||
$body = array_merge(['callback' => $this->callback], $body);
|
||||
}
|
||||
$params = [
|
||||
'headers' => ['content-type' => 'application/json', 'accept' => 'application/json'],
|
||||
'body' => json_encode([
|
||||
'signature' => $signature,
|
||||
'data' => $params
|
||||
]),
|
||||
'headers' => ['content-type' => 'application/json', 'accept' => 'application/json', 'user-agent' => 'mes sdk'],
|
||||
'body' => json_encode($body),
|
||||
'http_errors' => false
|
||||
];
|
||||
|
||||
try {
|
||||
$response = $this->getHttpClient()
|
||||
$response = $this->httpClient
|
||||
->request('POST', $url, $params)
|
||||
->getBody()
|
||||
->getContents();
|
||||
|
||||
return 'json' === $this->format ? json_decode($response, true) : $response;
|
||||
return 'json' === self::DEFAULT_FORMAT ? json_decode($response, true) : $response;
|
||||
} catch (\Exception $e) {
|
||||
throw new HttpException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
@ -103,27 +96,23 @@ class Order
|
||||
* 获取工单信息
|
||||
* @param $order_sn
|
||||
* @return mixed|string
|
||||
* @throws GuzzleException
|
||||
* @throws HttpException
|
||||
* @throws InvalidArgumentException
|
||||
* @throws HttpException|GuzzleException
|
||||
*/
|
||||
public function getProductOrder($order_sn)
|
||||
{
|
||||
$data = [
|
||||
'order_sn' => $order_sn
|
||||
];
|
||||
return $this->baseFun($data, 'production.status');
|
||||
return $this->sendRequest('production.status', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加工工单创建
|
||||
* @return mixed|string
|
||||
* @throws GuzzleException
|
||||
* @throws HttpException
|
||||
* @throws InvalidArgumentException
|
||||
* @throws HttpException|GuzzleException
|
||||
*/
|
||||
public function createProductOrder($data)
|
||||
{
|
||||
return $this->baseFun($data, 'production.create');
|
||||
return $this->sendRequest('production.create', $data);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user