liufumingren d953976391 feat(wms): waybill 入参支持「单号|手机号后四位」,查询轨迹时透传
- WaybillCodes 支持拆分单号与手机号后四位(去空格/非数字,超长取后四位),
  按单号去重保序,无后缀时行为与原来完全一致
- express/Trace 支持 phone:buildQueryString 在有 phone 时追加第 4 段
  (单号|单号|公司代码|后四位),无 phone 时保持原格式;去重键改为 单号|phone
- waybill.getTrace / waybill.getreceive 均透传 phone,响应中单号仍不含手机号

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-12 03:54:52 +08:00

119 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
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
*/
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)
{
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'])) {
$raw = $body['request']['waybillCodes'];
} elseif (isset($body['waybill_codes'])) {
$raw = $body['waybill_codes'];
} elseif (isset($body['waybillCodes'])) {
$raw = $body['waybillCodes'];
} else {
$raw = [];
}
return is_array($raw) ? $raw : [$raw];
}
}