| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace addons\ShortVideo\api\modules\v1\controllers;
- use addons\ShortVideo\common\enums\AddonsShortVideoEnum;
- use addons\ShortVideo\common\models\ShortVideoComment;
- use addons\ShortVideo\common\models\ShortVideoConfig;
- use addons\ShortVideo\common\models\ShortVideoTag;
- use Yii;
- use api\controllers\OnAuthController;
- use common\components\sensitivewords\SensitiveWords;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use yii\caching\ArrayCache;
- /**
- * 视频评论控制器
- *
- * Class DefaultController
- * @package addons\ShortVideo\api\modules\v1\controllers
- */
- class CommentController extends BaseController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = [];
- /**
- * @name:
- * @msg: 评论列表
- * @param {integer} $video_id
- * @return {*}
- */
- public function actionList()
- {
- if (!\Yii::$app->request->isGet) {
- return $this->error('请求方式错误');
- }
- $video_id = \Yii::$app->request->get('video_id');
- $page = \Yii::$app->request->get('page', 1);
- $limit = \Yii::$app->request->get('limit', $this->pageSize);
- if (empty($video_id)) return $this->error('参数错误');
- $wheres[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'video_id' => $video_id];
- $params = [
- 'select' => 'id,user_id,video_id,content,like_num,created_at',
- 'where' => $wheres,
- 'order' => 'is_top desc,created_at desc,like_num desc,id desc',
- 'page' => $page,
- 'limit' => $limit,
- ];
- $comment_list = ShortVideoComment::listPage($params);
- if (empty($comment_list['list'])) {
- if (is_array($comment_list['list'])) {
- return $this->success('success', $comment_list);
- } else {
- return $this->error(ShortVideoComment::getStaticError());
- }
- }
- $user_ids = array_column($comment_list['list'], 'user_id');
- $users = User::find()
- ->select('id,nickname,mobile,username,avatar_url')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $user_ids])
- ->asArray()
- ->indexBy('id')
- ->all();
- foreach ($comment_list['list'] as &$comment) {
- $user = $users[$comment['user_id']] ?? [];
- $comment['nickname'] = empty($user['nickname']) ? '默认会员' : ($user['nickname'] ?? '默认会员');
- $comment['avatar_url'] = $user['avatar_url'] ?? \Yii::$app->request->hostInfo . '/resources/img/common/default-avatar.png';
- $comment['is_praise'] = $comment['like_num'] > 0 ? 1 : 0;
- }
- return $this->success('success', $comment_list);
- }
- /**
- * @name:
- * @msg: 添加评论
- * @param {integer} $video_id 视频id
- * @param {string} $content 评论内容
- * @return {*}
- */
- public function actionCreate()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $post = \Yii::$app->request->post();
- $rules = [
- [['video_id', 'content'], 'required'],
- ['content', 'string', 'max' => 255],
- ['video_id', 'integer'],
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $configs = ShortVideoConfig::getConfig($this->mall_id);
- if ($configs['client_status'] == AddonsShortVideoEnum::CLIENT_STATUS1) {
- // 开启敏感词检查
- $sensitive = new SensitiveWords();
- $textStatus = $sensitive->getTextStatus($post['content'], $this->mall_id);
- if ($textStatus['status'] != 0) {
- return $this->error('评论内容包含敏感词汇:' . $textStatus['msg']);
- }
- }
- $params = [
- 'video_id' => $post['video_id'],
- 'content' => $post['content'],
- 'user_id' => $this->user_id,
- ];
- $res = ShortVideoComment::setData($this->mall_id, $params);
- if (false === $res) {
- return $this->error('评论失败:' . ShortVideoTag::getStaticError());
- } else {
- return $this->success('评论成功');
- }
- }
- /**
- * @name:
- * @msg: 点赞评论
- * @param {integer} $comment_id 评论id
- * @param {string} $type 操作类型,praise:点赞;unpraise:取消点赞
- * @return {*}
- */
- public function actionCommentLike()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $post = \Yii::$app->request->post();
- $rules = [
- [['comment_id', 'type'], 'required'],
- ['comment_id', 'integer'],
- ['type', 'in', 'range' => ['praise', 'unpraise']],
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $params = [
- 'user_id' => $this->user_id,
- 'comment_id' => $post['comment_id'],
- 'type' => $post['type'],
- ];
- if ('praise' == $post['type']) {
- $action = '点赞';
- } else {
- $action = '取消点赞';
- }
- $res = ShortVideoComment::commentLike($this->mall_id, $params);
- if (false === $res) {
- return $this->error($action . '失败:' . ShortVideoTag::getStaticError());
- } else {
- return $this->success($action . '成功');
- }
- }
- }
|