dmmes/src/OrderActions/Order.php

119 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;
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 $httpClient;
protected $secretKey;
protected $callback;
public function __construct($secretKey, $callback = '')
{
$this->secretKey = $secretKey;
$this->httpClient = new Client($this->guzzleOptions);
$this->callback = $callback;
}
public function getHttpClient(): Client
{
return $this->httpClient;
}
/**
* 请求参数签名
* @param $params
* @return string
*/
private function generateSignature($params): string
{
return (new Sign())->generateHmacSignature($this->secretKey, $params);
}
/**
* 接口请求
* @param $api
* @param $data
* @return mixed|string
* @throws HttpException|GuzzleException
*/
private function sendRequest($api, $data)
{
$url = self::API_URL . $api;
// 附加参数
$params = array_merge([
'times' => time(),
'output' => self::DEFAULT_FORMAT,
'extensions' => self::DEFAULT_TYPE,
], $data);
$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', 'user-agent' => 'mes sdk'],
'body' => json_encode($body),
'http_errors' => false
];
try {
$response = $this->httpClient
->request('POST', $url, $params)
->getBody()
->getContents();
return 'json' === self::DEFAULT_FORMAT ? json_decode($response, true) : $response;
} catch (\Exception $e) {
throw new HttpException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* 获取工单信息
* @param $order_sn
* @return mixed|string
* @throws HttpException|GuzzleException
*/
public function getProductOrder($order_sn)
{
$data = [
'order_sn' => $order_sn
];
return $this->sendRequest('production.status', $data);
}
/**
* 加工工单创建
* @return mixed|string
* @throws HttpException|GuzzleException
*/
public function createProductOrder($data)
{
return $this->sendRequest('production.create', $data);
}
}