2
0

NotificationController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Resources\NotificationResource;
  4. use App\Models\Notification;
  5. use App\Services\AuthService;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Http\Response;
  8. use Illuminate\Support\Str;
  9. class NotificationController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return Response
  15. */
  16. public function index(Request $request)
  17. {
  18. //
  19. $user = AuthService::current($request);
  20. if (! $user) {
  21. return $this->error(__('auth.failed'), 401, 401);
  22. }
  23. switch ($request->input('view')) {
  24. case 'to':
  25. $table = Notification::where('to', $user['user_uid']);
  26. $unread = Notification::where('to', $user['user_uid'])
  27. ->where('status', 'unread')->count();
  28. break;
  29. }
  30. if ($request->has('status')) {
  31. $table = $table->whereIn('status', explode(',', $request->input('status')));
  32. }
  33. $count = $table->count();
  34. $table = $table->orderBy($request->input('order', 'created_at'), $request->input('dir', 'desc'));
  35. $table = $table->skip($request->input('offset', 0))
  36. ->take($request->input('limit', 10));
  37. $result = $table->get();
  38. return $this->ok(
  39. [
  40. 'rows' => NotificationResource::collection($result),
  41. 'count' => $count,
  42. 'unread' => $unread,
  43. ]
  44. );
  45. }
  46. /**
  47. * Store a newly created resource in storage.
  48. *
  49. * @return Response
  50. */
  51. public function store(Request $request)
  52. {
  53. //
  54. $user = AuthService::current($request);
  55. if (! $user) {
  56. return $this->error(__('auth.failed'), 401, 401);
  57. }
  58. $new = new Notification;
  59. $new->id = Str::uuid();
  60. $new->from = $user['user_uid'];
  61. $new->to = $request->input('to');
  62. $new->url = $request->input('url');
  63. $new->content = $request->input('content');
  64. $new->res_type = $request->input('res_type');
  65. $new->res_id = $request->input('res_id');
  66. $new->channel = $request->input('channel');
  67. $new->save();
  68. return $this->ok(new NotificationResource($new));
  69. }
  70. public static function insert($from, $to, $res_type, $res_id, $channel)
  71. {
  72. foreach ($to as $key => $one) {
  73. $new = new Notification;
  74. $new->id = Str::uuid();
  75. $new->from = $from;
  76. $new->to = $one;
  77. $new->url = '';
  78. $new->content = '';
  79. $new->res_type = $res_type;
  80. $new->res_id = $res_id;
  81. $new->channel = $channel;
  82. $new->save();
  83. }
  84. return count($to);
  85. }
  86. /**
  87. * Display the specified resource.
  88. *
  89. * @return Response
  90. */
  91. public function show(Notification $notification)
  92. {
  93. //
  94. return $this->ok(new NotificationResource($notification));
  95. }
  96. /**
  97. * Update the specified resource in storage.
  98. *
  99. * @return Response
  100. */
  101. public function update(Request $request, Notification $notification)
  102. {
  103. //
  104. $user = AuthService::current($request);
  105. if (! $user) {
  106. return $this->error(__('auth.failed'), 401, 401);
  107. }
  108. if ($notification->to === $user['user_uid']) {
  109. $notification->status = $request->input('status', 'read');
  110. $notification->save();
  111. $unread = Notification::where('to', $notification->to)
  112. ->where('status', 'unread')
  113. ->count();
  114. return $this->ok(['unread' => $unread]);
  115. } else {
  116. return $this->error(__('auth.failed'), 403, 403);
  117. }
  118. }
  119. /**
  120. * Remove the specified resource from storage.
  121. *
  122. * @return Response
  123. */
  124. public function destroy(Notification $notification)
  125. {
  126. //
  127. $notification->delete();
  128. if ($notification->trashed()) {
  129. return $this->ok('ok');
  130. } else {
  131. return $this->error('fail', 500, 500);
  132. }
  133. }
  134. }