- express/Recognizer:按字母前缀 + 位数号段识别运单快递公司(p01~p35)
- express/Trace:批量并发查轨迹(curl_multi,默认 10 并发),解析状态/首末轨迹/中转签收时效
- notify/WaybillReceiveHandler:外部传入运单号自动识别公司后查轨迹,轨迹非空即已揽收,
已揽收结果缓存 24h(命中不再请求接口),未揽收/查不到/识别不出/接口异常一律返回 0
- notify/RawResponse:支持裸数组响应透出,不套 {code,msg,data} 外壳
- new_wms_notify 输出 [{"waybill_code","is_galaxy_receive"}]
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
195 lines
6.5 KiB
PHP
195 lines
6.5 KiB
PHP
<?php
|
||
|
||
namespace app\index\library\express;
|
||
|
||
/**
|
||
* 运单号快递公司识别
|
||
*
|
||
* 外部传入的运单号通常不带快递公司,而查询轨迹接口需要 postcode(p01~p35)。
|
||
* 这里按「字母前缀 → 位数 → 号段前缀」的顺序做本地规则识别,不产生任何网络请求。
|
||
*
|
||
* 说明:
|
||
* - 识别只做启发式判断,返回 confidence 供调用方决定是否采用;
|
||
* - 纯数字单号只按「位数 + 前 1~2 位」判定,同位数不同公司的号段可能重叠,属于已知局限;
|
||
* - 需要新增规则时用 Recognizer::addPattern() / Recognizer::addDigitRule(),或直接改 $patterns / $digitRules。
|
||
*
|
||
* 用法:
|
||
* $r = Recognizer::recognize('771234567890123');
|
||
* // ['postnum'=>'771234567890123','postcode'=>'p02','company'=>'申通',
|
||
* // 'confidence'=>'medium','matched_by'=>'prefix','length'=>15]
|
||
*/
|
||
class Recognizer
|
||
{
|
||
/**
|
||
* 带字母前缀的单号:命中即可直接判定,置信度 high
|
||
* @var array [正则, postcode, 置信度]
|
||
*/
|
||
protected static $patterns = [
|
||
['/^SF\d{10,16}$/', 'p05', 'high'], // 顺丰 SF+数字
|
||
['/^YT\d{10,16}$/', 'p01', 'high'], // 圆通 YT+数字
|
||
['/^JT\d{10,16}$/', 'p32', 'high'], // 极兔 JT+数字
|
||
['/^JD[A-Z0-9]{8,20}$/', 'p10', 'high'], // 京东 JDVA/JDX...
|
||
['/^DPK\d{8,16}$/', 'p26', 'high'], // 德邦 DPK+数字
|
||
['/^[A-Z]{2}\d{9}CN$/', 'p07', 'high'], // EMS EA123456789CN
|
||
];
|
||
|
||
/**
|
||
* 纯数字单号:按位数 + 号段前缀判定
|
||
* @var array [位数, 前缀数组, postcode, 置信度]
|
||
*/
|
||
protected static $digitRules = [
|
||
[15, ['77', '88', '66'], 'p02', 'medium'], // 申通 15 位
|
||
[15, ['10', '12', '19', '31', '32', '42', '43', '46', '50', '51', '52', '53', '57', '58', '59'], 'p03', 'medium'], // 韵达 15 位
|
||
[14, ['7', '8', '9'], 'p04', 'medium'], // 中通 14 位
|
||
[12, ['66', '68', '71', '72', '73', '76', '77', '78', '86', '88'], 'p01', 'low'], // 圆通 12 位
|
||
];
|
||
|
||
/** @var array postcode => 快递公司名,与 api/controller/Configs.php 的 $pcodes 保持一致 */
|
||
protected static $companies = [
|
||
'p01' => '圆通',
|
||
'p02' => '申通',
|
||
'p03' => '韵达',
|
||
'p04' => '中通',
|
||
'p05' => '顺丰',
|
||
'p06' => '百世快运',
|
||
'p07' => 'EMS',
|
||
'p08' => '京广速运',
|
||
'p09' => '百世极兔/百世快递',
|
||
'p10' => '京东',
|
||
'p16' => '安能',
|
||
'p17' => '优速',
|
||
'p18' => '自提',
|
||
'p19' => '零担',
|
||
'p20' => '整车',
|
||
'p21' => '运通速运',
|
||
'p22' => '邮政',
|
||
'p23' => '跨越',
|
||
'p24' => '韵达国际',
|
||
'p25' => '中通快运',
|
||
'p26' => '德邦快递/德邦物流',
|
||
'p27' => '壹米滴答',
|
||
'p30' => '其他',
|
||
'p32' => '极兔速递',
|
||
'p34' => '丰网速运',
|
||
'p35' => '邮政标快',
|
||
];
|
||
|
||
/**
|
||
* 清洗运单号:转大写并去掉空格、横杠、中文等非字母数字字符
|
||
* @param string $number
|
||
* @return string
|
||
*/
|
||
public static function clean($number)
|
||
{
|
||
return preg_replace('/[^A-Z0-9]/', '', strtoupper(trim((string)$number)));
|
||
}
|
||
|
||
/**
|
||
* 识别单个运单号
|
||
* @param string $number 运单号(可含空格、分隔符)
|
||
* @return array
|
||
*/
|
||
public static function recognize($number)
|
||
{
|
||
$standard = self::clean($number);
|
||
$length = strlen($standard);
|
||
|
||
if ($length === 0) {
|
||
return self::build($standard, 'UNKNOWN', 'none', 'none', $length);
|
||
}
|
||
|
||
foreach (self::$patterns as $rule) {
|
||
if (preg_match($rule[0], $standard)) {
|
||
return self::build($standard, $rule[1], $rule[2], 'pattern', $length);
|
||
}
|
||
}
|
||
|
||
if (ctype_digit($standard)) {
|
||
foreach (self::$digitRules as $rule) {
|
||
if ($rule[0] !== $length) {
|
||
continue;
|
||
}
|
||
foreach ($rule[1] as $prefix) {
|
||
if (strpos($standard, $prefix) === 0) {
|
||
return self::build($standard, $rule[2], $rule[3], 'prefix', $length);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
return self::build($standard, 'UNKNOWN', 'none', 'none', $length);
|
||
}
|
||
|
||
/**
|
||
* 批量识别
|
||
* @param array $numbers
|
||
* @return array 与输入下标一致的识别结果
|
||
*/
|
||
public static function batch(array $numbers)
|
||
{
|
||
$results = [];
|
||
foreach ($numbers as $key => $number) {
|
||
$results[$key] = self::recognize($number);
|
||
}
|
||
return $results;
|
||
}
|
||
|
||
/**
|
||
* 新增字母前缀规则(优先于内置规则)
|
||
* @param string $pattern 正则,如 '/^SF\d{10,16}$/'
|
||
* @param string $postcode
|
||
* @param string $confidence high|medium|low
|
||
*/
|
||
public static function addPattern($pattern, $postcode, $confidence = 'high')
|
||
{
|
||
array_unshift(self::$patterns, [$pattern, $postcode, $confidence]);
|
||
}
|
||
|
||
/**
|
||
* 新增纯数字号段规则(优先于内置规则)
|
||
* @param int $length 位数
|
||
* @param array|string $prefixes 号段前缀
|
||
* @param string $postcode
|
||
* @param string $confidence high|medium|low
|
||
*/
|
||
public static function addDigitRule($length, $prefixes, $postcode, $confidence = 'medium')
|
||
{
|
||
array_unshift(self::$digitRules, [(int)$length, (array)$prefixes, $postcode, $confidence]);
|
||
}
|
||
|
||
/**
|
||
* postcode 转公司名
|
||
* @param string $postcode
|
||
* @return string
|
||
*/
|
||
public static function companyName($postcode)
|
||
{
|
||
if (isset(self::$companies[$postcode])) {
|
||
return self::$companies[$postcode];
|
||
}
|
||
return ($postcode === 'UNKNOWN' || $postcode === '') ? '未知快递' : $postcode;
|
||
}
|
||
|
||
/**
|
||
* 是否识别成功
|
||
* @param array $result recognize() 的返回值
|
||
* @return bool
|
||
*/
|
||
public static function isRecognized(array $result)
|
||
{
|
||
return isset($result['postcode']) && $result['postcode'] !== 'UNKNOWN';
|
||
}
|
||
|
||
protected static function build($standard, $postcode, $confidence, $matchedBy, $length)
|
||
{
|
||
return [
|
||
'postnum' => $standard,
|
||
'postcode' => $postcode,
|
||
'company' => self::companyName($postcode),
|
||
'confidence' => $confidence,
|
||
'matched_by' => $matchedBy,
|
||
'length' => $length,
|
||
];
|
||
}
|
||
}
|