feat: IM通道在线检测(qiwe新增getProfile/checkOnline,new_wms_notify reply离线返回503)
This commit is contained in:
parent
468b1ecfe9
commit
e177a4771f
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
96
application/index/library/new_wms/notify/OnlineChecker.php
Normal file
96
application/index/library/new_wms/notify/OnlineChecker.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace app\index\library\new_wms\notify;
|
||||
|
||||
use fast\Http;
|
||||
|
||||
/**
|
||||
* IM 通道在线检测
|
||||
*
|
||||
* wx : 查询微信服务器账号列表,有任一账号 isLogin==1 即视为在线
|
||||
* qiwe: 调用 /user/getProfile,能成功获取账号信息即视为在线
|
||||
*
|
||||
* 供 new_wms_notify 入站回复(reply)使用:对应通道不在线时,
|
||||
* 返回非200让 WMS 视为失败并重试。
|
||||
*/
|
||||
class OnlineChecker
|
||||
{
|
||||
/**
|
||||
* wx 服务器列表(与 application/index/controller/wms/Chat.php 的 $this->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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
||||
@ -249,4 +249,14 @@ class Apis
|
||||
]
|
||||
];
|
||||
}
|
||||
//获取账号信息(调用成功即可视为账号在线)
|
||||
public static function getProfile($args){
|
||||
[$guid] = $args;
|
||||
return [
|
||||
"method"=>"/user/getProfile",
|
||||
"params"=>[
|
||||
"guid"=>$guid
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user