DiscussController.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wniu
  5. * Date: 2022/3/18
  6. * Time: 4:12 下午
  7. */
  8. namespace addons\Course\api\modules\v1\controllers;
  9. use addons\Course\common\logic\Notify;
  10. use addons\Course\common\models\AddonsCourse;
  11. use addons\Course\common\models\AddonsCourseChapterLikes;
  12. use addons\Course\common\models\AddonsCourseDiscuss;
  13. use addons\Course\common\models\AddonsCourseOrders;
  14. use api\controllers\OnAuthController;
  15. use common\enums\StatusEnum;
  16. class DiscussController extends OnAuthController
  17. {
  18. public $modelClass = '';
  19. /**
  20. * 不用进行登录验证的方法
  21. *
  22. * 例如: ['index', 'update', 'create', 'view', 'delete']
  23. * 默认全部需要验证
  24. *
  25. * @var array
  26. */
  27. protected $authOptional = ['all-discuss', 'share'];
  28. /**
  29. * @name: actionAllDiscuss
  30. * @msg: 获取某个指定课程下所有的评论
  31. * @param {*}
  32. * @return {*}
  33. */
  34. public function actionAllDiscuss()
  35. {
  36. $get = \Yii::$app->request->get();
  37. $res = \Yii::$app->services->validate->validate($get, [
  38. [['course_id'], 'required'],
  39. ]);
  40. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  41. $discuss_data = AddonsCourseDiscuss::allDiscussList($get['course_id'], $this->mall_id);
  42. return $this->success('success', $discuss_data);
  43. }
  44. /**
  45. * @name: actionAddDiscuss
  46. * @msg: 针对某一课程添加评论
  47. * @param integer cid 课程 id
  48. * @param string content 评论内容
  49. * @param integer score 打分(星级)
  50. * @param integer is_anonymous 是否匿名评论
  51. * @return {*}
  52. */
  53. public function actionAddDiscuss()
  54. {
  55. $post = \Yii::$app->request->post();
  56. $res = \Yii::$app->services->validate->validate($post, [
  57. [['cid', 'is_anonymous', 'content', 'score'], 'required'],
  58. [['cid', 'cpid', 'score', 'is_anonymous'], 'integer'],
  59. [['content'], 'string'],
  60. ['is_anonymous', 'in', 'range' => [0, 1]],
  61. ]);
  62. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  63. $post['cpid'] = is_null($post['cpid']) ? 0 : $post['cpid'];
  64. $course_info = AddonsCourse::getOne([['id' => $post['cid']]]);
  65. if (0 === (int)$course_info->can_comment) {
  66. return $this->error('该课程不允许评论');
  67. }
  68. $post['mall_id'] = $this->mall_id;
  69. $post['uid'] = $this->user_id;
  70. $res = AddonsCourseDiscuss::setData($post);
  71. if ($res === false) return $this->error(AddonsCourseOrders::getStaticError());
  72. //设置课评论次数 + 1
  73. if (!$course_info->updateCounters(['comments' => 1])) {
  74. return $this->error('保存课程评论次数失败');
  75. }
  76. return $this->success('提交成功');
  77. }
  78. /**
  79. * 课程分享
  80. * @Author: guyu
  81. * @DateTime: 2021-03-22 14:53
  82. * @return mixed
  83. */
  84. public function actionShare()
  85. {
  86. $get = \Yii::$app->request->get();
  87. $res = \Yii::$app->services->validate->validate($get, [
  88. [['cid'], 'required'],
  89. ]);
  90. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  91. $course_info = AddonsCourse::getOne([['id' => $get['cid'], 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]]);
  92. if (empty($course_info)) {
  93. return $this->error("课程不存在或已下架");
  94. }
  95. //设置课程视频分享次数 + 1
  96. $course_res = $course_info->updateCounters(['shares' => 1]);
  97. if (true === $course_res) {
  98. return $this->success('课程分享成功');
  99. } else {
  100. return $this->error("课程分享失败!");
  101. }
  102. }
  103. /**
  104. * @name: actionThumbs
  105. * @msg: 对课程点赞 / 取消点赞
  106. * @param $id integer 课程 id
  107. * @param $star_status integer 点赞状态,1:点赞;0:取消点赞
  108. * @return {*}
  109. */
  110. public function actionThumbs()
  111. {
  112. $post = \Yii::$app->request->post();
  113. $res = \Yii::$app->services->validate->validate($post, [
  114. [['course_id'], 'required'],
  115. ]);
  116. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  117. $post['user_id'] = $this->user_id;
  118. $res = AddonsCourseChapterLikes::setData($this->mall_id, $post);
  119. if ($res === false) return $this->error(AddonsCourseOrders::getStaticError());
  120. return $this->success('操作成功', $res);
  121. }
  122. }