12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\model\Comment;
- use app\common\service\ScoreSend;
- use think\Db;
- /**
- * 评论模块
- */
- class CommentController extends Api
- {
- protected $noNeedLogin = ['index'];
- protected $noNeedRight=['post'];
- /**
- * 获取评论
- * @ApiParams (name=commentable_type,description="评论类型,video:评论")
- * @ApiParams (name=commentable_id,description="评论类型id")
- * @ApiReturnParams (name=body,description="评论内容")
- * @ApiReturnParams (name=created_at,description="发布时间")
- * @ApiReturnParams (name=user[nickname],description="用户昵称")
- * @ApiReturnParams (name=user[avatar],description="用户头像")
- */
- public function index(){
- $data=input();
- $this->validate($data,[
- 'commentable_type'=>['require'],
- 'commentable_id'=>['require'],
- ]);
- $comments=Comment::order('id','desc')
- ->with(['user'])
- ->where('commentable_type',$data['commentable_type'])
- ->where('commentable_id',$data['commentable_id'])
- ->paginate($data['limit']??10);
- $this->success('',$comments);
- }
- /**
- * 发表评论
- * @ApiParams (name=body,description=内容)
- * @ApiParams (name=commentable_type,description="评论类型,info:资讯,video:视频")
- * @ApiParams (name=commentable_id,description="评论类型id")
- * @ApiReturnParams (name=score,description="获得的积分数量")
- */
- public function post(ScoreSend $scoreSend){
- $data=input();
- $user=$this->auth->getUser();
- $this->validate($data,[
- 'body|评论'=>['require','max:200'],
- 'commentable_type'=>['require','in:'.implode(',',array_keys(Comment::$alias))],
- 'commentable_id'=>['require'],
- ]);
- $finally=[
- 'score'=>0,
- ];
- Db::startTrans();
- model($data['commentable_type'])->findOrFail($data['commentable_id']);
- $user->comments()->save($data);
- if($data['commentable_type']=='video'){
- $finally['score']=$scoreSend
- ->setUser($user)
- ->setObject(['video',$data['commentable_id']])
- ->setField('score')
- ->setMemo('评论送积分')
- ->setConfig('score_commentVideo')
- ->onlyOne();
- }
- Db::commit();
- $this->success('',$finally);
- }
- }
|