article_id; $model = Comment::uniacid()->where(['article_id' => $article_id, 'comment_id' => 0]); $enabled_review = \Setting::get('plugin.article.enabled_review'); if($enabled_review){ $model = $model->where('is_pass', 1); } $num = $model->count(); $model = $model->with(['hasManyReply' => function ($query) use ($enabled_review) { if($enabled_review){ $query->where('is_pass', 1); } }]) ->orderBy('created_at', 'desc') ->orderBy('id','desc') ->paginate('', ['*'], '', (int)request()->page ?: 1); $model = $model->toArray(); $model['data'] = collect($model['data'])->map(function ($item) { $num = count($item['has_many_reply']); unset($item['has_many_reply']); $collection = collect($item) ->put('reply_num', $num); return $collection; }); $member = Member::where('uid',\YunShop::app()->getMemberId()) ->select(['uid','avatar','nickname'])->first(); return $this->successJson('ok', ['list'=>$model,'member' => $member,'num' => $num]); } /** * @return \Illuminate\Http\JsonResponse * 查看回复 */ public function getReplys() { $comment_id = \YunShop::request()->comment_id; $comment = Comment::uniacid()->find($comment_id); if(!$comment || $comment->comment_id != 0) { return $this->errorJson('未检测到数据!'); } $model = Comment::uniacid(); if(\Setting::get('plugin.article.enabled_review')){ $model = $model->where('is_pass', 1); } $result = $model->where('comment_id',$comment_id) ->orderBy('created_at', 'desc') ->orderBy('id','desc') ->paginate('', ['*'], '', (int)request()->page ?: 1); return $this->successJson('ok', $result); } //创建评论 public function createComment() { $commentModel = new Comment(); $ingress = request()->ingress; $member = Member::getUserInfos(\YunShop::app()->getMemberId())->first(); if (!$member) { return $this->errorJson('评论失败!未检测到会员数据!'); } $commentStatus = '1'; $comment = [ 'article_id' => \YunShop::request()->article_id, 'content' => \YunShop::request()->content, ]; if ($ingress && !empty($comment['content'])) { $check_result = (new MiniFileLimitService())->checkMsg($comment['content']); if ($check_result['errcode'] != 0) { return $this->errorJson('输入信息含有违法违规内容'); } } if (!$comment['article_id']) { return $this->errorJson('评论失败!未检测到文章ID!'); } if (!$comment['content']) { return $this->errorJson('评论失败!未检测到评论内容!'); } if (\YunShop::request()->images) { $comment['images'] = json_decode(\YunShop::request()->images); if (is_array($comment['images'])) { if (count($comment['images']) > 5) { return $this->errorJson('追加评论失败!评论图片不能多于5张!'); } $comment['images'] = serialize($comment['images']); } else { return $this->errorJson('追加评论失败!评论图片数据不正确!'); } } else { $comment['images'] = serialize([]); } $commentModel->setRawAttributes($comment); $commentModel->uniacid = \YunShop::app()->uniacid; $manager = Manager::uniacid()->where('member_id', $member->uid)->first(); if ($manager && $manager->status == 1) { $commentModel->manager_id = $manager->id; $commentModel->nick_name = $manager->nickname; } else { $commentModel->nick_name = $member->nickname; } $commentModel->uid = $member->uid; $commentModel->head_img_url = $member->avatar; $commentModel->type = '1'; $commentModel->is_pass = $commentModel->isPass(); $res = $this->insertComment($commentModel, $commentStatus); return $res; } //回复评论 public function replyComment() { $commentModel = new Comment(); $member = Member::getUserInfos(\YunShop::app()->getMemberId())->first(); if (!$member) { return $this->errorJson('回复评论失败!未检测到会员数据!'); } $id = \YunShop::request()->comment_id; $reply = $commentModel::uniacid()->find($id); if (!$reply) { return $this->errorJson('回复评论失败!未检测到评论数据!'); } $comment = [ 'article_id' => $reply->article_id, 'content' => \YunShop::request()->content, 'comment_id' => $reply->comment_id ?: $reply->id, ]; if (!$comment['content']) { return $this->errorJson('回复评论失败!未检测到评论内容!'); } if (\YunShop::request()->images) { $comment['images'] = json_decode(\YunShop::request()->images); if (is_array($comment['images'])) { if (count($comment['images']) > 5) { return $this->errorJson('追加评论失败!评论图片不能多于5张!'); } $comment['images'] = serialize($comment['images']); } else { return $this->errorJson('追加评论失败!评论图片数据不正确!'); } } else { $comment['images'] = serialize([]); } $commentModel->setRawAttributes($comment); $commentModel->uniacid = \YunShop::app()->uniacid; $commentModel->uid = $member->uid; $manager = Manager::uniacid()->where('member_id', $member->uid)->first(); if ($manager && $manager->status == 1) { $commentModel->manager_id = $manager->id; $commentModel->nick_name = $manager->nickname; } else { $commentModel->nick_name = $member->nickname; } $commentModel->head_img_url = $member->avatar; if ($reply->comment_id) { $commentModel->reply_id = $reply->uid; $commentModel->reply_name = $reply->nick_name; }else{ $commentModel->reply_id = 0; $commentModel->reply_name = null; } $commentModel->reply_comment_id = $id; $commentModel->type = '2'; $commentModel->is_pass = $commentModel->isPass(); return $this->insertComment($commentModel); } public function del() { //主评论不关联删除子评论。没人有权删除别人的评论,一直存在 $id = \YunShop::request()->comment_id; $comment = Comment::uniacid()->find($id); if(!$comment || $comment->uid != \YunShop::app()->getMemberId()) { return $this->errorJson('评论不存在'); } $comment->delete(); return $this->successJson('ok', '删除成功'); } private function insertComment($commentModel, $commentStatus = '') { $validator = $commentModel->validator($commentModel->getAttributes()); if ($validator->fails()) { //检测失败 return $this->errorJson($validator->messages()); } else { //数据保存 if ($commentModel->save()) { return $this->successJson('评论成功!', $commentModel); } else { return $this->errorJson('评论失败!'); } } } }