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