diff --git a/app/Events/Users.php b/app/Events/Users.php index 4e2ce5e..87bc047 100644 --- a/app/Events/Users.php +++ b/app/Events/Users.php @@ -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'; } diff --git a/app/Http/Controllers/Admin/NotificationController.php b/app/Http/Controllers/Admin/NotificationController.php index d1fe4c5..306197a 100644 --- a/app/Http/Controllers/Admin/NotificationController.php +++ b/app/Http/Controllers/Admin/NotificationController.php @@ -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(); } diff --git a/app/Http/Controllers/Modules/BroadcastController.php b/app/Http/Controllers/Modules/BroadcastController.php index 0389c46..caa6e10 100644 --- a/app/Http/Controllers/Modules/BroadcastController.php +++ b/app/Http/Controllers/Modules/BroadcastController.php @@ -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', ]; } diff --git a/app/Notifications/User/UserNotification.php b/app/Notifications/User/UserNotification.php new file mode 100644 index 0000000..3bc742a --- /dev/null +++ b/app/Notifications/User/UserNotification.php @@ -0,0 +1,73 @@ +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', + ]; + } +} diff --git a/app/Notifications/WebNotification.php b/app/Notifications/WebNotification.php index 4e383ed..e9514c5 100644 --- a/app/Notifications/WebNotification.php +++ b/app/Notifications/WebNotification.php @@ -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; } /**