增加和改进 前端通知
This commit is contained in:
parent
0a5867d8fd
commit
fc00c5a867
@ -17,10 +17,9 @@ class Users extends Event implements ShouldBroadcastNow
|
||||
use SerializesModels;
|
||||
|
||||
public User $user;
|
||||
public string $type = 'ping';
|
||||
public array|Model $data;
|
||||
public null|Module $module = null;
|
||||
public string $event = 'messages';
|
||||
public string $event = 'notification';
|
||||
|
||||
public Carbon $sent_at;
|
||||
|
||||
@ -30,7 +29,7 @@ class Users extends Event implements ShouldBroadcastNow
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(User|int $user, $type, array|Model $data)
|
||||
public function __construct(User|int $user, $event, array|Model $data)
|
||||
{
|
||||
// init vars
|
||||
$this->sent_at = Carbon::now();
|
||||
@ -42,7 +41,7 @@ public function __construct(User|int $user, $type, array|Model $data)
|
||||
|
||||
$this->user = $user;
|
||||
|
||||
$this->type = $type;
|
||||
$this->event = $event;
|
||||
|
||||
if ($data instanceof Model) {
|
||||
$this->data = $data->toArray();
|
||||
@ -50,8 +49,6 @@ public function __construct(User|int $user, $type, array|Model $data)
|
||||
$this->data = $data;
|
||||
}
|
||||
|
||||
|
||||
// check if module
|
||||
if (Auth::guard('module')->check()) {
|
||||
$this->module = Auth::guard('module')->user();
|
||||
|
||||
@ -60,11 +57,11 @@ public function __construct(User|int $user, $type, array|Model $data)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 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,
|
||||
@ -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);
|
||||
}
|
||||
|
||||
public function broadcastAs(): string
|
||||
public
|
||||
function broadcastAs(): string
|
||||
{
|
||||
return 'messages';
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Jobs\SendCommonNotificationsJob;
|
||||
use App\Jobs\User\SendUserNotificationsJob;
|
||||
use App\Models\Module;
|
||||
use App\Models\User;
|
||||
use GeneaLabs\LaravelModelCaching\CachedBuilder;
|
||||
@ -80,9 +80,13 @@ public function store(Request $request): RedirectResponse
|
||||
'user_id' => 'nullable',
|
||||
'module_id' => '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();
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ public function broadcast_to_user(Request $request, User $user): JsonResponse
|
||||
{
|
||||
$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.");
|
||||
}
|
||||
@ -23,8 +23,7 @@ private function rules(): array
|
||||
{
|
||||
return [
|
||||
'message' => 'required|string|max:255',
|
||||
'type' => 'required|in:info,error,warning,success',
|
||||
'event' => 'nullable|alpha',
|
||||
'event' => 'required|alpha',
|
||||
];
|
||||
}
|
||||
|
||||
|
73
app/Notifications/User/UserNotification.php
Normal file
73
app/Notifications/User/UserNotification.php
Normal 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',
|
||||
];
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Notifications;
|
||||
|
||||
use App\Notifications\Channels\WebChannel;
|
||||
use Illuminate\Bus\Queueable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Notifications\Notification;
|
||||
@ -11,22 +12,20 @@ class WebNotification extends Notification
|
||||
use Queueable;
|
||||
|
||||
public array|Model $message = [];
|
||||
public string $type = 'info';
|
||||
public string $event = 'notification';
|
||||
|
||||
/**
|
||||
* Create a new notification instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array|Model $message, string $type)
|
||||
public function __construct(array|Model $message, string $event)
|
||||
{
|
||||
if ($message instanceof Model) {
|
||||
$message = $message->toArray();
|
||||
}
|
||||
|
||||
$this->message = $message;
|
||||
|
||||
$this->type = $type;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user