love_php/app/Events/NoticeServer.php

94 lines
2.4 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\Notice;
use App\Models\User;
use App\Models\ChatMessage;
use App\Models\AssistantUser;
class NoticeServer implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $user;
public $new_notice_count;
public $new_message_count;
public $new_assistant_message_count;
/**
* 指定事件被放置在哪个队列上
*
* @var string
*/
public $broadcastQueue = 'love';
/**
* Create a new event instance.
*
* @return void
*/
public function __construct(User $user)
{
$this->user = $user;
$this->new_notice_count = Notice::where('user_id', $this->user->id)->where('status', 0)->count();
$this->new_message_count = ChatMessage::where('other_user_id', $this->user->id)->where('user_id', '<>', 1)->where('status', 0)->count();
$this->new_assistant_message_count = AssistantUser::where('user_id', $this->user->id)->where('status', 0)->count();
}
/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
if (empty($this->user)) {
return;
}
if (empty($this->user->id)) {
return;
}
return new PrivateChannel('users.'.$this->user->id.'.notices');
}
// /**
// * 事件的广播名称。
// *
// * @return string
// */
// public function broadcastAs()
// {
// return 'notice.server';
// }
// /**
// * 指定广播数据
// *
// * @return array
// */
// public function broadcastWith()
// {
// return [
// 'new_notice_count' => $this->new_notice_count,
// 'new_message_count'=> $this->new_message_count,
// ];
// }
// /**
// * Determine if this event should broadcast.
// *
// * @return bool
// */
// public function broadcastWhen()
// {
// return $this->new_notice_count > 0 || $this->new_notice_count > 0;
// }
}