diff --git a/application/index/library/new_wms/WmsSign.php b/application/index/library/new_wms/WmsSign.php index 1ee4301..0912671 100644 --- a/application/index/library/new_wms/WmsSign.php +++ b/application/index/library/new_wms/WmsSign.php @@ -33,6 +33,15 @@ class WmsSign $this->appSecret = $appSecret; } + /** + * 获取 appSecret(供外部验签使用) + * @return string + */ + public function getAppSecret() + { + return $this->appSecret; + } + // ==================== 公开方法 ==================== /** diff --git a/application/index/library/new_wms/WmsVerify.php b/application/index/library/new_wms/WmsVerify.php index d821b30..f3db835 100644 --- a/application/index/library/new_wms/WmsVerify.php +++ b/application/index/library/new_wms/WmsVerify.php @@ -94,6 +94,33 @@ class WmsVerify // ==================== 内部方法 ==================== + // ==================== 入站通知专用验签 ==================== + + /** + * 验证入站通知签名(算法不同:MD5(secret + timestamp + bodyJson)) + * + * @param array $queryParams URL query 参数(含 sign / timestamp) + * @param string $rawBody 原始请求体字符串 + * @return bool + */ + public function verifyNotify(array $queryParams, $rawBody) + { + $incomingSign = isset($queryParams['sign']) ? $queryParams['sign'] : ''; + $timestamp = isset($queryParams['timestamp']) ? $queryParams['timestamp'] : 0; + + if (empty($incomingSign) || empty($timestamp)) { + return false; + } + + // 入站签名: MD5(secret + timestamp + bodyJson) + $signText = $this->signer->getAppSecret() . $timestamp . $rawBody; + $expectedSign = md5($signText); + + return $this->hashEquals($expectedSign, $incomingSign); + } + + // ==================== 内部方法 ==================== + /** * 恒定时间字符串比较(防止时序攻击) * diff --git a/application/index/library/new_wms/notify/Dispatcher.php b/application/index/library/new_wms/notify/Dispatcher.php index bd23b0b..291c895 100644 --- a/application/index/library/new_wms/notify/Dispatcher.php +++ b/application/index/library/new_wms/notify/Dispatcher.php @@ -47,6 +47,14 @@ class Dispatcher { $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']); + } } // ==================== 处理器注册 ==================== @@ -103,8 +111,8 @@ class Dispatcher } } - // ---- 2. 验签 ---- - if (!$this->verifier->verify($queryParams, $body)) { + // ---- 2. 验签(入站算法:MD5(secret + timestamp + bodyJson))---- + if (!$this->verifier->verifyNotify($queryParams, $rawBody)) { \think\Log::error('[WmsNotify] 验签失败: ' . json_encode([ 'query' => $queryParams, 'body' => $rawBody, diff --git a/application/index/library/new_wms/notify/PingHandler.php b/application/index/library/new_wms/notify/PingHandler.php new file mode 100644 index 0000000..2c435c1 --- /dev/null +++ b/application/index/library/new_wms/notify/PingHandler.php @@ -0,0 +1,24 @@ + 200, 'msg' => 'ok', 'data' => null]; + } +} diff --git a/application/index/library/new_wms/notify/ReplyHandler.php b/application/index/library/new_wms/notify/ReplyHandler.php new file mode 100644 index 0000000..8031443 --- /dev/null +++ b/application/index/library/new_wms/notify/ReplyHandler.php @@ -0,0 +1,92 @@ +connect('127.0.0.1', 6379); + $redis->auth('qwe2019@'); + $redis->select(3); + $dedupeKey = "wms_reply:{$extId}"; + if ($redis->exists($dedupeKey)) { + \think\Log::info("[WmsNotify] reply 重复推送已忽略: ext_id={$extId}"); + return ['code' => 200, 'msg' => 'duplicate', 'data' => null]; + } + $redis->setex($dedupeKey, 3600, 1); + } catch (\Exception $e) { + // Redis 不可用时跳过去重,不阻塞 + } + } + + // ---- 根据 ext_data.source 判断消息来源,发送回复 ---- + if ($groupId && $content) { + $source = is_array($extData) ? ($extData['source'] ?? 'wx') : 'wx'; + $this->sendToGroup($groupId, $content, $userId, $source); + } + + return ['code' => 200, 'msg' => 'ok', 'data' => null]; + } + + /** + * 发送回复消息到 IM 群 + */ + private function sendToGroup($groupId, $content, $userId, $source) + { + try { + $msg = [ + 'method' => 'sendText', + 'wxid' => $groupId, + 'msg' => $content, + 'atid' => $userId ?: '', + 'pid' => 0, + ]; + + $redis = new \Redis(); + $redis->connect('127.0.0.1', 6379); + $redis->auth('qwe2019@'); + $redis->select(2); + + // 根据 ext_data.source 选择对应队列 + $msgKey = ($source === 'qiwe') ? 'QiweMessage' : 'Message'; + $redis->lpush($msgKey, json_encode($msg)); + + \think\Log::info("[WmsNotify] reply 已发送: source={$source} group={$groupId} key={$msgKey}"); + } catch (\Exception $e) { + \think\Log::error("[WmsNotify] reply 发送失败: " . $e->getMessage()); + } + } +}