123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- <?php
- namespace App\Http\Controllers\Goodnight;
- use App\Services\Goodnight\NoticeService;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- use App\Http\Controllers\Miniprogram\Auth;
- use App\Models\Goodnight\CommentModel;
- use App\Models\Goodnight\VoiceModel;
- class CommentController extends Controller
- {
- /**
- * 语音评论列表
- * @param Request $request
- * @param $voice_id
- * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
- */
- public function index(Request $request, $voice_id)
- {
- $voice = VoiceModel::findOrFail($voice_id);
- $builder = CommentModel::where('voice_id', $voice_id)->where('deleted_at', 0);
- $comments = $builder->orderBy('id', 'desc')->paginate($request->get('per_page', 20));
- foreach ($comments as $comment) {
- $comment->user;
- }
- return response([
- 'code' => '200',
- 'data' => $comments,
- ]);
- }
- /**
- * 创建评论
- * @param Request $request
- * @param $voice_id
- * @return \Illuminate\Http\JsonResponse
- * @throws \Tymon\JWTAuth\Exceptions\JWTException
- */
- public function store(Request $request, $voice_id)
- {
- $uid = Auth::auth();
- $this->validate($request, [
- 'content' => 'required|max:200',
- ], [
- 'content.*' => '评论不得超过200个字'
- ]);
- $voice = VoiceModel::findOrFail($voice_id);
- $comment = CommentModel::create($request->merge([
- 'uid' => $uid,
- 'voice_id' => $voice_id
- ])->all());
- // 通知
- try {
- if ($uid != $voice->uid) {
- $ns = new NoticeService();
- $ns->voiceComment($voice->uid, $uid, $voice->id);
- }
- } catch (\Exception $exception) {
- }
- return response([
- 'code' => 200,
- 'data' => $comment
- ]);
- }
- /**
- * 删除留言
- * @param Request $request
- * @param $voice_id
- * @param $comment_id
- * @return \Illuminate\Http\JsonResponse
- * @throws \Tymon\JWTAuth\Exceptions\JWTException
- */
- public function destroy(Request $request, $voice_id, $comment_id)
- {
- $uid = Auth::auth();
- $comment = CommentModel::where('deleted_at', 0)->where('id', $comment_id)->firstOrFail();
- $voice = VoiceModel::findOrFail($voice_id);
- if (!in_array($uid, [$voice->uid, $comment->uid])) {
- return response([
- 'message' => '无权限'
- ], 403001);
- }
- $comment->deleted_at = time();
- $comment->save();
- return response([
- 'code' => 200,
- 'message' => 'OK'
- ]);
- }
- /**
- * 回复留言
- * @param Request $request
- * @param $voice_id
- * @param $comment_id
- * @return \Illuminate\Http\JsonResponse
- * @throws \Tymon\JWTAuth\Exceptions\JWTException
- */
- public function update(Request $request, $voice_id, $comment_id)
- {
- $uid = Auth::auth();
- $this->validate($request, [
- 'reply_content' => 'max:100',
- ], [
- 'reply_content.max' => '回复内容不得超过100个字',
- ], [
- 'reply_content' => '回复内容'
- ]);
- $comment = CommentModel::where('deleted_at', 0)->where('id', $comment_id)->firstOrFail();
- $voice = VoiceModel::findOrFail($voice_id);
- if ($uid != $voice->uid) {
- return response([
- 'code' => 403001,
- 'message' => '无权限'
- ], 403);
- }
- $reply_content = $request->get('reply_content');
- $comment->reply_content = $reply_content;
- $comment->replyed_at = time();
- $comment->save();
- // 通知
- try {
- $ns = new NoticeService();
- $ns->receiveCommentReply($comment->uid, $comment->voice_id, $uid, $reply_content);
- } catch (\Exception $e) {
- app('sentry')->captureException($e);
- }
- return response([
- 'code' => 200,
- 'message' => 'OK'
- ]);
- }
- }
|