$commentId ])->find(); return $data; } /** * 获取课程的评论数量 * @param $course * @return mixed */ public function getCourseCommentCount($course){ return self::where([ 'course_id'=>$course, 'status'=>1 ])->count(); } public function user() { return $this->belongsTo('app\common\model\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0); } public function course() { return $this->belongsTo('app\admin\model\Course', 'course_id', 'id', [], 'LEFT')->setEagerlyType(0); } /** * 自关联 * @return mixed */ public function like() { return $this->hasMany('comment_like'); } /** * 获取评论列表 * @param $courseId 课程ID * @param $params 参数 * @return array */ public function getList($courseId = 0,$params = []){ $commentLikeModel = new CommentLike(); $where = [ 'comment.status'=>1, 'comment.uniacid'=>UNIACID ]; if($courseId){ $where['comment.course_id'] = $courseId; } if(isset($params['sort']) && !empty($params['sort'])){ if($params['sort']=='time'){ $orderField = 'comment.createtime'; }else{ $orderField = 'like_count'; } } $data = []; $list = self::with(['user']) ->alias("comment") ->join($commentLikeModel->name." like","comment.id=like.comment_id","left") ->where($where) ->limit($params['limit'])->page($params['page']) ->order($orderField,'desc') ->group("comment.id") ->field(["count(like.id) as like_count","comment.content","comment.createtime","comment.id","comment.course_id"]) ->select(); if(!empty($list)){ foreach ($list as $item){ if(!empty($item['reply_comment_id'])){ $item['reply'] = self::with(['user'])->where([ 'comment.id'=>$item['reply_comment_id'], 'comment.status'=>1 ])->find(); } if(isset($item['reply']) && !empty($item['reply'])){ $item['reply']['user']->visible(["nickname","avatar"]); $item['reply'] = $item['reply']->visible(["content","createtime","id","user"]); } $item['content'] = str_replace("\n","
",$item['content']); $item['user'] = $item['user']->visible(["nickname","avatar"]); $item['islike'] = false; $item['candel'] = false; if(isset($params['user_id']) && $params['user_id']){ $item['islike'] = $commentLikeModel->getIsLike($item['id'],$params['user_id']); $item['candel'] = $params['user_id']==$item['user_id']; } $item->user->avatar = cdnurl($item->user->avatar); if($item->user_id==0){ $item->user->nickname = '管理员'; $item->user->avatar = '/assets/logo.png'; } $data[] = $item; } } return $data; } /** * 删除评论 * @param $commentId * @return void */ public function del($commentId){ self::where([ 'id'=>$commentId ])->delete(); CommentLike::where([ 'comment_id'=>$commentId ])->delete(); return true; } }