CommentController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace App\Http\Controllers\Goodnight;
  3. use App\Services\Goodnight\NoticeService;
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6. use App\Http\Controllers\Miniprogram\Auth;
  7. use App\Models\Goodnight\CommentModel;
  8. use App\Models\Goodnight\VoiceModel;
  9. class CommentController extends Controller
  10. {
  11. /**
  12. * 语音评论列表
  13. * @param Request $request
  14. * @param $voice_id
  15. * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
  16. */
  17. public function index(Request $request, $voice_id)
  18. {
  19. $voice = VoiceModel::findOrFail($voice_id);
  20. $builder = CommentModel::where('voice_id', $voice_id)->where('deleted_at', 0);
  21. $comments = $builder->orderBy('id', 'desc')->paginate($request->get('per_page', 20));
  22. foreach ($comments as $comment) {
  23. $comment->user;
  24. }
  25. return response([
  26. 'code' => '200',
  27. 'data' => $comments,
  28. ]);
  29. }
  30. /**
  31. * 创建评论
  32. * @param Request $request
  33. * @param $voice_id
  34. * @return \Illuminate\Http\JsonResponse
  35. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  36. */
  37. public function store(Request $request, $voice_id)
  38. {
  39. $uid = Auth::auth();
  40. $this->validate($request, [
  41. 'content' => 'required|max:200',
  42. ], [
  43. 'content.*' => '评论不得超过200个字'
  44. ]);
  45. $voice = VoiceModel::findOrFail($voice_id);
  46. $comment = CommentModel::create($request->merge([
  47. 'uid' => $uid,
  48. 'voice_id' => $voice_id
  49. ])->all());
  50. // 通知
  51. try {
  52. if ($uid != $voice->uid) {
  53. $ns = new NoticeService();
  54. $ns->voiceComment($voice->uid, $uid, $voice->id);
  55. }
  56. } catch (\Exception $exception) {
  57. }
  58. return response([
  59. 'code' => 200,
  60. 'data' => $comment
  61. ]);
  62. }
  63. /**
  64. * 删除留言
  65. * @param Request $request
  66. * @param $voice_id
  67. * @param $comment_id
  68. * @return \Illuminate\Http\JsonResponse
  69. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  70. */
  71. public function destroy(Request $request, $voice_id, $comment_id)
  72. {
  73. $uid = Auth::auth();
  74. $comment = CommentModel::where('deleted_at', 0)->where('id', $comment_id)->firstOrFail();
  75. $voice = VoiceModel::findOrFail($voice_id);
  76. if (!in_array($uid, [$voice->uid, $comment->uid])) {
  77. return response([
  78. 'message' => '无权限'
  79. ], 403001);
  80. }
  81. $comment->deleted_at = time();
  82. $comment->save();
  83. return response([
  84. 'code' => 200,
  85. 'message' => 'OK'
  86. ]);
  87. }
  88. /**
  89. * 回复留言
  90. * @param Request $request
  91. * @param $voice_id
  92. * @param $comment_id
  93. * @return \Illuminate\Http\JsonResponse
  94. * @throws \Tymon\JWTAuth\Exceptions\JWTException
  95. */
  96. public function update(Request $request, $voice_id, $comment_id)
  97. {
  98. $uid = Auth::auth();
  99. $this->validate($request, [
  100. 'reply_content' => 'max:100',
  101. ], [
  102. 'reply_content.max' => '回复内容不得超过100个字',
  103. ], [
  104. 'reply_content' => '回复内容'
  105. ]);
  106. $comment = CommentModel::where('deleted_at', 0)->where('id', $comment_id)->firstOrFail();
  107. $voice = VoiceModel::findOrFail($voice_id);
  108. if ($uid != $voice->uid) {
  109. return response([
  110. 'code' => 403001,
  111. 'message' => '无权限'
  112. ], 403);
  113. }
  114. $reply_content = $request->get('reply_content');
  115. $comment->reply_content = $reply_content;
  116. $comment->replyed_at = time();
  117. $comment->save();
  118. // 通知
  119. try {
  120. $ns = new NoticeService();
  121. $ns->receiveCommentReply($comment->uid, $comment->voice_id, $uid, $reply_content);
  122. } catch (\Exception $e) {
  123. app('sentry')->captureException($e);
  124. }
  125. return response([
  126. 'code' => 200,
  127. 'message' => 'OK'
  128. ]);
  129. }
  130. }