增加和改进 前端通知

This commit is contained in:
iVampireSP.com 2023-01-13 22:12:49 +08:00
parent 0a5867d8fd
commit fc00c5a867
No known key found for this signature in database
GPG Key ID: 2F7B001CA27A8132
5 changed files with 92 additions and 18 deletions

View File

@ -17,10 +17,9 @@ class Users extends Event implements ShouldBroadcastNow
use SerializesModels; use SerializesModels;
public User $user; public User $user;
public string $type = 'ping';
public array|Model $data; public array|Model $data;
public null|Module $module = null; public null|Module $module = null;
public string $event = 'messages'; public string $event = 'notification';
public Carbon $sent_at; public Carbon $sent_at;
@ -30,7 +29,7 @@ class Users extends Event implements ShouldBroadcastNow
* *
* @return void * @return void
*/ */
public function __construct(User|int $user, $type, array|Model $data) public function __construct(User|int $user, $event, array|Model $data)
{ {
// init vars // init vars
$this->sent_at = Carbon::now(); $this->sent_at = Carbon::now();
@ -42,7 +41,7 @@ public function __construct(User|int $user, $type, array|Model $data)
$this->user = $user; $this->user = $user;
$this->type = $type; $this->event = $event;
if ($data instanceof Model) { if ($data instanceof Model) {
$this->data = $data->toArray(); $this->data = $data->toArray();
@ -50,8 +49,6 @@ public function __construct(User|int $user, $type, array|Model $data)
$this->data = $data; $this->data = $data;
} }
// check if module
if (Auth::guard('module')->check()) { if (Auth::guard('module')->check()) {
$this->module = Auth::guard('module')->user(); $this->module = Auth::guard('module')->user();
@ -60,11 +57,11 @@ public function __construct(User|int $user, $type, array|Model $data)
} }
} }
// log // log
if (config('app.env') != 'production') { if (config('app.env') != 'production') {
Log::debug('Users Event', [ Log::debug('Users Event', [
'user' => $this->user->id, 'user' => $this->user->id,
'type' => $this->type,
'data' => $this->data, 'data' => $this->data,
'module' => $this->module, 'module' => $this->module,
'event' => $this->event, 'event' => $this->event,
@ -72,12 +69,14 @@ public function __construct(User|int $user, $type, array|Model $data)
} }
} }
public function broadcastOn(): PrivateChannel public
function broadcastOn(): PrivateChannel
{ {
return new PrivateChannel('users.' . $this->user->id); return new PrivateChannel('users.' . $this->user->id);
} }
public function broadcastAs(): string public
function broadcastAs(): string
{ {
return 'messages'; return 'messages';
} }

View File

@ -3,7 +3,7 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Jobs\SendCommonNotificationsJob; use App\Jobs\User\SendUserNotificationsJob;
use App\Models\Module; use App\Models\Module;
use App\Models\User; use App\Models\User;
use GeneaLabs\LaravelModelCaching\CachedBuilder; use GeneaLabs\LaravelModelCaching\CachedBuilder;
@ -80,9 +80,13 @@ public function store(Request $request): RedirectResponse
'user_id' => 'nullable', 'user_id' => 'nullable',
'module_id' => 'nullable', 'module_id' => 'nullable',
'user' => 'nullable', 'user' => 'nullable',
'send_mail' => 'boolean',
]); ]);
dispatch(new SendCommonNotificationsJob($request->toArray(), $request->input('title'), $request->input('content'))); // send mail 是 checkbox值为 1
$send_mail = $request->has('send_mail');
dispatch(new SendUserNotificationsJob($request->toArray(), $request->input('title'), $request->input('content'), $send_mail));
return back()->with('success', '通知发送成功。')->withInput(); return back()->with('success', '通知发送成功。')->withInput();
} }

View File

@ -14,7 +14,7 @@ public function broadcast_to_user(Request $request, User $user): JsonResponse
{ {
$this->validate($request, $this->rules()); $this->validate($request, $this->rules());
$user->notify(new WebNotification($request->all(), $request->input('type'))); $user->notify(new WebNotification($request->all(), $request->input('event')));
return $this->created("message sent."); return $this->created("message sent.");
} }
@ -23,8 +23,7 @@ private function rules(): array
{ {
return [ return [
'message' => 'required|string|max:255', 'message' => 'required|string|max:255',
'type' => 'required|in:info,error,warning,success', 'event' => 'required|alpha',
'event' => 'nullable|alpha',
]; ];
} }

View File

@ -0,0 +1,73 @@
<?php
namespace App\Notifications\User;
use App\Notifications\Channels\WebChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class UserNotification extends Notification implements ShouldQueue
{
use Queueable;
public string $title;
public string $content;
public bool $send_mail;
/**
* Create a new notification instance.
*
* @return void
*/
public function __construct(string $title, string $content, bool $send_mail)
{
$this->title = $title;
$this->content = $content;
$this->send_mail = $send_mail;
}
/**
* Get the notification's delivery channels.
*
* @return array
*/
public function via(): array
{
$channels = [WebChannel::class];
if ($this->send_mail) {
$channels[] = 'mail';
}
return $channels;
}
/**
* Get the mail representation of the notification.
*
* @return MailMessage
*/
public function toMail(): MailMessage
{
return (new MailMessage)->subject($this->title)->markdown('mail.common', [
'title' => $this->title,
'content' => $this->content,
]);
}
/**
* Get the array representation of the notification.
*
* @return array
*/
public function toArray(): array
{
return [
'title' => $this->title,
'content' => $this->content,
'event' => 'notification',
];
}
}

View File

@ -2,6 +2,7 @@
namespace App\Notifications; namespace App\Notifications;
use App\Notifications\Channels\WebChannel;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notification; use Illuminate\Notifications\Notification;
@ -11,22 +12,20 @@ class WebNotification extends Notification
use Queueable; use Queueable;
public array|Model $message = []; public array|Model $message = [];
public string $type = 'info'; public string $event = 'notification';
/** /**
* Create a new notification instance. * Create a new notification instance.
* *
* @return void * @return void
*/ */
public function __construct(array|Model $message, string $type) public function __construct(array|Model $message, string $event)
{ {
if ($message instanceof Model) { if ($message instanceof Model) {
$message = $message->toArray(); $message = $message->toArray();
} }
$this->message = $message; $this->message = $message;
$this->type = $type;
} }
/** /**