CommentController.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\Comment;
  5. use app\common\service\ScoreSend;
  6. use think\Db;
  7. /**
  8. * 评论模块
  9. */
  10. class CommentController extends Api
  11. {
  12. protected $noNeedLogin = ['index'];
  13. protected $noNeedRight=['post'];
  14. /**
  15. * 获取评论
  16. * @ApiParams (name=commentable_type,description="评论类型,video:评论")
  17. * @ApiParams (name=commentable_id,description="评论类型id")
  18. * @ApiReturnParams (name=body,description="评论内容")
  19. * @ApiReturnParams (name=created_at,description="发布时间")
  20. * @ApiReturnParams (name=user[nickname],description="用户昵称")
  21. * @ApiReturnParams (name=user[avatar],description="用户头像")
  22. */
  23. public function index(){
  24. $data=input();
  25. $this->validate($data,[
  26. 'commentable_type'=>['require'],
  27. 'commentable_id'=>['require'],
  28. ]);
  29. $comments=Comment::order('id','desc')
  30. ->with(['user'])
  31. ->where('commentable_type',$data['commentable_type'])
  32. ->where('commentable_id',$data['commentable_id'])
  33. ->paginate($data['limit']??10);
  34. $this->success('',$comments);
  35. }
  36. /**
  37. * 发表评论
  38. * @ApiParams (name=body,description=内容)
  39. * @ApiParams (name=commentable_type,description="评论类型,info:资讯,video:视频")
  40. * @ApiParams (name=commentable_id,description="评论类型id")
  41. * @ApiReturnParams (name=score,description="获得的积分数量")
  42. */
  43. public function post(ScoreSend $scoreSend){
  44. $data=input();
  45. $user=$this->auth->getUser();
  46. $this->validate($data,[
  47. 'body|评论'=>['require','max:200'],
  48. 'commentable_type'=>['require','in:'.implode(',',array_keys(Comment::$alias))],
  49. 'commentable_id'=>['require'],
  50. ]);
  51. $finally=[
  52. 'score'=>0,
  53. ];
  54. Db::startTrans();
  55. model($data['commentable_type'])->findOrFail($data['commentable_id']);
  56. $user->comments()->save($data);
  57. if($data['commentable_type']=='video'){
  58. $finally['score']=$scoreSend
  59. ->setUser($user)
  60. ->setObject(['video',$data['commentable_id']])
  61. ->setField('score')
  62. ->setMemo('评论送积分')
  63. ->setConfig('score_commentVideo')
  64. ->onlyOne();
  65. }
  66. Db::commit();
  67. $this->success('',$finally);
  68. }
  69. }