liufumingren c00708f58c feat: 入站 notify 对接完成(ping/reply + 入站签名算法)
- 新增 notify/PingHandler.php(notify.ping 连通性测试)
- 新增 notify/ReplyHandler.php(notify.reply 消息回复,Redis 幂等 + 推送到 IM)
- Dispatcher 自动注册内置处理器,入站签名改为 MD5(secret+timestamp+bodyJson)
- WmsSign 新增 getAppSecret() 供验签使用
- ReplyHandler 通过 ext_data.source 判断消息来源(wx/qiwe)
2026-08-07 22:52:46 +08:00

150 lines
4.7 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']);
}
}
// ==================== 处理器注册 ====================
/**
* 注册一个方法处理器
*
* @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 ['code' => int, 'msg' => string, 'data' => mixed]
*/
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);
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];
}
}
}