NotificationController.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. $pageSize=$request->get('page_size') ?? 10;
  23. $notifications = Notification::query()
  24. ->filter($request->all())
  25. ->join("notification_records", "notifications.id", "=", "notification_records.notification_id")
  26. ->where("notification_records.user_id", Auth::id())
  27. ->selectRaw("notifications.*,notification_records.read_at")
  28. ->orderByDesc("created_at")
  29. ->paginate($pageSize);
  30. return new NotificationCollection($notifications);
  31. }
  32. /**
  33. * 标记为已读
  34. *
  35. * @param Request $request
  36. * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Response
  37. */
  38. public function markAsRead(Request $request)
  39. {
  40. $ids = $request->get("ids");
  41. if (! $ids) {
  42. return $this->badRequest("Data is empty");
  43. }
  44. NotificationRecord::query()
  45. ->where("user_id", Auth::id())
  46. ->whereIn("notification_id", $ids)
  47. ->whereNull("read_at")
  48. ->update([
  49. 'read_at' => Carbon::now(),
  50. ]);
  51. return $this->noContent();
  52. }
  53. /**
  54. * 未读消息
  55. *
  56. * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
  57. */
  58. public function unread(Request $request)
  59. {
  60. $pageSize=$request->get('page_size') ?? 10;
  61. $announcements = Notification::query()
  62. ->leftJoin("notification_records", "notifications.id", "=", "notification_records.notification_id")
  63. ->where("notification_records.user_id", Auth::id())
  64. ->where("notifications.status", NotificationStatus::RELEASE)
  65. ->where("notifications.start_at", "<", Carbon::now())
  66. ->where(fn($query) => $query->where("notifications.end_at", ">", Carbon::now())->orWhereNull("notifications.end_at"))
  67. ->whereNull("notification_records.id")
  68. ->selectRaw("notifications.id")
  69. ->get();
  70. foreach ($announcements as $announcement) {
  71. NotificationRecord::query()->firstOrCreate([
  72. 'notification_id' => $announcement->id,
  73. 'user_id' => Auth::id()
  74. ]);
  75. }
  76. $notifications = Notification::query()
  77. ->join("notification_records", "notifications.id", "=", "notification_records.notification_id")
  78. ->where("notification_records.user_id", Auth::id())
  79. ->whereNull("notification_records.read_at")
  80. ->selectRaw("notifications.*,notification_records.read_at")
  81. ->orderByDesc("created_at")
  82. ->paginate($pageSize);
  83. return NotificationResource::collection($notifications);
  84. }
  85. }