liufumingren 4e5c05dfc3 feat(wms): 新增运单揽收状态查询回调 waybill.getreceive
- 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>
2026-11-11 03:22:21 +08:00

157 lines
5.1 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;
use app\index\library\new_wms\WmsVerify;
/**
* WMS 入站通知分发器
*
* 统一处理外部 → 本系统的回调/通知请求。
*
* 流程:
* 1. 验签(WmsVerify)
* 2. 解析 method 参数
* 3. 查找已注册的处理器
* 4. 调用处理器,返回统一格式响应
*
* 与 func/ 目录的区别:
* - func/ 是『出方向』:本系统 → WMS 仓库 API
* - notify/ 是『入方向』:外部系统 → 本系统(当前文件)
*
* 使用示例:
* Dispatcher::register('order.status_change', function($body, $query) {
* // 处理订单状态变更通知
* return ['code' => 1, 'msg' => 'ok'];
* });
*
* $dispatcher = new Dispatcher();
* $result = $dispatcher->handle($_GET, file_get_contents('php://input'));
*/
class Dispatcher
{
/**
* 已注册的方法处理器
* method => callable($body, $query): array
* @var array
*/
private static $handlers = [];
/**
* 验签器
* @var WmsVerify
*/
private $verifier;
public function __construct()
{
$appSecret = config('wms.app_secret') ?: '';
$this->verifier = new WmsVerify($appSecret);
// 注册内置处理器
if (!isset(self::$handlers['notify.ping'])) {
self::register('notify.ping', [new PingHandler(), 'handle']);
}
if (!isset(self::$handlers['notify.reply'])) {
self::register('notify.reply', [new ReplyHandler(), 'handle']);
}
if (!isset(self::$handlers['waybill.getreceive'])) {
self::register('waybill.getreceive', [new WaybillReceiveHandler(), 'handle']);
}
}
// ==================== 处理器注册 ====================
/**
* 注册一个方法处理器
*
* @param string $method 方法名(如 'order.status_change')
* @param callable $handler 处理器 function(array $body, array $query): array
*/
public static function register($method, callable $handler)
{
self::$handlers[$method] = $handler;
}
/**
* 批量注册处理器
*
* @param array $map ['method' => callable, ...]
*/
public static function registerAll(array $map)
{
foreach ($map as $method => $handler) {
self::register($method, $handler);
}
}
/**
* 获取已注册的方法列表
* @return array
*/
public static function registeredMethods()
{
return array_keys(self::$handlers);
}
// ==================== 请求处理 ====================
/**
* 处理入站请求
*
* @param array $queryParams URL query 参数($_GET)
* @param string $rawBody 原始请求体(php://input)
* @return array|RawResponse 默认 ['code' => int, 'msg' => string, 'data' => mixed];
* 处理器返回 RawResponse 时原样透出(裸数组响应,如 waybill.getreceive)
*/
public function handle(array $queryParams, $rawBody)
{
// ---- 1. 解析请求体 ----
$body = [];
if ($rawBody !== '' && $rawBody !== null) {
$body = json_decode($rawBody, true);
if ($body === null && json_last_error() !== JSON_ERROR_NONE) {
return ['code' => 400, 'msg' => '请求体 JSON 解析失败', 'data' => null];
}
}
// ---- 2. 验签(入站算法:MD5(secret + timestamp + bodyJson))----
if (!$this->verifier->verifyNotify($queryParams, $rawBody)) {
\think\Log::error('[WmsNotify] 验签失败: ' . json_encode([
'query' => $queryParams,
'body' => $rawBody,
], JSON_UNESCAPED_UNICODE));
return ['code' => 401, 'msg' => '签名验证失败', 'data' => null];
}
// ---- 3. 解析 method ----
$method = isset($queryParams['method']) ? $queryParams['method'] : '';
if (empty($method)) {
return ['code' => 400, 'msg' => '缺少 method 参数', 'data' => null];
}
\think\Log::info("[WmsNotify] {$method} 入站: " . json_encode([
'query' => $queryParams,
'body' => $body,
], JSON_UNESCAPED_UNICODE));
// ---- 4. 查找处理器 ----
if (!isset(self::$handlers[$method])) {
\think\Log::error("[WmsNotify] {$method} 未注册处理器,已注册: " . implode(', ', self::registeredMethods()));
return ['code' => 404, 'msg' => "方法 {$method} 未注册", 'data' => null];
}
// ---- 5. 调用处理器 ----
try {
$result = call_user_func(self::$handlers[$method], $body, $queryParams);
if ($result instanceof RawResponse) {
return $result;
}
return is_array($result) ? $result : ['code' => 1, 'msg' => 'ok', 'data' => $result];
} catch (\Throwable $e) {
\think\Log::error("[WmsNotify] {$method} 处理器异常: " . $e->getMessage());
return ['code' => 500, 'msg' => '处理器异常: ' . $e->getMessage(), 'data' => null];
}
}
}