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 . '成功'); } } }