| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- /**
- * Created by PhpStorm.
- * User: wniu
- * Date: 2022/3/18
- * Time: 4:12 下午
- */
- namespace addons\Course\api\modules\v1\controllers;
- use addons\Course\common\logic\Notify;
- use addons\Course\common\models\AddonsCourse;
- use addons\Course\common\models\AddonsCourseChapterLikes;
- use addons\Course\common\models\AddonsCourseDiscuss;
- use addons\Course\common\models\AddonsCourseOrders;
- use api\controllers\OnAuthController;
- use common\enums\StatusEnum;
- class DiscussController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['all-discuss', 'share'];
- /**
- * @name: actionAllDiscuss
- * @msg: 获取某个指定课程下所有的评论
- * @param {*}
- * @return {*}
- */
- public function actionAllDiscuss()
- {
- $get = \Yii::$app->request->get();
- $res = \Yii::$app->services->validate->validate($get, [
- [['course_id'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $discuss_data = AddonsCourseDiscuss::allDiscussList($get['course_id'], $this->mall_id);
- return $this->success('success', $discuss_data);
- }
- /**
- * @name: actionAddDiscuss
- * @msg: 针对某一课程添加评论
- * @param integer cid 课程 id
- * @param string content 评论内容
- * @param integer score 打分(星级)
- * @param integer is_anonymous 是否匿名评论
- * @return {*}
- */
- public function actionAddDiscuss()
- {
- $post = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($post, [
- [['cid', 'is_anonymous', 'content', 'score'], 'required'],
- [['cid', 'cpid', 'score', 'is_anonymous'], 'integer'],
- [['content'], 'string'],
- ['is_anonymous', 'in', 'range' => [0, 1]],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $post['cpid'] = is_null($post['cpid']) ? 0 : $post['cpid'];
- $course_info = AddonsCourse::getOne([['id' => $post['cid']]]);
- if (0 === (int)$course_info->can_comment) {
- return $this->error('该课程不允许评论');
- }
- $post['mall_id'] = $this->mall_id;
- $post['uid'] = $this->user_id;
- $res = AddonsCourseDiscuss::setData($post);
- if ($res === false) return $this->error(AddonsCourseOrders::getStaticError());
- //设置课评论次数 + 1
- if (!$course_info->updateCounters(['comments' => 1])) {
- return $this->error('保存课程评论次数失败');
- }
- return $this->success('提交成功');
- }
- /**
- * 课程分享
- * @Author: guyu
- * @DateTime: 2021-03-22 14:53
- * @return mixed
- */
- public function actionShare()
- {
- $get = \Yii::$app->request->get();
- $res = \Yii::$app->services->validate->validate($get, [
- [['cid'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $course_info = AddonsCourse::getOne([['id' => $get['cid'], 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]]);
- if (empty($course_info)) {
- return $this->error("课程不存在或已下架");
- }
- //设置课程视频分享次数 + 1
- $course_res = $course_info->updateCounters(['shares' => 1]);
- if (true === $course_res) {
- return $this->success('课程分享成功');
- } else {
- return $this->error("课程分享失败!");
- }
- }
- /**
- * @name: actionThumbs
- * @msg: 对课程点赞 / 取消点赞
- * @param $id integer 课程 id
- * @param $star_status integer 点赞状态,1:点赞;0:取消点赞
- * @return {*}
- */
- public function actionThumbs()
- {
- $post = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($post, [
- [['course_id'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $post['user_id'] = $this->user_id;
- $res = AddonsCourseChapterLikes::setData($this->mall_id, $post);
- if ($res === false) return $this->error(AddonsCourseOrders::getStaticError());
- return $this->success('操作成功', $res);
- }
- }
|