NotificationController.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Http\Controllers\API;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Resources\API\NotificationCollection;
  5. use App\Http\Resources\API\NotificationResource;
  6. use App\Models\Enums\NotificationStatus;
  7. use App\Models\Notification;
  8. use App\Models\NotificationRecord;
  9. use Carbon\Carbon;
  10. use Illuminate\Http\Request;
  11. use Illuminate\Support\Facades\Auth;
  12. class NotificationController extends Controller
  13. {
  14. /**
  15. * 用户通知列表
  16. *
  17. * @param Request $request
  18. * @return NotificationCollection
  19. */
  20. public function index(Request $request)
  21. {
  22. $notifications = Notification::query()
  23. ->filter($request->all())
  24. ->join("notification_records", "notifications.id", "=", "notification_records.notification_id")
  25. ->where("notification_records.user_id", Auth::id())
  26. ->selectRaw("notifications.*,notification_records.read_at")
  27. ->orderByDesc("created_at")
  28. ->paginate();
  29. return new NotificationCollection($notifications);
  30. }
  31. /**
  32. * 标记为已读
  33. *
  34. * @param Request $request
  35. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
  36. */
  37. public function markAsRead(Request $request)
  38. {
  39. $ids = $request->get("ids");
  40. if (! $ids) {
  41. return $this->badRequest("Data is empty");
  42. }
  43. NotificationRecord::query()
  44. ->where("user_id", Auth::id())
  45. ->whereIn("notification_id", $ids)
  46. ->whereNull("read_at")
  47. ->update([
  48. 'read_at' => Carbon::now(),
  49. ]);
  50. return $this->noContent();
  51. }
  52. /**
  53. * 未读消息
  54. *
  55. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
  56. */
  57. public function unread()
  58. {
  59. $announcements = Notification::query()
  60. ->leftJoin("notification_records", "notifications.id", "=", "notification_records.notification_id")
  61. ->where("notification_records.user_id", Auth::id())
  62. ->where("notifications.status", NotificationStatus::RELEASE)
  63. ->where("notifications.start_at", "<", Carbon::now())
  64. ->where(fn($query) => $query->where("notifications.end_at", ">", Carbon::now())->orWhereNull("notifications.end_at"))
  65. ->whereNull("notification_records.id")
  66. ->selectRaw("notifications.id")
  67. ->get();
  68. foreach ($announcements as $announcement) {
  69. NotificationRecord::query()->firstOrCreate([
  70. 'notification_id' => $announcement->id,
  71. 'user_id' => Auth::id()
  72. ]);
  73. }
  74. $notifications = Notification::query()
  75. ->join("notification_records", "notifications.id", "=", "notification_records.notification_id")
  76. ->where("notification_records.user_id", Auth::id())
  77. ->whereNull("notification_records.read_at")
  78. ->selectRaw("notifications.*,notification_records.read_at")
  79. ->orderByDesc("created_at")
  80. ->paginate();
  81. return NotificationResource::collection($notifications);
  82. }
  83. }