NoticeController.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Models\DynamicModel;
  4. use App\Models\UserLikeModel;
  5. use App\Models\UserSystemMessageModel;
  6. use App\Services\NoticeService;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\DB;
  9. class NoticeController extends Controller
  10. {
  11. protected $noticeService;
  12. public function __construct()
  13. {
  14. $this->noticeService = new NoticeService();
  15. }
  16. /**
  17. * 获取消息列表
  18. * @param Request $request
  19. */
  20. public function get_notice_list(Request $request){
  21. try {
  22. $user = auth('api')->user();
  23. $res['notice_list'] = [];
  24. //未读喜欢数量
  25. $res['like_num'] = UserLikeModel::query()->where(["like_id"=>$user->id,'status'=>0])->count();
  26. //未读点赞数量
  27. $res['zan_num'] = DynamicModel::query()->with(["user"])->where('status','=',0)
  28. ->whereHas("user",function($query)use($user){
  29. $query->where('id',$user->id)->select();
  30. })->count();
  31. //未读系统消息
  32. $res['system_num'] = UserSystemMessageModel::query()->where(['user_id'=>$user->id,'status'=>0])->count();
  33. }catch (\Exception $exception){
  34. return $this->response->errorForbidden($exception->getMessage());
  35. }
  36. return response()->json($res);
  37. }
  38. /**
  39. * 获取喜欢列表
  40. * @param Request $request
  41. * @return \Illuminate\Http\JsonResponse|void
  42. */
  43. public function get_like_list(Request $request){
  44. try {
  45. $user = auth('api')->user();
  46. $param['user_id'] = $user->id;
  47. $param['type'] = $request->type;
  48. $res = $this->noticeService->get_like_list($param);
  49. } catch (\Exception $exception){
  50. return $this->response->errorForbidden($exception->getMessage());
  51. }
  52. return response()->json($res);
  53. }
  54. /**
  55. * 获取点赞列表
  56. * @param Request $request
  57. * @return \Illuminate\Http\JsonResponse|void
  58. */
  59. public function get_zan_list(Request $request){
  60. try {
  61. $user = auth('api')->user();
  62. $param['user_id'] = $user->id;
  63. $param['type'] = $request->type;
  64. $res = $this->noticeService->get_zan_list($param);
  65. } catch (\Exception $exception){
  66. return $this->response->errorForbidden($exception->getMessage());
  67. }
  68. return response()->json($res);
  69. }
  70. /**
  71. * 获取系统消息
  72. * @param Request $request
  73. * @return \Illuminate\Http\JsonResponse|void
  74. */
  75. public function get_system_message(Request $request){
  76. try {
  77. $user = auth('api')->user();
  78. $res =UserSystemMessageModel::query()->where('user_id',$user->id)->orderByDesc('id')->paginate(request("perPage",20));
  79. //清除未读状态
  80. UserSystemMessageModel::query()->where(['user_id'=>$user->id,'status'=>0])->update(['status'=>1]);
  81. } catch (\Exception $exception){
  82. return $this->response->errorForbidden($exception->getMessage());
  83. }
  84. return response()->json($res);
  85. }
  86. }