From 396e9d0581571900b635bd26b040a772b5c46d7d Mon Sep 17 00:00:00 2001 From: liufumingren Date: Wed, 11 Nov 2026 20:34:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(wms):=20=E6=96=B0=E5=A2=9E=E8=BF=90?= =?UTF-8?q?=E5=8D=95=E8=BD=A8=E8=BF=B9=E6=9F=A5=E8=AF=A2=E5=9B=9E=E8=B0=83?= =?UTF-8?q?=20waybill.getTrace?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - notify/WaybillTraceHandler:按 {request:{waybill_codes}} 批量查询轨迹(10 单并发), 输出裸数组 [{waybillNo, trace:[{opTime,outBizCode,memo,opOrgCityName,weight,scanType}]}] - notify/WaybillCodes:抽取 waybill.* 系列共用的入参解析(兼容平铺结构、去空、去重保序、限量) - WaybillReceiveHandler 改用 WaybillCodes,行为不变 - express/Trace::parseResult 增加 raw(上游原始轨迹),供 getTrace 原样透出 - Dispatcher 注册 waybill.getTrace Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- application/index/library/express/Trace.php | 1 + .../library/new_wms/notify/Dispatcher.php | 3 + .../library/new_wms/notify/WaybillCodes.php | 55 +++++++ .../new_wms/notify/WaybillReceiveHandler.php | 32 +---- .../new_wms/notify/WaybillTraceHandler.php | 135 ++++++++++++++++++ 5 files changed, 195 insertions(+), 31 deletions(-) create mode 100644 application/index/library/new_wms/notify/WaybillCodes.php create mode 100644 application/index/library/new_wms/notify/WaybillTraceHandler.php diff --git a/application/index/library/express/Trace.php b/application/index/library/express/Trace.php index 44a6cda..30e7260 100644 --- a/application/index/library/express/Trace.php +++ b/application/index/library/express/Trace.php @@ -576,6 +576,7 @@ class Trace 'company' => Recognizer::companyName($task['postcode']), 'ok' => true, 'traces' => $traces, + 'raw' => $raw, 'status' => $status, 'type' => $type, 'type_text' => self::typeText($type), diff --git a/application/index/library/new_wms/notify/Dispatcher.php b/application/index/library/new_wms/notify/Dispatcher.php index 5561f99..72f0796 100644 --- a/application/index/library/new_wms/notify/Dispatcher.php +++ b/application/index/library/new_wms/notify/Dispatcher.php @@ -58,6 +58,9 @@ class Dispatcher if (!isset(self::$handlers['waybill.getreceive'])) { self::register('waybill.getreceive', [new WaybillReceiveHandler(), 'handle']); } + if (!isset(self::$handlers['waybill.getTrace'])) { + self::register('waybill.getTrace', [new WaybillTraceHandler(), 'handle']); + } } // ==================== 处理器注册 ==================== diff --git a/application/index/library/new_wms/notify/WaybillCodes.php b/application/index/library/new_wms/notify/WaybillCodes.php new file mode 100644 index 0000000..cbfcba0 --- /dev/null +++ b/application/index/library/new_wms/notify/WaybillCodes.php @@ -0,0 +1,55 @@ += $max) { + break; + } + } + + return array_values($codes); + } +} diff --git a/application/index/library/new_wms/notify/WaybillReceiveHandler.php b/application/index/library/new_wms/notify/WaybillReceiveHandler.php index 691764d..fdca40b 100644 --- a/application/index/library/new_wms/notify/WaybillReceiveHandler.php +++ b/application/index/library/new_wms/notify/WaybillReceiveHandler.php @@ -76,37 +76,7 @@ class WaybillReceiveHandler */ protected function extractCodes(array $body) { - $raw = []; - if (isset($body['request']['waybill_codes'])) { - $raw = $body['request']['waybill_codes']; - } elseif (isset($body['request']['waybillCodes'])) { - $raw = $body['request']['waybillCodes']; - } elseif (isset($body['waybill_codes'])) { - $raw = $body['waybill_codes']; - } elseif (isset($body['waybillCodes'])) { - $raw = $body['waybillCodes']; - } - - if (!is_array($raw)) { - $raw = [$raw]; - } - - $codes = []; - foreach ($raw as $item) { - if (is_array($item) || is_object($item)) { - continue; - } - $code = trim((string)$item); - if ($code === '' || isset($codes[$code])) { - continue; - } - $codes[$code] = $code; - if (count($codes) >= self::MAX_CODES) { - break; - } - } - - return array_values($codes); + return WaybillCodes::extract($body, self::MAX_CODES); } /** diff --git a/application/index/library/new_wms/notify/WaybillTraceHandler.php b/application/index/library/new_wms/notify/WaybillTraceHandler.php new file mode 100644 index 0000000..5b74597 --- /dev/null +++ b/application/index/library/new_wms/notify/WaybillTraceHandler.php @@ -0,0 +1,135 @@ +queryTraces($codes); + + $rows = []; + $count = 0; + foreach ($codes as $code) { + $traces = isset($traceMap[$code]) ? $traceMap[$code] : []; + $count += count($traces); + $rows[] = [ + 'waybillNo' => $code, + 'trace' => $traces, + ]; + } + + \think\Log::info('[WmsNotify] waybill.getTrace 处理完成: 共 ' . count($codes) . ' 单,轨迹 ' . $count . ' 条'); + + return new RawResponse($rows); + } + + /** + * 批量查询轨迹(10 单并发) + * + * @param array $codes + * @return array [运单号 => 轨迹数组] + */ + protected function queryTraces(array $codes) + { + $map = array_fill_keys($codes, []); + + try { + $traceOptions = [ + 'timeout' => self::TRACE_TIMEOUT, + 'concurrency' => self::TRACE_CONCURRENCY, + 'batch_size' => 1, + 'retries' => 0, + ]; + $traceBaseUrl = config('wms.trace_base_url'); + if (!empty($traceBaseUrl)) { + $traceOptions['base_url'] = $traceBaseUrl; + } + $trace = new Trace($traceOptions); + $list = $trace->query($codes); + } catch (\Throwable $e) { + \think\Log::error('[WmsNotify] waybill.getTrace 轨迹查询异常: ' . $e->getMessage()); + return $map; + } + + foreach ($list as $index => $item) { + $code = isset($codes[$index]) ? $codes[$index] : ''; + if ($code === '' || empty($item['ok']) || empty($item['raw'])) { + continue; + } + $map[$code] = $this->flattenTraces($item['raw']); + } + + return $map; + } + + /** + * 上游返回 [{waybillNo, trace:{opTime,...}}, ...],这里取内层 trace 对象, + * 输出成调用方需要的 [{opTime, outBizCode, memo, opOrgCityName, weight, scanType}, ...] + * + * @param array $raw + * @return array + */ + protected function flattenTraces($raw) + { + if (!is_array($raw)) { + return []; + } + + $traces = []; + foreach ($raw as $item) { + if (!is_array($item)) { + continue; + } + if (isset($item['trace']) && is_array($item['trace'])) { + $traces[] = $item['trace']; + } else { + $traces[] = $item; + } + } + + return $traces; + } +}