2026-02-26 21:30:07 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 文件功能:聊天机器人控制器
|
|
|
|
|
|
*
|
|
|
|
|
|
* 处理用户与 AI 机器人的对话请求。
|
|
|
|
|
|
* 先检查全局开关(sysparam: chatbot_enabled),再调用 AiChatService 获取回复。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author ChatRoom Laravel
|
|
|
|
|
|
*
|
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
2026-03-26 09:34:28 +08:00
|
|
|
|
use App\Enums\CurrencySource;
|
2026-02-26 22:11:11 +08:00
|
|
|
|
use App\Events\MessageSent;
|
|
|
|
|
|
use App\Jobs\SaveMessageJob;
|
2026-02-26 21:30:07 +08:00
|
|
|
|
use App\Models\Sysparam;
|
|
|
|
|
|
use App\Services\AiChatService;
|
2026-02-26 22:11:11 +08:00
|
|
|
|
use App\Services\ChatStateService;
|
2026-03-26 09:34:28 +08:00
|
|
|
|
use App\Services\UserCurrencyService;
|
2026-02-26 21:30:07 +08:00
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2026-03-26 09:34:28 +08:00
|
|
|
|
use Illuminate\Support\Facades\Redis;
|
2026-02-26 21:30:07 +08:00
|
|
|
|
|
|
|
|
|
|
class ChatBotController extends Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
/**
|
2026-02-26 22:11:11 +08:00
|
|
|
|
* 构造函数:注入 AI 聊天服务和聊天状态服务
|
2026-02-26 21:30:07 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
|
private readonly AiChatService $aiChat,
|
2026-02-26 22:11:11 +08:00
|
|
|
|
private readonly ChatStateService $chatState,
|
2026-03-26 09:34:28 +08:00
|
|
|
|
private readonly UserCurrencyService $currencyService,
|
2026-02-26 21:30:07 +08:00
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 与 AI 机器人对话
|
|
|
|
|
|
*
|
|
|
|
|
|
* 接收用户消息,检查全局开关后调用 AI 服务获取回复。
|
|
|
|
|
|
* 支持自动故障转移:默认厂商失败时自动尝试备用厂商。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param Request $request 请求对象,需包含 message 和 room_id
|
|
|
|
|
|
* @return JsonResponse 机器人回复或错误信息
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function chat(Request $request): JsonResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
// 验证请求参数
|
|
|
|
|
|
$request->validate([
|
|
|
|
|
|
'message' => 'required|string|max:2000',
|
|
|
|
|
|
'room_id' => 'required|integer',
|
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
|
|
// 检查全局开关
|
|
|
|
|
|
$enabled = Sysparam::getValue('chatbot_enabled', '0');
|
|
|
|
|
|
if ($enabled !== '1') {
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
|
'status' => 'error',
|
|
|
|
|
|
'message' => 'AI 机器人功能已关闭,请联系管理员开启。',
|
|
|
|
|
|
], 403);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-26 11:15:11 +08:00
|
|
|
|
$aiUser = \App\Models\User::where('username', 'AI小班长')->first();
|
|
|
|
|
|
if ($aiUser) {
|
|
|
|
|
|
$aiUser->increment('exp_num', 1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-26 21:30:07 +08:00
|
|
|
|
$user = Auth::user();
|
|
|
|
|
|
$message = $request->input('message');
|
|
|
|
|
|
$roomId = $request->input('room_id');
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
2026-02-26 22:11:11 +08:00
|
|
|
|
// 先广播用户的提问消息
|
|
|
|
|
|
$userMsg = [
|
|
|
|
|
|
'id' => $this->chatState->nextMessageId($roomId),
|
|
|
|
|
|
'room_id' => $roomId,
|
|
|
|
|
|
'from_user' => $user->username,
|
2026-02-27 10:54:41 +08:00
|
|
|
|
'to_user' => 'AI小班长',
|
2026-02-26 22:11:11 +08:00
|
|
|
|
'content' => $message,
|
|
|
|
|
|
'is_secret' => false,
|
|
|
|
|
|
'font_color' => '#000000',
|
|
|
|
|
|
'action' => '',
|
|
|
|
|
|
'sent_at' => now()->toDateTimeString(),
|
|
|
|
|
|
];
|
|
|
|
|
|
$this->chatState->pushMessage($roomId, $userMsg);
|
|
|
|
|
|
broadcast(new MessageSent($roomId, $userMsg));
|
|
|
|
|
|
SaveMessageJob::dispatch($userMsg);
|
|
|
|
|
|
|
2026-02-26 21:30:07 +08:00
|
|
|
|
$result = $this->aiChat->chat($user->id, $message, $roomId);
|
|
|
|
|
|
|
2026-03-26 09:34:28 +08:00
|
|
|
|
$reply = $result['reply'];
|
|
|
|
|
|
|
|
|
|
|
|
// 检查 AI 是否决定给用户发金币
|
|
|
|
|
|
if (str_contains($reply, '[ACTION:GIVE_GOLD]')) {
|
|
|
|
|
|
$reply = str_replace('[ACTION:GIVE_GOLD]', '', $reply);
|
|
|
|
|
|
$reply = trim($reply);
|
|
|
|
|
|
|
2026-03-26 11:15:11 +08:00
|
|
|
|
$maxDailyRewards = (int) Sysparam::getValue('chatbot_max_daily_rewards', '1');
|
|
|
|
|
|
$maxGold = (int) Sysparam::getValue('chatbot_max_gold', '5000');
|
|
|
|
|
|
|
2026-03-26 09:34:28 +08:00
|
|
|
|
$redisKey = 'ai_chat:give_gold:'.date('Ymd').':'.$user->id;
|
2026-03-26 11:15:11 +08:00
|
|
|
|
$dailyCount = (int) Redis::get($redisKey);
|
|
|
|
|
|
|
|
|
|
|
|
if ($dailyCount < $maxDailyRewards) {
|
|
|
|
|
|
$goldAmount = rand(100, $maxGold);
|
|
|
|
|
|
|
|
|
|
|
|
if ($aiUser && $aiUser->jjb >= $goldAmount) {
|
|
|
|
|
|
Redis::incr($redisKey);
|
|
|
|
|
|
Redis::expire($redisKey, 86400); // 缓存 24 小时
|
|
|
|
|
|
|
|
|
|
|
|
// 真实扣除 AI 金币
|
|
|
|
|
|
$this->currencyService->change(
|
|
|
|
|
|
$aiUser,
|
|
|
|
|
|
'gold',
|
|
|
|
|
|
-$goldAmount,
|
|
|
|
|
|
CurrencySource::GIFT_SENT,
|
|
|
|
|
|
"赏赐给 {$user->username} 的金币福利",
|
|
|
|
|
|
$roomId
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 给用户发放金币
|
|
|
|
|
|
$this->currencyService->change(
|
|
|
|
|
|
$user,
|
|
|
|
|
|
'gold',
|
|
|
|
|
|
$goldAmount,
|
|
|
|
|
|
CurrencySource::AI_GIFT,
|
|
|
|
|
|
'AI小班长发善心赠送的金币福利',
|
|
|
|
|
|
$roomId
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
// 发送全场大广播
|
|
|
|
|
|
$sysMsg = [
|
|
|
|
|
|
'id' => $this->chatState->nextMessageId($roomId),
|
|
|
|
|
|
'room_id' => $roomId,
|
|
|
|
|
|
'from_user' => 'AI小班长',
|
|
|
|
|
|
'to_user' => $user->username,
|
|
|
|
|
|
'content' => "🤖 听闻小萌新哭穷,本班长看你骨骼惊奇,大方地赏赐了 {$goldAmount} 枚金币福利!",
|
|
|
|
|
|
'is_secret' => false,
|
|
|
|
|
|
'font_color' => '#d97706', // 橙色醒目
|
|
|
|
|
|
'action' => '大声宣告',
|
|
|
|
|
|
'sent_at' => now()->toDateTimeString(),
|
|
|
|
|
|
];
|
|
|
|
|
|
$this->chatState->pushMessage($roomId, $sysMsg);
|
|
|
|
|
|
broadcast(new MessageSent($roomId, $sysMsg));
|
|
|
|
|
|
SaveMessageJob::dispatch($sysMsg);
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 如果余额不足
|
|
|
|
|
|
$reply .= "\n\n(哎呀,我这个月的工资花光啦,没钱发金币了,大家多赏点吧~)";
|
|
|
|
|
|
}
|
2026-03-26 09:34:28 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 如果已经领过了,修改回复提醒
|
2026-03-26 11:15:11 +08:00
|
|
|
|
$reply .= "\n\n(系统提示:你今天已经领过金币福利啦,把机会留给其他人吧!)";
|
2026-03-26 09:34:28 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-02-26 22:11:11 +08:00
|
|
|
|
// 广播 AI 回复消息
|
|
|
|
|
|
$botMsg = [
|
|
|
|
|
|
'id' => $this->chatState->nextMessageId($roomId),
|
|
|
|
|
|
'room_id' => $roomId,
|
2026-02-27 10:54:41 +08:00
|
|
|
|
'from_user' => 'AI小班长',
|
2026-02-26 22:11:11 +08:00
|
|
|
|
'to_user' => $user->username,
|
2026-03-26 09:34:28 +08:00
|
|
|
|
'content' => $reply,
|
2026-02-26 22:11:11 +08:00
|
|
|
|
'is_secret' => false,
|
|
|
|
|
|
'font_color' => '#16a34a',
|
|
|
|
|
|
'action' => '',
|
|
|
|
|
|
'sent_at' => now()->toDateTimeString(),
|
|
|
|
|
|
];
|
|
|
|
|
|
$this->chatState->pushMessage($roomId, $botMsg);
|
|
|
|
|
|
broadcast(new MessageSent($roomId, $botMsg));
|
|
|
|
|
|
SaveMessageJob::dispatch($botMsg);
|
|
|
|
|
|
|
2026-02-26 21:30:07 +08:00
|
|
|
|
return response()->json([
|
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
|
'reply' => $result['reply'],
|
|
|
|
|
|
'provider' => $result['provider'],
|
|
|
|
|
|
'model' => $result['model'],
|
|
|
|
|
|
]);
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
|
'status' => 'error',
|
|
|
|
|
|
'message' => $e->getMessage(),
|
|
|
|
|
|
], 500);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 清除当前用户的 AI 对话上下文
|
|
|
|
|
|
*
|
|
|
|
|
|
* 用于用户想要重新开始对话时使用。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param Request $request 请求对象
|
|
|
|
|
|
* @return JsonResponse 操作结果
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function clearContext(Request $request): JsonResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
$user = Auth::user();
|
|
|
|
|
|
$this->aiChat->clearContext($user->id);
|
|
|
|
|
|
|
|
|
|
|
|
return response()->json([
|
|
|
|
|
|
'status' => 'success',
|
|
|
|
|
|
'message' => '对话上下文已清除,可以开始新的对话了。',
|
|
|
|
|
|
]);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|