From d953976391cb1b7efaa7b976db2e3837ad82e50f Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 12 Nov 2026 03:54:52 +0800 Subject: [PATCH] =?UTF-8?q?feat(wms):=20waybill=20=E5=85=A5=E5=8F=82?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E3=80=8C=E5=8D=95=E5=8F=B7|=E6=89=8B?= =?UTF-8?q?=E6=9C=BA=E5=8F=B7=E5=90=8E=E5=9B=9B=E4=BD=8D=E3=80=8D=EF=BC=8C?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E8=BD=A8=E8=BF=B9=E6=97=B6=E9=80=8F=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - WaybillCodes 支持拆分单号与手机号后四位(去空格/非数字,超长取后四位), 按单号去重保序,无后缀时行为与原来完全一致 - express/Trace 支持 phone:buildQueryString 在有 phone 时追加第 4 段 (单号|单号|公司代码|后四位),无 phone 时保持原格式;去重键改为 单号|phone - waybill.getTrace / waybill.getreceive 均透传 phone,响应中单号仍不含手机号 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- application/index/library/express/Trace.php | 40 ++++++- .../library/new_wms/notify/WaybillCodes.php | 109 ++++++++++++++---- .../new_wms/notify/WaybillReceiveHandler.php | 51 ++++---- .../new_wms/notify/WaybillTraceHandler.php | 32 +++-- 4 files changed, 176 insertions(+), 56 deletions(-) diff --git a/application/index/library/express/Trace.php b/application/index/library/express/Trace.php index 30e7260..085ded5 100644 --- a/application/index/library/express/Trace.php +++ b/application/index/library/express/Trace.php @@ -129,7 +129,7 @@ class Trace ], 0); continue; } - $key = $item['postnum']; + $key = $item['postnum'] . '|' . $item['phone']; if ($opts['dedupe'] && isset($taskIndex[$key])) { $tasks[$taskIndex[$key]]['indexes'][] = $i; continue; @@ -138,6 +138,7 @@ class Trace $tasks[] = [ 'postnum' => $item['postnum'], 'postcode' => $item['postcode'], + 'phone' => $item['phone'], 'indexes' => [$i], ]; } @@ -200,6 +201,7 @@ class Trace { $postnum = ''; $postcode = ''; + $phone = ''; if (is_array($input)) { if (isset($input['postnum'])) { $postnum = $input['postnum']; @@ -209,8 +211,16 @@ class Trace if (isset($input['postcode'])) { $postcode = $input['postcode']; } + if (isset($input['phone'])) { + $phone = $input['phone']; + } } else { - $postnum = $input; + // 兼容『单号|手机号后四位』写法 + $parts = explode('|', (string)$input); + $postnum = $parts[0]; + if (isset($parts[1])) { + $phone = $parts[1]; + } } $recognized = Recognizer::recognize($postnum); @@ -221,13 +231,37 @@ class Trace return [ 'postnum' => $recognized['postnum'], 'postcode' => $postcode, + 'phone' => self::normalizePhone($phone), 'recognized' => $recognized, ]; } + /** + * 手机号后缀归一:只保留数字,超过 4 位取后 4 位 + * + * @param string $phone + * @return string 4 位数字或空串 + */ + protected static function normalizePhone($phone) + { + $digits = preg_replace('/[^0-9]/', '', trim((string)$phone)); + if ($digits === '') { + return ''; + } + return strlen($digits) > 4 ? substr($digits, -4) : $digits; + } + + /** + * 组装上游查询串:单号|单号|公司代码[|手机号后四位] + * 未提供手机号时与原格式完全一致 + */ protected function buildQueryString(array $task) { - return $task['postnum'] . '|' . $task['postnum'] . '|' . $task['postcode']; + $query = $task['postnum'] . '|' . $task['postnum'] . '|' . $task['postcode']; + if (!empty($task['phone'])) { + $query .= '|' . $task['phone']; + } + return $query; } /** diff --git a/application/index/library/new_wms/notify/WaybillCodes.php b/application/index/library/new_wms/notify/WaybillCodes.php index cbfcba0..448bca3 100644 --- a/application/index/library/new_wms/notify/WaybillCodes.php +++ b/application/index/library/new_wms/notify/WaybillCodes.php @@ -6,21 +6,101 @@ namespace app\index\library\new_wms\notify; * 入站回调运单号参数解析 * * waybill.* 系列方法的请求体统一为 { request: { waybill_codes: [...] } }, - * 这里统一做「兼容平铺结构 + 去空 + 去重保序 + 限量」,供各处理器复用。 + * 这里统一做「兼容平铺结构 + 拆分收件人手机号后四位 + 去空 + 去重保序 + 限量」,供各处理器复用。 + * + * 单号可带「手机号后四位」后缀(部分快递查询轨迹需要核验收件人): + * {"request":{"waybill_codes":["776447927635244|8000","770358128188071"]}} + * 拆分后:776447927635244 => phone=8000;770358128188071 => phone=''(不带后缀,保持原格式) */ class WaybillCodes { /** 默认单次最多处理的运单号数量 */ const DEFAULT_MAX = 50; + /** 手机号后缀位数 */ + const PHONE_SUFFIX_LENGTH = 4; + /** + * 解析成 [{waybill_code, phone}, ...],按运单号去重保序 + * * @param array $body 请求体 * @param int $max 最多返回条数 - * @return array 去重保序后的运单号列表 + * @return array + */ + public static function parse(array $body, $max = self::DEFAULT_MAX) + { + $items = []; + foreach (self::rawList($body) as $item) { + if (is_array($item) || is_object($item)) { + continue; + } + $parsed = self::split($item); + $code = $parsed['waybill_code']; + if ($code === '' || isset($items[$code])) { + continue; + } + $items[$code] = $parsed; + if (count($items) >= $max) { + break; + } + } + + return array_values($items); + } + + /** + * 只取运单号列表(按运单号去重保序) + * + * @param array $body + * @param int $max + * @return array */ public static function extract(array $body, $max = self::DEFAULT_MAX) { - $raw = []; + return array_column(self::parse($body, $max), 'waybill_code'); + } + + /** + * 拆分「单号|手机号后四位」 + * + * @param string $text + * @return array ['waybill_code' => string, 'phone' => string] + */ + public static function split($text) + { + $parts = explode('|', (string)$text); + return [ + 'waybill_code' => trim($parts[0]), + 'phone' => isset($parts[1]) ? self::normalizePhone($parts[1]) : '', + ]; + } + + /** + * 手机号后缀归一:只保留数字,超过 4 位时取后 4 位(兼容误传完整手机号) + * + * @param string $phone + * @return string 4 位数字或空串 + */ + public static function normalizePhone($phone) + { + $digits = preg_replace('/[^0-9]/', '', trim((string)$phone)); + if ($digits === '') { + return ''; + } + if (strlen($digits) > self::PHONE_SUFFIX_LENGTH) { + $digits = substr($digits, -self::PHONE_SUFFIX_LENGTH); + } + return $digits; + } + + /** + * 取出请求体里的原始运单号数组,兼容 request.waybill_codes / 平铺 waybill_codes 等写法 + * + * @param array $body + * @return array + */ + protected static function rawList(array $body) + { if (isset($body['request']['waybill_codes'])) { $raw = $body['request']['waybill_codes']; } elseif (isset($body['request']['waybillCodes'])) { @@ -29,27 +109,10 @@ class WaybillCodes $raw = $body['waybill_codes']; } elseif (isset($body['waybillCodes'])) { $raw = $body['waybillCodes']; + } else { + $raw = []; } - 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) >= $max) { - break; - } - } - - return array_values($codes); + return is_array($raw) ? $raw : [$raw]; } } diff --git a/application/index/library/new_wms/notify/WaybillReceiveHandler.php b/application/index/library/new_wms/notify/WaybillReceiveHandler.php index fdca40b..6fb06cf 100644 --- a/application/index/library/new_wms/notify/WaybillReceiveHandler.php +++ b/application/index/library/new_wms/notify/WaybillReceiveHandler.php @@ -11,7 +11,8 @@ use app\index\library\express\Trace; * 单批最多 10 单,签名模式 simple:md5(appSecret + timestamp + bodyJson)。 * * 请求体: - * {"request":{"waybill_codes":["773361284774644","321457247679904"]}} + * {"request":{"waybill_codes":["773361284774644|8000","321457247679904"]}} + * 单号可带「收件人手机号后四位」后缀,不带则按原格式查询。 * 响应体(裸数组,不套 {code,msg,data} 外壳): * [{"waybill_code":"773361284774644","is_galaxy_receive":1}, * {"waybill_code":"321457247679904","is_galaxy_receive":0}] @@ -48,59 +49,62 @@ class WaybillReceiveHandler */ public function handle(array $body, array $query) { - $codes = $this->extractCodes($body); - if (empty($codes)) { + $items = $this->parseCodes($body); + if (empty($items)) { return new RawResponse([]); } - $received = $this->resolve($codes); + $received = $this->resolve($items); $rows = []; - foreach ($codes as $code) { + foreach ($items as $item) { + $code = $item['waybill_code']; $rows[] = [ 'waybill_code' => $code, 'is_galaxy_receive' => !empty($received[$code]) ? 1 : 0, ]; } - \think\Log::info('[WmsNotify] waybill.getreceive 处理完成: 共 ' . count($codes) . ' 单,已揽收 ' . count(array_filter($received)) . ' 单'); + \think\Log::info('[WmsNotify] waybill.getreceive 处理完成: 共 ' . count($items) . ' 单,已揽收 ' . count(array_filter($received)) . ' 单'); return new RawResponse($rows); } /** - * 提取并清洗运单号(去空、去重、保序、限量) + * 解析运单号(去空、去重、保序、限量,并拆分『单号|手机号后四位』) * * @param array $body - * @return array + * @return array [{waybill_code, phone}, ...] */ - protected function extractCodes(array $body) + protected function parseCodes(array $body) { - return WaybillCodes::extract($body, self::MAX_CODES); + return WaybillCodes::parse($body, self::MAX_CODES); } /** * 逐单判定是否已揽收:缓存优先,其余并发查询轨迹接口 * - * @param array $codes + * @param array $items [{waybill_code, phone}, ...] * @return array [运单号 => 1|0] */ - protected function resolve(array $codes) + protected function resolve(array $items) { - $received = array_fill_keys($codes, 0); + $received = []; $redis = $this->connectRedis(); $pending = []; - foreach ($codes as $code) { + foreach ($items as $item) { + $code = $item['waybill_code']; + $received[$code] = 0; if ($redis !== null && $this->cacheHit($redis, $code)) { $received[$code] = 1; continue; } - $pending[] = $code; + $pending[] = $item; } if (empty($pending)) { - \think\Log::info('[WmsNotify] waybill.getreceive 命中缓存,跳过轨迹接口: ' . count($codes) . ' 单'); + \think\Log::info('[WmsNotify] waybill.getreceive 命中缓存,跳过轨迹接口: ' . count($items) . ' 单'); return $received; } @@ -116,16 +120,23 @@ class WaybillReceiveHandler if (!empty($traceBaseUrl)) { $traceOptions['base_url'] = $traceBaseUrl; } + $inputs = []; + foreach ($pending as $item) { + $inputs[] = [ + 'postnum' => $item['waybill_code'], + 'phone' => $item['phone'], + ]; + } $trace = new Trace($traceOptions); - $list = $trace->query($pending); + $list = $trace->query($inputs); } catch (\Throwable $e) { \think\Log::error('[WmsNotify] waybill.getreceive 轨迹查询异常: ' . $e->getMessage()); return $received; } - foreach ($list as $index => $item) { - $code = isset($pending[$index]) ? $pending[$index] : ''; - if ($code === '' || empty($item['ok']) || empty($item['traces'])) { + foreach ($list as $index => $result) { + $code = isset($pending[$index]['waybill_code']) ? $pending[$index]['waybill_code'] : ''; + if ($code === '' || empty($result['ok']) || empty($result['traces'])) { continue; } $received[$code] = 1; diff --git a/application/index/library/new_wms/notify/WaybillTraceHandler.php b/application/index/library/new_wms/notify/WaybillTraceHandler.php index 5b74597..b5f1e89 100644 --- a/application/index/library/new_wms/notify/WaybillTraceHandler.php +++ b/application/index/library/new_wms/notify/WaybillTraceHandler.php @@ -11,7 +11,8 @@ use app\index\library\express\Trace; * 签名模式 simple:md5(appSecret + timestamp + bodyJson)。 * * 请求体: - * {"request":{"waybill_codes":["770358128188071","319415577176711"]}} + * {"request":{"waybill_codes":["770358128188071|8000","319415577176711"]}} + * 单号可带「收件人手机号后四位」后缀,不带则按原格式查询。 * 响应体(裸数组,不套 {code,msg,data} 外壳): * [ * {"waybillNo":"770358128188071","trace":[ @@ -48,7 +49,7 @@ class WaybillTraceHandler return new RawResponse([]); } - $traceMap = $this->queryTraces($codes); + $traceMap = $this->queryTraces(WaybillCodes::parse($body, self::MAX_CODES)); $rows = []; $count = 0; @@ -69,12 +70,23 @@ class WaybillTraceHandler /** * 批量查询轨迹(10 单并发) * - * @param array $codes + * @param array $items [{waybill_code, phone}, ...] * @return array [运单号 => 轨迹数组] */ - protected function queryTraces(array $codes) + protected function queryTraces(array $items) { - $map = array_fill_keys($codes, []); + $map = []; + $inputs = []; + foreach ($items as $item) { + $map[$item['waybill_code']] = []; + $inputs[] = [ + 'postnum' => $item['waybill_code'], + 'phone' => $item['phone'], + ]; + } + if (empty($inputs)) { + return $map; + } try { $traceOptions = [ @@ -88,18 +100,18 @@ class WaybillTraceHandler $traceOptions['base_url'] = $traceBaseUrl; } $trace = new Trace($traceOptions); - $list = $trace->query($codes); + $list = $trace->query($inputs); } 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'])) { + foreach ($list as $index => $result) { + $code = isset($inputs[$index]['postnum']) ? $inputs[$index]['postnum'] : ''; + if ($code === '' || empty($result['ok']) || empty($result['raw'])) { continue; } - $map[$code] = $this->flattenTraces($item['raw']); + $map[$code] = $this->flattenTraces($result['raw']); } return $map;