dmmes/src/OrderActions/Order.php

118 lines
2.9 KiB
PHP

<?php
/**
* Author: ykxiao
* Date: 2023/9/6
* Time: 20:01
* Description: 工单
*/
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 $guzzleOptions = [];
protected $apiUrl = 'https://api.dev.dwoodauto.com/api/v3/';
protected $data = [];
/**
* @var string
*/
private $secretKey;
/**
* @var mixed|string
*/
private $type;
/**
* @var mixed|string
*/
private $format;
public function __construct($secretKey, $type = 'base', $format = 'json')
{
$this->secretKey = $secretKey;
$this->type = $type;
$this->format = $format;
}
public function getHttpClient()
{
return new Client($this->guzzleOptions);
}
public function setGuzzleOptions(array $options)
{
$this->guzzleOptions = $options;
}
/**
* @param $params
* @param $api
* @return mixed|string
* @throws GuzzleException
* @throws HttpException
* @throws InvalidArgumentException
*/
public function baseFun($params, $api)
{
$url = $this->apiUrl . $api;
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);
}
// 附加参数
$params['times'] = time();
$params['output'] = $this->format;
$params['extensions'] = $this->type;
$signature = (new Sign())->generateHmacSignature($this->secretKey, $params);
$params = [
'headers' => ['content-type' => 'application/json', 'accept' => 'application/json'],
'body' => json_encode([
'signature' => $signature,
'data' => $params
]),
'http_errors' => false
];
try {
$response = $this->getHttpClient()
->request('POST', $url, $params)
->getBody()
->getContents();
return 'json' === $this->format ? json_decode($response, true) : $response;
} catch (\Exception $e) {
throw new HttpException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* 获取工单信息
* @param $order_sn
* @return mixed|string
* @throws GuzzleException
* @throws HttpException
* @throws InvalidArgumentException
*/
public function getProductOrder($order_sn)
{
$data = [
'order_sn' => $order_sn
];
return $this->baseFun($data, 'production.status');
}
}