68 lines
1.7 KiB
PHP
68 lines
1.7 KiB
PHP
<?php
|
||
|
||
/**
|
||
* 文件功能:过期神秘箱子队列任务
|
||
*
|
||
* 在箱子到期后将其状态更新为 expired(若尚未被领取),并向公屏广播过期通知。
|
||
*
|
||
* @author ChatRoom Laravel
|
||
*
|
||
* @version 1.0.0
|
||
*/
|
||
|
||
namespace App\Jobs;
|
||
|
||
use App\Events\MessageSent;
|
||
use App\Models\MysteryBox;
|
||
use App\Services\ChatStateService;
|
||
use Illuminate\Contracts\Queue\ShouldQueue;
|
||
use Illuminate\Foundation\Queue\Queueable;
|
||
|
||
class ExpireMysteryBoxJob implements ShouldQueue
|
||
{
|
||
use Queueable;
|
||
|
||
/**
|
||
* 最大重试次数。
|
||
*/
|
||
public int $tries = 1;
|
||
|
||
/**
|
||
* @param int $boxId 要过期的箱子ID
|
||
*/
|
||
public function __construct(public readonly int $boxId) {}
|
||
|
||
/**
|
||
* 执行过期逻辑。
|
||
*/
|
||
public function handle(ChatStateService $chatState): void
|
||
{
|
||
$box = MysteryBox::find($this->boxId);
|
||
|
||
// 箱子不存在或已经被领取/过期,跳过
|
||
if (! $box || $box->status !== 'open') {
|
||
return;
|
||
}
|
||
|
||
// 标记为过期
|
||
$box->update(['status' => 'expired']);
|
||
|
||
// 公屏广播过期通知
|
||
$msg = [
|
||
'id' => $chatState->nextMessageId(1),
|
||
'room_id' => 1,
|
||
'from_user' => '系统传音',
|
||
'to_user' => '大家',
|
||
'content' => "⏰ 神秘箱子(暗号:{$box->passcode})已超时,箱子消失了!下次要快哦~",
|
||
'is_secret' => false,
|
||
'font_color' => '#9ca3af',
|
||
'action' => '大声宣告',
|
||
'sent_at' => now()->toDateTimeString(),
|
||
];
|
||
|
||
$chatState->pushMessage(1, $msg);
|
||
broadcast(new MessageSent(1, $msg));
|
||
SaveMessageJob::dispatch($msg);
|
||
}
|
||
}
|