NotificationController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. /**
  72. * Display the specified resource.
  73. *
  74. * @param Notification $notification
  75. * @return \Illuminate\Http\Response
  76. */
  77. public function show(Notification $notification)
  78. {
  79. //
  80. return $this->ok(new NotificationResource($notification));
  81. }
  82. /**
  83. * Update the specified resource in storage.
  84. *
  85. * @param \Illuminate\Http\Request $request
  86. * @param Notification $notification
  87. * @return \Illuminate\Http\Response
  88. */
  89. public function update(Request $request, Notification $notification)
  90. {
  91. //
  92. $notification->status = $request->get('status','read');
  93. $notification->save();
  94. $unread = Notification::where('to',$notification->to)
  95. ->where('status','unread')
  96. ->count();
  97. return $this->ok(['unread'=>$unread]);
  98. }
  99. /**
  100. * Remove the specified resource from storage.
  101. *
  102. * @param Notification $notification
  103. * @return \Illuminate\Http\Response
  104. */
  105. public function destroy(Notification $notification)
  106. {
  107. //
  108. $notification->delete();
  109. if($notification->trashed()){
  110. return $this->ok('ok');
  111. }else{
  112. return $this->error('fail',500,500);
  113. }
  114. }
  115. }