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