NotificationController.php 3.1 KB

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