feat(wechat): 微信机器人全链路集成与稳定性修复
- 新增:管理员后台的微信机器人双向收发参数设置页面及扫码绑定能力。 - 新增:WechatBotApiService 与 KafkaConsumerService 模块打通过往僵尸进程导致的拒绝连接问题。 - 新增:下发所有群发/私聊通知时统一带上「[和平聊吧]」标注前缀。 - 优化:前端个人中心绑定逻辑支持一键生成及复制动态口令。 - 修复:闭环联调修补各个模型中产生的变量警告如 stdClass 对象获取等异常预警。
This commit is contained in:
@@ -0,0 +1,125 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:微信机器人接口调用服务
|
||||
*
|
||||
* 封装与微信机器人 HTTP API 的通信逻辑,用于向上游服务发送文本和富文本消息。
|
||||
*
|
||||
* @author ChatRoom Laravel
|
||||
*
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
namespace App\Services\WechatBot;
|
||||
|
||||
use App\Models\SysParam;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class WechatBotApiService
|
||||
{
|
||||
/**
|
||||
* @var string API 基础地址
|
||||
*/
|
||||
protected string $baseUrl;
|
||||
|
||||
/**
|
||||
* @var int 请求超时时间(秒)
|
||||
*/
|
||||
protected int $timeout = 10;
|
||||
|
||||
/**
|
||||
* @var string 机器人 Key
|
||||
*/
|
||||
protected string $botKey;
|
||||
|
||||
/**
|
||||
* 构造函数 — 从 SysParam 获取 api_base_url 配置
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$param = SysParam::where('alias', 'wechat_bot_config')->first();
|
||||
if ($param && ! empty($param->body)) {
|
||||
$config = json_decode($param->body, true);
|
||||
$this->baseUrl = rtrim($config['api']['base_url'] ?? '', '/');
|
||||
$this->botKey = $config['api']['bot_key'] ?? '';
|
||||
} else {
|
||||
$this->baseUrl = '';
|
||||
$this->botKey = '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送文本消息
|
||||
*
|
||||
* @param string $toUser 目标用户 wxid 或群聊 ID
|
||||
* @param string $content 文本内容
|
||||
* @param array $atList 群聊中 @ 的用户 wxid 列表
|
||||
* @return array{success: bool, data: array|null, error: string|null}
|
||||
*/
|
||||
public function sendTextMessage(string $toUser, string $content, array $atList = []): array
|
||||
{
|
||||
if (empty($this->baseUrl)) {
|
||||
return ['success' => false, 'data' => null, 'error' => 'API Base URL is not configured.'];
|
||||
}
|
||||
|
||||
$url = "{$this->baseUrl}/message/SendTextMessage";
|
||||
|
||||
$finalContent = "[和平聊吧]\n".$content;
|
||||
|
||||
$payload = [
|
||||
'MsgItem' => [
|
||||
[
|
||||
'AtWxIDList' => $atList,
|
||||
'ImageContent' => '',
|
||||
'MsgType' => 0,
|
||||
'TextContent' => $finalContent,
|
||||
'ToUserName' => $toUser,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
try {
|
||||
$response = Http::timeout($this->timeout)
|
||||
->withHeaders([
|
||||
'Content-Type' => 'application/json',
|
||||
'Accept' => 'application/json',
|
||||
])
|
||||
->post("{$url}?key={$this->botKey}", $payload);
|
||||
|
||||
if ($response->successful()) {
|
||||
$data = $response->json();
|
||||
|
||||
if (($data['Code'] ?? 0) === 200) {
|
||||
Log::info('微信机器人消息发送成功', [
|
||||
'to_user' => $toUser,
|
||||
'content' => mb_substr($content, 0, 50),
|
||||
]);
|
||||
|
||||
return ['success' => true, 'data' => $data['Data'] ?? [], 'error' => null];
|
||||
}
|
||||
|
||||
$desc = $data['Text'] ?? 'Unknown';
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'data' => null,
|
||||
'error' => "API 返回错误: Code={$data['Code']}, Text={$desc}",
|
||||
];
|
||||
}
|
||||
|
||||
return [
|
||||
'success' => false,
|
||||
'data' => null,
|
||||
'error' => "HTTP 请求失败: {$response->status()}",
|
||||
];
|
||||
} catch (\Exception $e) {
|
||||
Log::error('微信机器人消息发送异常', [
|
||||
'to_user' => $toUser,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
|
||||
return ['success' => false, 'data' => null, 'error' => '发送异常: '.$e->getMessage()];
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user