love_php/app/Events/ChatMessages.php

73 lines
1.9 KiB
PHP
Raw Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Models\User;
use App\Models\ChatMessage;
use App\Models\Notice;
class ChatMessages implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public $message;
public $new_notice_count;
public $new_message_count;
public $tries = 3;
/**
* 指定事件被放置在哪个队列上
*
* @var string
*/
public $broadcastQueue = 'love';
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($message, User $user)
{
$this->user = $user;
$this->message = $message;
$new_notice_count = Notice::where('user_id', $user->id)->where('status', 0)->count();
if (empty($new_notice_count)) {
$new_notice_count = 0;
}
$new_message_count = ChatMessage::where('other_user_id', $user->id)->where('status', 0)->count();
if (empty($new_message_count)) {
$new_message_count = 0;
}
$this->new_notice_count = $new_notice_count;
$this->new_message_count = $new_message_count;
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
if (empty($this->user)) {
return;
}
return new PrivateChannel('users.'.$this->user->id.'.chat.message');
}
// /**
// * 事件的广播名称。
// *
// * @return string
// */
// public function broadcastAs()
// {
// return 'chat.message';
// }
}