NotificationController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Str;
  5. use Illuminate\Support\Facades\Log;
  6. use App\Models\Notification;
  7. use App\Services\AuthService;
  8. use App\Http\Resources\NotificationResource;
  9. class NotificationController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return \Illuminate\Http\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. * @param \Illuminate\Http\Request $request
  50. * @return \Illuminate\Http\Response
  51. */
  52. public function store(Request $request)
  53. {
  54. //
  55. $user = AuthService::current($request);
  56. if (!$user) {
  57. return $this->error(__('auth.failed'), 401, 401);
  58. }
  59. $new = new Notification;
  60. $new->id = Str::uuid();
  61. $new->from = $user['user_uid'];
  62. $new->to = $request->input('to');
  63. $new->url = $request->input('url');
  64. $new->content = $request->input('content');
  65. $new->res_type = $request->input('res_type');
  66. $new->res_id = $request->input('res_id');
  67. $new->channel = $request->input('channel');
  68. $new->save();
  69. return $this->ok(new NotificationResource($new));
  70. }
  71. public static function insert($from, $to, $res_type, $res_id, $channel)
  72. {
  73. foreach ($to as $key => $one) {
  74. $new = new Notification;
  75. $new->id = Str::uuid();
  76. $new->from = $from;
  77. $new->to = $one;
  78. $new->url = '';
  79. $new->content = '';
  80. $new->res_type = $res_type;
  81. $new->res_id = $res_id;
  82. $new->channel = $channel;
  83. $new->save();
  84. }
  85. return count($to);
  86. }
  87. /**
  88. * Display the specified resource.
  89. *
  90. * @param Notification $notification
  91. * @return \Illuminate\Http\Response
  92. */
  93. public function show(Notification $notification)
  94. {
  95. //
  96. return $this->ok(new NotificationResource($notification));
  97. }
  98. /**
  99. * Update the specified resource in storage.
  100. *
  101. * @param \Illuminate\Http\Request $request
  102. * @param Notification $notification
  103. * @return \Illuminate\Http\Response
  104. */
  105. public function update(Request $request, Notification $notification)
  106. {
  107. //
  108. $user = AuthService::current($request);
  109. if (!$user) {
  110. return $this->error(__('auth.failed'), 401, 401);
  111. }
  112. if ($notification->to === $user['user_uid']) {
  113. $notification->status = $request->input('status', 'read');
  114. $notification->save();
  115. $unread = Notification::where('to', $notification->to)
  116. ->where('status', 'unread')
  117. ->count();
  118. return $this->ok(['unread' => $unread]);
  119. } else {
  120. return $this->error(__('auth.failed'), 403, 403);
  121. }
  122. }
  123. /**
  124. * Remove the specified resource from storage.
  125. *
  126. * @param Notification $notification
  127. * @return \Illuminate\Http\Response
  128. */
  129. public function destroy(Notification $notification)
  130. {
  131. //
  132. $notification->delete();
  133. if ($notification->trashed()) {
  134. return $this->ok('ok');
  135. } else {
  136. return $this->error('fail', 500, 500);
  137. }
  138. }
  139. }