diff --git a/app/Http/Controllers/Admin/NotificationController.php b/app/Http/Controllers/Admin/NotificationController.php new file mode 100644 index 0000000..ed58e72 --- /dev/null +++ b/app/Http/Controllers/Admin/NotificationController.php @@ -0,0 +1,88 @@ +query($request)->paginate(20)->withQueryString(); + + return view('admin.notifications.create', compact('modules', 'users')); + } + + /** + * Store a newly created resource in storage. + * + * @param Request $request + * + * @return RedirectResponse + */ + public function store(Request $request) + { + $request->validate([ + 'title' => 'required', + 'content' => 'required', + 'user_id' => 'nullable', + 'module_id' => 'nullable', + 'user' => 'nullable', + ]); + + dispatch(new SendCommonNotificationsJob($request->toArray(), $request->input('title'), $request->input('content'))); + + return back()->with('success', '通知发送成功。')->withInput(); + } + + public function query(Request|array $request): User|CachedBuilder + { + if ($request instanceof Request) { + $request = $request->all(); + } + + if (!empty($request['user_id'])) { + $users = User::where('id', $request['user_id']); + } else { + $users = User::query(); + + if (!empty($request['user'])) { + $user = $request['user']; + + if ($user == 'active') { + // 寻找有 host 的用户 + $users = $users->whereHas('hosts'); + } else if ($user == 'normal') { + $users = $users->whereNull('banned_at'); + } else if ($user == 'banned') { + $users = $users->whereNotNull('banned_at'); + } + } + } + + if (!empty($request['module_id'])) { + // 从 hosts 中找到 module_id,然后找到拥有此 host 的用户 + $users = $users->whereHas('hosts', function ($query) use ($request) { + $query->where('module_id', $request['module_id']); + }); + } + + return $users; + } +} diff --git a/app/Jobs/SendCommonNotificationsJob.php b/app/Jobs/SendCommonNotificationsJob.php new file mode 100644 index 0000000..294e600 --- /dev/null +++ b/app/Jobs/SendCommonNotificationsJob.php @@ -0,0 +1,57 @@ +requests = $requests; + $this->title = $title; + $this->content = $content; + } + + /** + * Execute the job. + * + * @return void + */ + public function handle(): void + { + // + + $notificationController = new NotificationController(); + + $users = $notificationController->query($this->requests); + + // chunk + $users->chunk(100, function ($users) { + foreach ($users as $user) { + $user->notify(new CommonNotification($this->title, $this->content)); + } + }); + } +} diff --git a/app/Notifications/CommonNotification.php b/app/Notifications/CommonNotification.php new file mode 100644 index 0000000..99464b5 --- /dev/null +++ b/app/Notifications/CommonNotification.php @@ -0,0 +1,69 @@ +title = $title; + $this->content = $content; + } + + /** + * Get the notification's delivery channels. + * + * @param mixed $notifiable + * + * @return array + */ + public function via($notifiable): array + { + return ['mail']; + } + + /** + * Get the mail representation of the notification. + * + * @param mixed $notifiable + * + * @return MailMessage + */ + public function toMail($notifiable): MailMessage + { + return (new MailMessage)->subject($this->title)->markdown('mail.common', [ + 'title' => $this->title, + 'content' => $this->content, + ]); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * + * @return array + */ + public function toArray($notifiable): array + { + return [ + // + ]; + } +} diff --git a/resources/views/admin/notifications/create.blade.php b/resources/views/admin/notifications/create.blade.php new file mode 100644 index 0000000..1e5638e --- /dev/null +++ b/resources/views/admin/notifications/create.blade.php @@ -0,0 +1,127 @@ +@extends('layouts.admin') + +@section('title', '通知') + +@section('content') +
ID | +用户名 | +邮箱 | +余额 | +用户组 | +注册时间 | +操作 | + + + + @foreach ($users as $user) +
---|---|---|---|---|---|---|
+ + {{ $user->id }} + + | ++ + {{ $user->name }} + + | ++ {{ $user->email }} + | ++ {{ $user->balance }} 元 + | ++ @if ($user->user_group_id) + + {{ $user->user_group->name }} + + @else + 无 + @endif + | ++ {{ $user->created_at }} + | ++ 编辑 + | +