diff --git a/application/index/controller/wms/Wms.php b/application/index/controller/wms/Wms.php index f227d06..a8efdcd 100644 --- a/application/index/controller/wms/Wms.php +++ b/application/index/controller/wms/Wms.php @@ -11725,4 +11725,29 @@ class Wms extends Frontend $this->sendMsg($msgData, $room['room_id'], $cacheType); Log::error("通知到货主群".json_encode($msgData['primary'])); } + + // ==================== WMS 入站通知入口 ==================== + + /** + * new_wms_notify — 统一外部入站接口 + * + * 接收 WMS 仓库 API 的回调/通知,经过签名校验后自动分发到已注册的处理器。 + * 与 func/ 出方向方法独立,互不干扰。 + * + * 入口: POST /index/wms/wms/new_wms_notify?method=xxx&... + */ + public function new_wms_notify() + { + $queryParams = $this->request->get(); + $rawBody = file_get_contents('php://input'); + + \think\Log::error("[new_wms_notify] 入参\nQuery: " . json_encode($queryParams, JSON_UNESCAPED_UNICODE) . "\nBody: {$rawBody}"); + + $dispatcher = new \app\index\library\new_wms\notify\Dispatcher(); + $result = $dispatcher->handle($queryParams, $rawBody); + + \think\Log::error("[new_wms_notify] 出参: " . json_encode($result, JSON_UNESCAPED_UNICODE)); + + return json($result); + } } diff --git a/application/index/library/new_wms/notify/Dispatcher.php b/application/index/library/new_wms/notify/Dispatcher.php new file mode 100644 index 0000000..bd23b0b --- /dev/null +++ b/application/index/library/new_wms/notify/Dispatcher.php @@ -0,0 +1,141 @@ + 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); + } + + // ==================== 处理器注册 ==================== + + /** + * 注册一个方法处理器 + * + * @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. 验签 ---- + if (!$this->verifier->verify($queryParams, $body)) { + \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]; + } + } +}