改进 通知

This commit is contained in:
iVampireSP.com 2023-01-11 20:19:05 +08:00
parent 736638aadf
commit 2c275c08cf
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
9 changed files with 119 additions and 35 deletions

View File

@ -7,8 +7,10 @@
use Carbon\Carbon;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
class Users extends Event implements ShouldBroadcastNow
{
@ -16,8 +18,9 @@ class Users extends Event implements ShouldBroadcastNow
public User $user;
public string $type = 'ping';
public array $data;
public null|Module $module;
public array|Model $data;
public null|Module $module = null;
public string $event = 'messages';
public Carbon $sent_at;
@ -27,18 +30,45 @@ class Users extends Event implements ShouldBroadcastNow
*
* @return void
*/
public function __construct(User $user, $type, array $data)
public function __construct(User|int $user, $type, array|Model $data)
{
$this->user = $user;
$this->type = $type;
$this->data = $data;
// init vars
$this->sent_at = Carbon::now();
// if user is int
if (is_int($user)) {
$user = User::find($user);
}
$this->user = $user;
$this->type = $type;
if ($data instanceof Model) {
$this->data = $data->toArray();
} else {
$this->data = $data;
}
// check if module
if (Auth::guard('module')->check()) {
$this->module = Auth::guard('module')->user();
} else {
$this->module = null;
if (isset($this->data['event'])) {
$this->event = $this->module->id . '.' . $this->data['event'];
}
}
// log
if (config('app.env') != 'production') {
Log::debug('Users Event', [
'user' => $this->user->id,
'type' => $this->type,
'data' => $this->data,
'module' => $this->module,
'event' => $this->event,
]);
}
}
@ -49,6 +79,6 @@ public function broadcastOn(): PrivateChannel
public function broadcastAs(): string
{
return 'common';
return 'messages';
}
}

View File

@ -2,10 +2,9 @@
namespace App\Http\Controllers\Modules;
use App\Events\Users;
use App\Http\Controllers\Controller;
use App\Models\Host;
use App\Models\User;
use App\Notifications\WebNotification;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
@ -15,7 +14,7 @@ public function broadcast_to_user(Request $request, User $user): JsonResponse
{
$this->validate($request, $this->rules());
broadcast(new Users($user, $request->input('type'), $request->all()));
$user->notify(new WebNotification($request->all(), $request->input('type')));
return $this->created("message sent.");
}
@ -25,7 +24,7 @@ private function rules(): array
return [
'message' => 'required|string|max:255',
'type' => 'required|in:info,error,warning,success',
'data' => 'nullable|json',
'event' => 'nullable|alpha',
];
}

View File

@ -4,6 +4,7 @@
use App\Events\Users;
use App\Jobs\Module\HostJob;
use App\Notifications\WebNotification;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
@ -51,10 +52,13 @@ protected static function boot()
}
});
static::created(function ($model) {
broadcast(new Users($model->user_id, 'hosts.created', $model));
static::created(function (self $model) {
// broadcast(new Users($model->user, 'success', $model));
$model->user->notify(new WebNotification($model, 'hosts.created'));
});
static::updating(function ($model) {
if ($model->isDirty('status')) {
if ($model->status == 'suspended') {

View File

@ -80,4 +80,10 @@ public function scopeBirthday()
return $this->select(['id', 'name', 'birthday_at', 'email_md5', 'created_at'])->whereMonth('birthday_at', now()->month)
->whereDay('birthday_at', now()->day)->whereNull('banned_at');
}
public function selectPublic(): User
{
// 过滤掉私有字段
return $this->select(['id', 'name', 'email_md5', 'created_at']);
}
}

View File

@ -9,7 +9,6 @@
use App\Models\User;
use App\Notifications\WorkOrder as WorkOrderNotification;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
@ -149,9 +148,4 @@ public function routeNotificationForMail(WorkOrderNotification $work_order): arr
return [$user->email => $user->name];
}
public function receivesBroadcastNotificationsOn(): string
{
return new PrivateChannel('users.' . $this->user_id);
}
}

View File

@ -7,7 +7,7 @@
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
class CommonChannel extends Notification
class WebChannel extends Notification
{
use Queueable;
@ -32,10 +32,9 @@ public function send(mixed $notifiable, Notification $notification): void
$user = User::find($user_id);
if (!in_array($data['type'] ?? '', ['info', 'success', 'warning', 'error'])) {
return;
$data['type'] = 'info';
}
broadcast(new Users($user, $data['type'], $data));
}
}

View File

@ -0,0 +1,51 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notification;
class WebNotification extends Notification
{
use Queueable;
public array|Model $message = [];
public string $type = 'info';
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(array|Model $message, string $type)
{
if ($message instanceof Model) {
$message = $message->toArray();
}
$this->message = $message;
$this->type = $type;
}
/**
* Get the notification's delivery channels.
*
* @return array
*/
public function via(): array
{
return [WebChannel::class];
}
/**
* Get the array representation of the notification.
*
* @return array
*/
public function toArray(): array
{
return $this->message;
}
}

View File

@ -33,7 +33,7 @@ public function __construct(WorkOrderModel $work_order)
*/
public function via(WorkOrderModel $workOrder): array
{
$methods = [WeComChannel::class, CommonChannel::class];
$methods = [WeComChannel::class, WebChannel::class];
if (in_array($workOrder->status, ['processing', 'replied'])) {
$methods[] = 'mail';
@ -69,8 +69,12 @@ public function toMail(WorkOrderModel $workOrder): MailMessage
public function toArray(WorkOrderModel $workOrder): array
{
$array = $workOrder->toArray();
$array['type'] = 'info';
$array['title'] = '工单: ' . $workOrder->title . ' 状态更新。';
$array['event'] = 'WorkOrder.updated';
if ($workOrder->status === 'replied') {
$array['event'] = 'WorkOrder.replied';
}
return $array;
}

View File

@ -1,18 +1,15 @@
<?php
// use App\Models\Task;
use Illuminate\Support\Facades\Broadcast;
Broadcast::channel('users.{userId}', function ($user, $userId) {
return (int)$user->id === (int)$userId;
});
Broadcast::channel('tasks.{userId}', function ($user, $userId) {
return (int)$user->id === (int)$userId;
});
Broadcast::channel('work-orders.{uuid}', function () {
return true;
});
// Broadcast::channel('tasks.{task}', function ($user, Task $task) {
// return (int)$user->id === (int)$task->user_id;
// });
Broadcast::channel('servers', function () {
return true;