NotificationController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Str;
  5. use App\Models\Notification;
  6. use App\Http\Api\AuthApi;
  7. use App\Http\Resources\NotificationResource;
  8. class NotificationController extends Controller
  9. {
  10. /**
  11. * Display a listing of the resource.
  12. *
  13. * @return \Illuminate\Http\Response
  14. */
  15. public function index(Request $request)
  16. {
  17. //
  18. $user = AuthApi::current($request);
  19. if(!$user){
  20. Log::error('notification auth failed {request}',['request'=>$request]);
  21. return $this->error(__('auth.failed'),401,401);
  22. }
  23. switch ($request->get('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->get('status')) );
  32. }
  33. $count = $table->count();
  34. $table = $table->orderBy($request->get('order','created_at'),$request->get('dir','desc'));
  35. $table = $table->skip($request->get("offset",0))
  36. ->take($request->get('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. * Store a newly created resource in storage.
  47. *
  48. * @param \Illuminate\Http\Request $request
  49. * @return \Illuminate\Http\Response
  50. */
  51. public function store(Request $request)
  52. {
  53. //
  54. $user = AuthApi::current($request);
  55. if(!$user){
  56. Log::error('notification auth failed {request}',['request'=>$request]);
  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->get('to');
  63. $new->url = $request->get('url');
  64. $new->content = $request->get('content');
  65. $new->res_type = $request->get('res_type');
  66. $new->res_id = $request->get('res_id');
  67. $new->channel = $request->get('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. 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. * @param Notification $notification
  90. * @return \Illuminate\Http\Response
  91. */
  92. public function show(Notification $notification)
  93. {
  94. //
  95. return $this->ok(new NotificationResource($notification));
  96. }
  97. /**
  98. * Update the specified resource in storage.
  99. *
  100. * @param \Illuminate\Http\Request $request
  101. * @param Notification $notification
  102. * @return \Illuminate\Http\Response
  103. */
  104. public function update(Request $request, Notification $notification)
  105. {
  106. //
  107. $user = AuthApi::current($request);
  108. if(!$user){
  109. return $this->error(__('auth.failed'),401,401);
  110. }
  111. if($notification->to===$user['user_uid']){
  112. $notification->status = $request->get('status','read');
  113. $notification->save();
  114. $unread = Notification::where('to',$notification->to)
  115. ->where('status','unread')
  116. ->count();
  117. return $this->ok(['unread'=>$unread]);
  118. }else{
  119. return $this->error(__('auth.failed'),403,403);
  120. }
  121. }
  122. /**
  123. * Remove the specified resource from storage.
  124. *
  125. * @param Notification $notification
  126. * @return \Illuminate\Http\Response
  127. */
  128. public function destroy(Notification $notification)
  129. {
  130. //
  131. $notification->delete();
  132. if($notification->trashed()){
  133. return $this->ok('ok');
  134. }else{
  135. return $this->error('fail',500,500);
  136. }
  137. }
  138. }