From e177a4771f203dc949faf6e1ed8a4196b96463e2 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Tue, 18 Aug 2026 00:55:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20IM=E9=80=9A=E9=81=93=E5=9C=A8=E7=BA=BF?= =?UTF-8?q?=E6=A3=80=E6=B5=8B=EF=BC=88qiwe=E6=96=B0=E5=A2=9EgetProfile/che?= =?UTF-8?q?ckOnline=EF=BC=8Cnew=5Fwms=5Fnotify=20reply=E7=A6=BB=E7=BA=BF?= =?UTF-8?q?=E8=BF=94=E5=9B=9E503=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/index/controller/qiwe/Index.php | 41 ++++++++ application/index/controller/wms/Wms.php | 6 +- .../library/new_wms/notify/OnlineChecker.php | 96 +++++++++++++++++++ .../library/new_wms/notify/ReplyHandler.php | 13 ++- application/index/library/qiwe/Apis.php | 10 ++ 5 files changed, 163 insertions(+), 3 deletions(-) create mode 100644 application/index/library/new_wms/notify/OnlineChecker.php diff --git a/application/index/controller/qiwe/Index.php b/application/index/controller/qiwe/Index.php index 0e61b8a..89cb504 100644 --- a/application/index/controller/qiwe/Index.php +++ b/application/index/controller/qiwe/Index.php @@ -1285,4 +1285,45 @@ class Index extends Frontend ] ]); } + + /** + * 检查企微是否在线 + * 调用 /user/getProfile 接口,能成功获取账号信息即视为在线 + */ + public function checkOnline() + { + $profile = $this->getProfileData(); + \think\Log::error('checkOnline请求结果: ' . json_encode($profile)); + + if ($profile !== null) { + return json([ + 'code' => 0, + 'msg' => 'success', + 'is_online' => true, + 'profile' => $profile, + ]); + } + + return json([ + 'code' => 1, + 'msg' => 'offline', + 'is_online' => false, + ]); + } + + /** + * 获取账号信息 + * @return array|null 成功返回 profile 数组,失败返回 null + */ + private function getProfileData() + { + $result = $this->query->query("getProfile", [$this->guid]); + $data = json_decode($result, true); + + if ($data && ($data['code'] ?? 1) == 0) { + return $data['data'] ?? []; + } + + return null; + } } \ No newline at end of file diff --git a/application/index/controller/wms/Wms.php b/application/index/controller/wms/Wms.php index d3017c5..f520768 100644 --- a/application/index/controller/wms/Wms.php +++ b/application/index/controller/wms/Wms.php @@ -7856,7 +7856,7 @@ class Wms extends Frontend } if (empty($decoded['method_name']) || empty($decoded['data'])) { $decoded['method_name'] = "tuijian"; - // throw new \Exception("Missing method_name or data"); + //throw new \Exception("Missing method_name or data"); } $method_name = $decoded['method_name']; @@ -11749,6 +11749,8 @@ class Wms extends Frontend \think\Log::error("[new_wms_notify] 出参: " . json_encode($result, JSON_UNESCAPED_UNICODE)); - return json($result); + // 业务 code >= 400 时,HTTP 状态码同步为非200,调用方视为失败并重试 + $httpCode = (is_array($result) && isset($result['code']) && $result['code'] >= 400) ? $result['code'] : 200; + return json($result, $httpCode); } } diff --git a/application/index/library/new_wms/notify/OnlineChecker.php b/application/index/library/new_wms/notify/OnlineChecker.php new file mode 100644 index 0000000..02b40aa --- /dev/null +++ b/application/index/library/new_wms/notify/OnlineChecker.php @@ -0,0 +1,96 @@ +wechats 保持一致) + * 如需扩展多台服务器,在下面追加即可。 + */ + private static $wxServers = [ + [ + 'wechatServer' => '42.194.235.215:8203', + 'key' => '8DE6CAD0E48D4DF84CF0593AD01DB62FE89D137B', + ], + ]; + + /** + * 检测 wx 是否在线 + * @return bool + */ + public static function wx() + { + foreach (self::$wxServers as $server) { + $list = self::queryWx(['method' => 'list'], $server); + if (is_array($list)) { + foreach ($list as $item) { + if (is_array($item) && ($item['isLogin'] ?? 0) == 1) { + return true; + } + } + } + } + return false; + } + + /** + * 检测 qiwe 是否在线 + * @return bool + */ + public static function qiwe() + { + try { + $tokenId = config('site.tokenId'); + $guid = config('site.guid'); + if (!$tokenId || !$guid) { + \think\Log::error('[OnlineChecker] 缺少企微 tokenId/guid 配置'); + return false; + } + $query = new \app\index\library\qiwe\Query(['tokenId' => $tokenId]); + $result = $query->query('getProfile', [$guid]); + $data = json_decode($result, true); + return is_array($data) && ($data['code'] ?? 1) == 0; + } catch (\Exception $e) { + \think\Log::error('[OnlineChecker] qiwe 检测异常: ' . $e->getMessage()); + return false; + } + } + + /** + * 调用 wx 服务器接口 + * @param array $params 请求参数 + * @param array $server 服务器配置 [wechatServer, key] + * @return array|null 成功返回 data 数组,失败返回 null + */ + private static function queryWx($params, $server) + { + try { + $header = ['Content-Type: application/x-www-form-urlencoded; charset=UTF-8']; + $options = [ + CURLOPT_HTTPHEADER => $header, + ]; + $url = "http://{$server['wechatServer']}/api?json&key={$server['key']}"; + $res = Http::sendRequest($url, http_build_query($params), 'POST', $options); + if (!$res['ret']) { + return null; + } + $data = json_decode($res['msg'], true); + return is_array($data) ? ($data['data'] ?? null) : null; + } catch (\Exception $e) { + \think\Log::error('[OnlineChecker] wx 检测异常: ' . $e->getMessage()); + return null; + } + } +} diff --git a/application/index/library/new_wms/notify/ReplyHandler.php b/application/index/library/new_wms/notify/ReplyHandler.php index 8031443..060f278 100644 --- a/application/index/library/new_wms/notify/ReplyHandler.php +++ b/application/index/library/new_wms/notify/ReplyHandler.php @@ -34,6 +34,18 @@ class ReplyHandler \think\Log::error("[WmsNotify] reply 入站: type={$type} ext_id={$extId} group={$groupId} user={$userId} content={$content}"); + // ---- 根据 ext_data.source 判断消息来源 ---- + $source = is_array($extData) ? ($extData['source'] ?? 'wx') : 'wx'; + + // ---- 检测对应 IM 通道是否在线(放在去重之前,离线时不写去重键,WMS 重试会重新检测) ---- + if ($groupId && $content) { + $isOnline = ($source === 'qiwe') ? OnlineChecker::qiwe() : OnlineChecker::wx(); + if (!$isOnline) { + \think\Log::error("[WmsNotify] reply 被拒绝: {$source} 不在线"); + return ['code' => 503, 'msg' => "{$source} offline", 'data' => null]; + } + } + // ---- 幂等去重 ---- if ($extId) { try { @@ -54,7 +66,6 @@ class ReplyHandler // ---- 根据 ext_data.source 判断消息来源,发送回复 ---- if ($groupId && $content) { - $source = is_array($extData) ? ($extData['source'] ?? 'wx') : 'wx'; $this->sendToGroup($groupId, $content, $userId, $source); } diff --git a/application/index/library/qiwe/Apis.php b/application/index/library/qiwe/Apis.php index 37dd783..0f823e8 100644 --- a/application/index/library/qiwe/Apis.php +++ b/application/index/library/qiwe/Apis.php @@ -249,4 +249,14 @@ class Apis ] ]; } + //获取账号信息(调用成功即可视为账号在线) + public static function getProfile($args){ + [$guid] = $args; + return [ + "method"=>"/user/getProfile", + "params"=>[ + "guid"=>$guid + ] + ]; + } } \ No newline at end of file