From 248b8876be13bc1364195a9b59e599daf9f176b6 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 6 Aug 2026 18:06:02 +0800 Subject: [PATCH] 1 --- application/index/controller/wms/Chat.php | 2 +- application/index/library/new_wms/Forward.php | 123 ++++++++++++++++++ 2 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 application/index/library/new_wms/Forward.php diff --git a/application/index/controller/wms/Chat.php b/application/index/controller/wms/Chat.php index 0de1c19..3c92a43 100644 --- a/application/index/controller/wms/Chat.php +++ b/application/index/controller/wms/Chat.php @@ -22,7 +22,7 @@ use app\index\library\zopsdk\ZopRequest; use app\index\controller\wms\Wms as WmsController; use fast\Http; use fast\Random; -use app\index\library\chat\Forward; +use app\index\library\new_wms\Forward; class Chat extends Frontend { protected $noNeedLogin = 'OnMessage,OnMessage2,Message,Notify,Notify5,trace_test,tihuotest,Notify2,Notify3,kdroomMessageExplpde2,test,checkOnline,getAllWechatRooms,noticeConfirmApplyStorage'; diff --git a/application/index/library/new_wms/Forward.php b/application/index/library/new_wms/Forward.php new file mode 100644 index 0000000..77ac9dc --- /dev/null +++ b/application/index/library/new_wms/Forward.php @@ -0,0 +1,123 @@ +handle($params, $SOURCE, $text); + * if ($result !== null) return $result; // 跳过本地处理 + */ +class Forward +{ + // ==================== WMS 开放接口配置 ==================== + const WMS_API_BASE_URL = 'https://wms.wms.gztbyc.com/warehouse'; // WMS 开放接口地址 + const WMS_APP_KEY = ''; // 应用 Key(待配置) + const WMS_APP_SECRET = ''; // 应用 Secret(待配置) + const WMS_API_TIMEOUT = 10; // 请求超时(秒) + + /** + * 处理消息转发 + * + * @param array $params 统一消息结构(与 processMessage 入参一致) + * @param string $source 消息来源: 'wx' | 'qiwe' + * @param string $text 已提取的消息文本内容 + * @return array|null 返回 null 表示不处理(继续本地逻辑), + * 返回 ["msgs" => ..., "info" => ...] 则跳过本地处理直接返回 + */ + public function handle($params, $source, $text) + { + // ---- 配置未就绪时跳过 ---- + if (empty(static::WMS_APP_KEY) || empty(static::WMS_APP_SECRET)) { + return null; + } + + // ---- 构建请求签名 ---- + $timestamp = (string) time(); + $nonce = $this->generateNonce(16); + $sign = $this->buildSign($timestamp, $nonce); + + // ---- 构建请求体 ---- + $postData = [ + 'fromid' => $params['data']['fromid'] ?? '', + 'memid' => $params['data']['memid'] ?? '', + 'memname' => $params['data']['memname'] ?? '', + 'nickName' => $params['data']['nickName'] ?? '', + 'msg' => $text, + 'source' => $source, + 'type' => $params['type'] ?? 1, + 'msgId' => $params['data']['id'] ?? '', + ]; + + // ---- 发起请求 ---- + try { + $response = Http::post( + static::WMS_API_BASE_URL . '/chat/handle', + json_encode($postData), + [ + CURLOPT_HTTPHEADER => [ + 'Content-Type: application/json', + 'X-App-Key: ' . static::WMS_APP_KEY, + 'X-Timestamp: ' . $timestamp, + 'X-Nonce: ' . $nonce, + 'X-Sign: ' . $sign, + ], + CURLOPT_TIMEOUT => static::WMS_API_TIMEOUT, + CURLOPT_CONNECTTIMEOUT => 5, + ] + ); + + $result = json_decode($response, true); + + // ---- 校验响应 ---- + if (!$result || empty($result['code'])) { + \think\Log::error('[Forward] WMS 接口返回异常: ' . json_encode($response)); + return null; // 异常时降级到本地处理 + } + + // code == 1 表示 WMS 已处理,返回给群聊 + if ((int) $result['code'] === 1 && !empty($result['data'])) { + \think\Log::info('[Forward] WMS 已处理消息: ' . json_encode($result)); + return [ + 'msgs' => $result['data']['msgs'] ?? [], + 'info' => $result['data']['info'] ?? '已转发WMS处理', + ]; + } + + // code == 0 或 data 为空 → 走本地逻辑 + return null; + + } catch (\Exception $e) { + \think\Log::error('[Forward] 请求异常: ' . $e->getMessage()); + return null; // 异常时降级到本地处理 + } + } + + /** + * 构建请求签名 + */ + private function buildSign($timestamp, $nonce) + { + return md5(static::WMS_APP_KEY . $timestamp . $nonce . static::WMS_APP_SECRET); + } + + /** + * 生成随机 nonce + */ + private function generateNonce($length = 16) + { + $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; + $nonce = ''; + for ($i = 0; $i < $length; $i++) { + $nonce .= $chars[random_int(0, strlen($chars) - 1)]; + } + return $nonce; + } +}