From bea811606c91529d3353ea75d956e9ea9aec1e44 Mon Sep 17 00:00:00 2001 From: ykxiao Date: Sat, 9 Sep 2023 14:55:35 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 12 ++++++++- src/OrderActions/Order.php | 50 ++++++++++++++++++++++++++++++++++---- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index c98cda7..5199bd5 100644 --- a/README.md +++ b/README.md @@ -17,9 +17,19 @@ 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'); diff --git a/src/OrderActions/Order.php b/src/OrderActions/Order.php index 73f8bd9..c8a833f 100644 --- a/src/OrderActions/Order.php +++ b/src/OrderActions/Order.php @@ -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) { @@ -132,7 +172,7 @@ class Order /** * 加工工单创建 * @return mixed|string - * @throws HttpException|GuzzleException + * @throws \Exception|GuzzleException */ public function createProductOrder($data) { From 9b3913ed608058318b341753d999c4cda16b33ae Mon Sep 17 00:00:00 2001 From: ykxiao Date: Sat, 9 Sep 2023 15:01:31 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5199bd5..57eca07 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ $secretKey = 'gFBfirGATxafTeq74RAngaL74Ksdxhuy'; $options = [ 'logger' => true, - 'logs_path' => storage_path('logs/mes-sdk/sdk.log'), + 'logs_path' => storage_path('logs/mes-sdk'), ]; $w = new Order($secretKey, 'callback url', $options); From 06a645973e575cc76e277bbdec6ab8c5f7c4ea8e Mon Sep 17 00:00:00 2001 From: ykxiao Date: Sat, 9 Sep 2023 15:36:29 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=AF=B7=E6=B1=82?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E6=97=A5=E5=BF=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/OrderActions/Order.php | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/OrderActions/Order.php b/src/OrderActions/Order.php index c8a833f..eac6eb3 100644 --- a/src/OrderActions/Order.php +++ b/src/OrderActions/Order.php @@ -124,10 +124,10 @@ class Order $result = 'json' === self::DEFAULT_FORMAT ? json_decode($response, true) : $response; // 日志 - $this->logger(['response' => $result]); + $this->logger(['request' => $body, 'response' => $result]); return $result; } catch (\Exception $e) { - $this->logger(['response' => $e]); + $this->logger(['request' => $body, 'response' => $e]); throw new HttpException($e->getMessage(), $e->getCode(), $e); } } @@ -136,7 +136,7 @@ class Order * 获取工单信息 * @param $order_sn * @return mixed|string - * @throws \Exception|GuzzleException + * @throws GuzzleException */ public function getProductOrder($order_sn) { @@ -150,29 +150,26 @@ class Order * 加工工单原木材种 * @return mixed|string * @throws GuzzleException - * @throws HttpException - * @throws InvalidArgumentException */ public function getWoodTypeOptions($data) { - return $this->baseFun($data, 'Wood.types'); + return $this->sendRequest('Wood.types', $data); } + /** * 加工等级 * @return mixed|string * @throws GuzzleException - * @throws HttpException - * @throws InvalidArgumentException */ public function getProductionLevelOptions($data) { - return $this->baseFun($data, 'production.levels'); + return $this->sendRequest('production.levels', $data); } /** * 加工工单创建 * @return mixed|string - * @throws \Exception|GuzzleException + * @throws GuzzleException */ public function createProductOrder($data) {