新增聊天室发送图片功能
This commit is contained in:
@@ -31,14 +31,20 @@ use App\Services\UserCurrencyService;
|
||||
use App\Services\VipService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\View\View;
|
||||
use Intervention\Image\Drivers\Gd\Driver;
|
||||
use Intervention\Image\ImageManager;
|
||||
|
||||
/**
|
||||
* 聊天室核心控制器
|
||||
* 负责进房、发言、退房、公告与聊天室内各种实时交互。
|
||||
*/
|
||||
class ChatController extends Controller
|
||||
{
|
||||
/**
|
||||
@@ -333,6 +339,7 @@ class ChatController extends Controller
|
||||
{
|
||||
$data = $request->validated();
|
||||
$user = Auth::user();
|
||||
$imagePayload = null;
|
||||
|
||||
// 0. 检查用户是否被禁言(Redis TTL 自动过期)
|
||||
$muteKey = "mute:{$id}:{$user->username}";
|
||||
@@ -360,12 +367,18 @@ class ChatController extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
// 1. 过滤净化消息体
|
||||
$pureContent = $this->filter->filter($data['content'] ?? '');
|
||||
if ($pureContent === '') {
|
||||
// 1. 过滤净化消息体;若本次只发图片,则允许文本内容为空。
|
||||
$rawContent = (string) ($data['content'] ?? '');
|
||||
$pureContent = $rawContent !== '' ? $this->filter->filter($rawContent) : '';
|
||||
if ($pureContent === '' && ! $request->hasFile('image')) {
|
||||
return response()->json(['status' => 'error', 'message' => '消息内容不能为空或不合法。'], 422);
|
||||
}
|
||||
|
||||
// 2. 若带图片,则生成原图与缩略图并按日期目录保存。
|
||||
if ($request->hasFile('image')) {
|
||||
$imagePayload = $this->storeChatImage($request->file('image'), $user->id);
|
||||
}
|
||||
|
||||
// 2. 封装消息对象
|
||||
$messageData = [
|
||||
'id' => $this->chatState->nextMessageId($id), // 分布式安全自增序号
|
||||
@@ -376,8 +389,12 @@ class ChatController extends Controller
|
||||
'is_secret' => $data['is_secret'] ?? false,
|
||||
'font_color' => $data['font_color'] ?? '',
|
||||
'action' => $data['action'] ?? '',
|
||||
'message_type' => $imagePayload ? 'image' : 'text',
|
||||
'sent_at' => now()->toDateTimeString(),
|
||||
];
|
||||
if ($imagePayload !== null) {
|
||||
$messageData = array_merge($messageData, $imagePayload);
|
||||
}
|
||||
|
||||
// 3. 压入 Redis 缓存列表 (防炸内存,只保留最近 N 条)
|
||||
$this->chatState->pushMessage($id, $messageData);
|
||||
@@ -405,6 +422,39 @@ class ChatController extends Controller
|
||||
return response()->json(['status' => 'success']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存聊天图片并生成原图、缩略图两份资源。
|
||||
*
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function storeChatImage(UploadedFile $image, int $userId): array
|
||||
{
|
||||
$manager = new ImageManager(new Driver);
|
||||
$extension = strtolower($image->extension() ?: 'jpg');
|
||||
$datePath = now()->format('Y-m-d');
|
||||
$basename = 'chat_'.$userId.'_'.Str::uuid();
|
||||
$originalPath = "chat-images/{$datePath}/{$basename}_original.{$extension}";
|
||||
$thumbPath = "chat-images/{$datePath}/{$basename}_thumb.{$extension}";
|
||||
|
||||
// 原图仅做缩边处理,避免超大文件直接灌进磁盘。
|
||||
$originalImage = $manager->read($image);
|
||||
$originalImage->scaleDown(width: 1600, height: 1600);
|
||||
Storage::disk('public')->put($originalPath, (string) $originalImage->encode());
|
||||
|
||||
// 缩略图限制在 220px 范围内,聊天室里只展示轻量小图。
|
||||
$thumbImage = $manager->read($image);
|
||||
$thumbImage->scaleDown(width: 220, height: 220);
|
||||
Storage::disk('public')->put($thumbPath, (string) $thumbImage->encode());
|
||||
|
||||
return [
|
||||
'image_path' => $originalPath,
|
||||
'image_thumb_path' => $thumbPath,
|
||||
'image_original_name' => $image->getClientOriginalName(),
|
||||
'image_url' => Storage::url($originalPath),
|
||||
'image_thumb_url' => Storage::url($thumbPath),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动挂机存点心跳与经验升级 (新增)
|
||||
* 替代原版定时 iframe 刷新的 save.asp。
|
||||
|
||||
Reference in New Issue
Block a user