德木自动化MES系统SDK初始版本

This commit is contained in:
ykxiao
2023-09-07 14:18:40 +08:00
commit a7fe8503fd
11 changed files with 344 additions and 0 deletions

114
src/OrderActions/Order.php Normal file
View File

@ -0,0 +1,114 @@
<?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
* @return mixed|string
* @throws GuzzleException
* @throws HttpException
* @throws InvalidArgumentException
*/
public function baseFun($params)
{
$url = $this->apiUrl . 'production.status';
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['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);
}
}