liufumingren 9b25b43e63 1
2026-09-10 14:58:24 +08:00

98 lines
3.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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]);
\think\Log::error("[QiweGetProfile] : {$result} ");
$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;
}
}
}