liufumingren 3d566595e0 feat: 入站通知接口 + 调试日志增强
- 新增 notify/Dispatcher.php 入站分发器(验签+method路由)
- Wms.php 新增 new_wms_notify 统一入站入口
- Forward.php 修正字段映射(groupId/userId 强转字符串,groupName/userName 修正)
- Base.php 调试日志全部改为 error 级别,打印完整请求/响应
- 移除 sourceType 入参
2026-08-07 21:47:34 +08:00

142 lines
4.4 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);
}
// ==================== 处理器注册 ====================
/**
* 注册一个方法处理器
*
* @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];
}
}
}