NoticeController.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /**
  87. * 删除系统消息
  88. */
  89. public function del_system_message(Request $request){
  90. try {
  91. $this->noticeService->del_message($request->id);
  92. } catch (\Exception $exception){
  93. return $this->response->errorForbidden($exception->getMessage());
  94. }
  95. return response()->json(['message'=>"操作成功"]);
  96. }
  97. }