CommentController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace addons\ShortVideo\api\modules\v1\controllers;
  3. use addons\ShortVideo\common\enums\AddonsShortVideoEnum;
  4. use addons\ShortVideo\common\models\ShortVideoComment;
  5. use addons\ShortVideo\common\models\ShortVideoConfig;
  6. use addons\ShortVideo\common\models\ShortVideoTag;
  7. use Yii;
  8. use api\controllers\OnAuthController;
  9. use common\components\sensitivewords\SensitiveWords;
  10. use common\enums\StatusEnum;
  11. use common\models\user\User;
  12. use yii\caching\ArrayCache;
  13. /**
  14. * 视频评论控制器
  15. *
  16. * Class DefaultController
  17. * @package addons\ShortVideo\api\modules\v1\controllers
  18. */
  19. class CommentController extends BaseController
  20. {
  21. public $modelClass = '';
  22. /**
  23. * 不用进行登录验证的方法
  24. *
  25. * 例如: ['index', 'update', 'create', 'view', 'delete']
  26. * 默认全部需要验证
  27. *
  28. * @var array
  29. */
  30. protected $authOptional = [];
  31. /**
  32. * @name:
  33. * @msg: 评论列表
  34. * @param {integer} $video_id
  35. * @return {*}
  36. */
  37. public function actionList()
  38. {
  39. if (!\Yii::$app->request->isGet) {
  40. return $this->error('请求方式错误');
  41. }
  42. $video_id = \Yii::$app->request->get('video_id');
  43. $page = \Yii::$app->request->get('page', 1);
  44. $limit = \Yii::$app->request->get('limit', $this->pageSize);
  45. if (empty($video_id)) return $this->error('参数错误');
  46. $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'video_id' => $video_id];
  47. $params = [
  48. 'select' => 'id,user_id,video_id,content,like_num,created_at',
  49. 'where' => $wheres,
  50. 'order' => 'is_top desc,created_at desc,like_num desc,id desc',
  51. 'page' => $page,
  52. 'limit' => $limit,
  53. ];
  54. $comment_list = ShortVideoComment::listPage($params);
  55. if (empty($comment_list['list'])) {
  56. if (is_array($comment_list['list'])) {
  57. return $this->success('success', $comment_list);
  58. } else {
  59. return $this->error(ShortVideoComment::getStaticError());
  60. }
  61. }
  62. $user_ids = array_column($comment_list['list'], 'user_id');
  63. $users = User::find()
  64. ->select('id,nickname,mobile,username,avatar_url')
  65. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_ids])
  66. ->asArray()
  67. ->indexBy('id')
  68. ->all();
  69. foreach ($comment_list['list'] as &$comment) {
  70. $user = $users[$comment['user_id']] ?? [];
  71. $comment['nickname'] = empty($user['nickname']) ? '默认会员' : ($user['nickname'] ?? '默认会员');
  72. $comment['avatar_url'] = $user['avatar_url'] ?? \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
  73. $comment['is_praise'] = $comment['like_num'] > 0 ? 1 : 0;
  74. }
  75. return $this->success('success', $comment_list);
  76. }
  77. /**
  78. * @name:
  79. * @msg: 添加评论
  80. * @param {integer} $video_id 视频id
  81. * @param {string} $content 评论内容
  82. * @return {*}
  83. */
  84. public function actionCreate()
  85. {
  86. if (!\Yii::$app->request->isPost) {
  87. return $this->error('请求方式错误');
  88. }
  89. $post = \Yii::$app->request->post();
  90. $rules = [
  91. [['video_id', 'content'], 'required'],
  92. ['content', 'string', 'max' => 255],
  93. ['video_id', 'integer'],
  94. ];
  95. $res = \Yii::$app->services->validate->validate($post, $rules);
  96. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  97. $configs = ShortVideoConfig::getConfig($this->mall_id);
  98. if ($configs['client_status'] == AddonsShortVideoEnum::CLIENT_STATUS1) {
  99. // 开启敏感词检查
  100. $sensitive = new SensitiveWords();
  101. $textStatus = $sensitive->getTextStatus($post['content'], $this->mall_id);
  102. if ($textStatus['status'] != 0) {
  103. return $this->error('评论内容包含敏感词汇:' . $textStatus['msg']);
  104. }
  105. }
  106. $params = [
  107. 'video_id' => $post['video_id'],
  108. 'content' => $post['content'],
  109. 'user_id' => $this->user_id,
  110. ];
  111. $res = ShortVideoComment::setData($this->mall_id, $params);
  112. if (false === $res) {
  113. return $this->error('评论失败:' . ShortVideoTag::getStaticError());
  114. } else {
  115. return $this->success('评论成功');
  116. }
  117. }
  118. /**
  119. * @name:
  120. * @msg: 点赞评论
  121. * @param {integer} $comment_id 评论id
  122. * @param {string} $type 操作类型,praise:点赞;unpraise:取消点赞
  123. * @return {*}
  124. */
  125. public function actionCommentLike()
  126. {
  127. if (!\Yii::$app->request->isPost) {
  128. return $this->error('请求方式错误');
  129. }
  130. $post = \Yii::$app->request->post();
  131. $rules = [
  132. [['comment_id', 'type'], 'required'],
  133. ['comment_id', 'integer'],
  134. ['type', 'in', 'range' => ['praise', 'unpraise']],
  135. ];
  136. $res = \Yii::$app->services->validate->validate($post, $rules);
  137. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  138. $params = [
  139. 'user_id' => $this->user_id,
  140. 'comment_id' => $post['comment_id'],
  141. 'type' => $post['type'],
  142. ];
  143. if ('praise' == $post['type']) {
  144. $action = '点赞';
  145. } else {
  146. $action = '取消点赞';
  147. }
  148. $res = ShortVideoComment::commentLike($this->mall_id, $params);
  149. if (false === $res) {
  150. return $this->error($action . '失败:' . ShortVideoTag::getStaticError());
  151. } else {
  152. return $this->success($action . '成功');
  153. }
  154. }
  155. }