Files

133 lines
3.3 KiB
PHP

<?php
namespace app\api\controller\course;
use app\common\controller\Api;
/**
* 评论
*/
class Comment extends Api
{
// 无需登录的接口,*表示全部
protected $noNeedLogin = ['index'];
protected $noNeedRight = '*';
public function _initialize()
{
parent::_initialize();
$this->model = new \app\common\model\course\Comment;
$this->commentLikeModel = new \app\common\model\course\CommentLike;
}
/**
* 获取评论列表
* @return void
*/
public function index(){
$courseId = $this->request->post('course_id');
$limit = input('limit',10);
$page = input('page',1);
$sort = input('sort','time');
$list = $this->model->getList($courseId,[
'limit'=>$limit,
'page'=>$page,
'sort'=>$sort,
'user_id'=>$this->auth->id
]);
$this->success('获取成功', $list);
}
/**
* 添加评论
* @return void
*/
public function add(){
$content = $this->request->post('content');
$courseId = $this->request->post('course_id');
$replyCommentId = $this->request->post('reply_comment_id');
$discussConfig = \app\common\model\config\System::getConfig('discuss');
if($discussConfig['comment_status'] == 'close'){
$this->error("评论功能暂时关闭");
}
if(mb_strlen($content) > 1000){
$this->error("评论内容长度超出限制");
}
if(empty($content)){
$this->error("请完善评论内容");
}
$data = [
'createtime'=>time(),
'content'=>$content,
'course_id'=>$courseId,
'uniacid'=>UNIACID,
'user_id'=>$this->auth->id
];
if($replyCommentId){
$replyCommentData = $this->model->getComment($replyCommentId);
if(!$replyCommentData){
$this->error("回复的评论不存在");
}
$data['reply_comment_id'] = $replyCommentData['id'];
$data['reply_user_id'] = $replyCommentData['user_id'];
}
$successTip = '评论成功';
if($discussConfig['comment_audit'] == 'open'){
$data['status'] = 0;
$successTip = '提交成功,评论将在审核通过后显示';
}
$this->model->save($data);
$this->success($successTip);
}
/**
* 删除评价
* @return void
*/
public function del(){
$commentId = $this->request->post('comment_id');
$commentData = $this->model->getComment($commentId);
if(!$commentData || $commentData['user_id'] != $this->auth->id){
$this->error("删除错误,请重试");
}
$this->model->del($commentData['id']);
$this->success("删除成功");
}
/**
* 喜欢评价
* @return void
*/
public function like(){
$commentId = $this->request->post('comment_id');
$commentData = $this->model->getComment($commentId);
if(!$commentData){
$this->error("获取评论数据失败,请刷新重试");
}
$this->commentLikeModel->setLike($commentId,$this->auth->id);
$this->success("操作成功");
}
}