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]; } } }