Compare commits
3 Commits
8c9680d364
...
bea811606c
Author | SHA1 | Date |
---|---|---|
ykxiao | bea811606c | |
hongwenwu | 4829305a44 | |
hongwenwu | c9484deaec |
34
README.md
34
README.md
|
@ -17,17 +17,45 @@ composer require ykxiao/dmmes
|
|||
# 使用
|
||||
|
||||
```php
|
||||
|
||||
// secretKey 必填
|
||||
// callback url 选填(业务逻辑需要时填写,详情查看SDK文档)
|
||||
// options 选填(日志选项,默认写入日志)
|
||||
|
||||
$secretKey = 'gFBfirGATxafTeq74RAngaL74Ksdxhuy';
|
||||
|
||||
$w = new Order($secretKey);
|
||||
$options = [
|
||||
'logger' => true,
|
||||
'logs_path' => storage_path('logs/mes-sdk/sdk.log'),
|
||||
];
|
||||
|
||||
$w = new Order($secretKey, 'callback url', $options);
|
||||
|
||||
// 获取工单数据
|
||||
$response = $w->getProductOrder('SN5436745676543');
|
||||
|
||||
// 加工工单原木材种
|
||||
$w->getWoodTypeOptions([
|
||||
'company_id' => 1
|
||||
]);
|
||||
|
||||
// 加工工单加工等级
|
||||
$w->getProductionLevelOptions([
|
||||
'company_id' => 1
|
||||
]);
|
||||
|
||||
// 创建加工工单
|
||||
$response = $w->createProductOrder([
|
||||
'cube_plan' => 32.23,
|
||||
'owner' => 'ykxiao'
|
||||
'company_id' => 1,
|
||||
'wood_type_id' => 1,
|
||||
'wood_level_id' => 1,
|
||||
'spec_thickness' => 12,
|
||||
'spec_width' => 22,
|
||||
'spec_length' => 38,
|
||||
'number_width' => 12,
|
||||
'number_height' => 22,
|
||||
'number_pcs' => 10,
|
||||
'owner' => 'ykxiao'
|
||||
]);
|
||||
```
|
||||
|
||||
|
|
|
@ -10,6 +10,7 @@ namespace Ykxiao\Dmmes\OrderActions;
|
|||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Ykxiao\Dmmes\Exceptions\Exception;
|
||||
use Ykxiao\Dmmes\Exceptions\HttpException;
|
||||
|
||||
class Order
|
||||
|
@ -23,12 +24,14 @@ class Order
|
|||
|
||||
protected $secretKey;
|
||||
protected $callback;
|
||||
protected $options = [];
|
||||
|
||||
public function __construct($secretKey, $callback = '')
|
||||
public function __construct($secretKey, $callback = '', $options = [])
|
||||
{
|
||||
$this->secretKey = $secretKey;
|
||||
$this->httpClient = new Client($this->guzzleOptions);
|
||||
$this->callback = $callback;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
public function getHttpClient(): Client
|
||||
|
@ -46,12 +49,45 @@ class Order
|
|||
return (new Sign())->generateHmacSignature($this->secretKey, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 日志
|
||||
* @param array $logs
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
private function logger(array $logs)
|
||||
{
|
||||
$logFolder = './storage/logs/mes-sdk'; // 默认日志文件夹路径
|
||||
$logFile = $logFolder . '/logs.log'; // 默认日志文件路径
|
||||
|
||||
// 如果有自定义日志路径,则使用自定义路径
|
||||
if (!empty($this->options['logger']) && !empty($this->options['logs_path'])) {
|
||||
$logFolder = $this->options['logs_path'];
|
||||
$logFile = $logFolder . '/logs.log';
|
||||
}
|
||||
|
||||
// 判断文件夹是否存在,如果不存在则创建文件夹
|
||||
if (!is_dir($logFolder)) {
|
||||
if (!mkdir($logFolder, 0777, true)) {
|
||||
throw new Exception("Failed to create log folder: $logFolder");
|
||||
}
|
||||
}
|
||||
|
||||
$timestamp = date('Y-m-d H:i:s');
|
||||
$logContent = "[$timestamp] " . json_encode($logs, JSON_UNESCAPED_UNICODE) . PHP_EOL;
|
||||
|
||||
// 打开日志文件并追加内容
|
||||
if (file_put_contents($logFile, $logContent, FILE_APPEND) === false) {
|
||||
throw new Exception("Failed to write to log file: $logFile");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 接口请求
|
||||
* @param $api
|
||||
* @param $data
|
||||
* @return mixed|string
|
||||
* @throws HttpException|GuzzleException
|
||||
* @throws \Exception|GuzzleException
|
||||
*/
|
||||
private function sendRequest($api, $data)
|
||||
{
|
||||
|
@ -86,8 +122,12 @@ class Order
|
|||
->getBody()
|
||||
->getContents();
|
||||
|
||||
return 'json' === self::DEFAULT_FORMAT ? json_decode($response, true) : $response;
|
||||
$result = 'json' === self::DEFAULT_FORMAT ? json_decode($response, true) : $response;
|
||||
// 日志
|
||||
$this->logger(['response' => $result]);
|
||||
return $result;
|
||||
} catch (\Exception $e) {
|
||||
$this->logger(['response' => $e]);
|
||||
throw new HttpException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
@ -96,7 +136,7 @@ class Order
|
|||
* 获取工单信息
|
||||
* @param $order_sn
|
||||
* @return mixed|string
|
||||
* @throws HttpException|GuzzleException
|
||||
* @throws \Exception|GuzzleException
|
||||
*/
|
||||
public function getProductOrder($order_sn)
|
||||
{
|
||||
|
@ -106,10 +146,33 @@ class Order
|
|||
return $this->sendRequest('production.status', $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加工工单原木材种
|
||||
* @return mixed|string
|
||||
* @throws GuzzleException
|
||||
* @throws HttpException
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getWoodTypeOptions($data)
|
||||
{
|
||||
return $this->baseFun($data, 'Wood.types');
|
||||
}
|
||||
/**
|
||||
* 加工等级
|
||||
* @return mixed|string
|
||||
* @throws GuzzleException
|
||||
* @throws HttpException
|
||||
* @throws InvalidArgumentException
|
||||
*/
|
||||
public function getProductionLevelOptions($data)
|
||||
{
|
||||
return $this->baseFun($data, 'production.levels');
|
||||
}
|
||||
|
||||
/**
|
||||
* 加工工单创建
|
||||
* @return mixed|string
|
||||
* @throws HttpException|GuzzleException
|
||||
* @throws \Exception|GuzzleException
|
||||
*/
|
||||
public function createProductOrder($data)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue