ChapterController.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. namespace addons\Course\backend\controllers;
  3. use addons\Course\common\enums\CourseEnum;
  4. use addons\Course\common\logic\Video;
  5. use addons\Course\common\models\AddonsCourse;
  6. use addons\Course\common\models\AddonsCourseChapter;
  7. use addons\Course\common\models\AddonsCourseDirectorys;
  8. use addons\Course\common\models\CourseCategory;
  9. use common\enums\RabbitMqEnum;
  10. use common\enums\StatusEnum;
  11. use common\models\goods\Goods;
  12. use common\models\user\UserLevel;
  13. /**
  14. * 默认控制器
  15. *
  16. * Class DefaultController
  17. * @package addons\Course\backend\controllers
  18. */
  19. class ChapterController extends BaseController
  20. {
  21. /**
  22. * @name:
  23. * @msg: 添加/编辑章节
  24. * @param {*}
  25. * @return {*}
  26. */
  27. public function actionStorage()
  28. {
  29. if (\Yii::$app->request->isGet) {
  30. $id = \Yii::$app->request->get('id');
  31. $course_id = \Yii::$app->request->get('course_id');
  32. $courses = AddonsCourse::find()
  33. ->select('id,name')
  34. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
  35. ->asArray()
  36. ->all();
  37. $return_data = [];
  38. $return_data['course_list'] = $courses;
  39. if (!empty($id)) {
  40. // 编辑章节
  41. if (empty($course_id)) {
  42. return $this->error('参数错误');
  43. }
  44. $directorys = AddonsCourseDirectorys::find()
  45. ->select('id,name')
  46. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'course_id' => $course_id])
  47. ->asArray()
  48. ->all();
  49. $chapter = AddonsCourseChapter::find()
  50. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id])
  51. ->asArray()
  52. ->one();
  53. if (!empty($chapter['goods_list'])) {
  54. $good_ids = array_unique(explode(',', $chapter['goods_list']));
  55. } else {
  56. $good_ids = [];
  57. }
  58. $goods = Goods::find()
  59. ->select('id,goods_name,cover_pic,price')
  60. ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $good_ids])
  61. ->asArray()
  62. ->all();
  63. $chapter['select_type']= 2;
  64. $return_data['directory_list'] = $directorys;
  65. $return_data['chapter'] = $chapter;
  66. $return_data['goods_list'] = $good_ids;
  67. $return_data['goods'] = $goods;
  68. if(!empty($chapter)&&$chapter['type']==1){
  69. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
  70. 'id'=>$id,
  71. 'mall_id'=>$this->mall_id
  72. ], Video::class, 'updateChapterDuration');
  73. }
  74. }
  75. return $this->success('success', $return_data);
  76. } else {
  77. $post = \Yii::$app->request->post();
  78. $rules = [
  79. [['course_id', 'name', 'video_url', 'type'], 'required'],
  80. [['course_id', 'type' ,'can_see_time'], 'integer'],
  81. [['thumb_url'], 'string', 'max' => 255],
  82. [['video_url'], 'string', 'max' => 1000],
  83. ['name', 'string', 'max' => 20, 'message' => '章节名称不能超过20个字符'],
  84. [['goods_list', 'sort', 'can_see', 'id', 'dir_id','select_type','video_length'], 'safe']
  85. ];
  86. $res = \Yii::$app->services->validate->validate($post, $rules);
  87. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  88. if (empty($post['goods_list'])) {
  89. $post['goods_list'] = '';
  90. }
  91. $extension = strtolower(pathinfo($post['video_url'], PATHINFO_EXTENSION));
  92. if ($post['type'] == CourseEnum::CHAPTER_TYPE_VIDEO) {
  93. if($post['select_type']==1){
  94. if (!in_array($extension, CourseEnum::ALLOW_VIDEO_TYPE)) {
  95. return $this->error('请上传视频格式文件');
  96. }
  97. }
  98. } else {
  99. if (!in_array($extension, CourseEnum::ALLOW_AUDIO_TYPE)) {
  100. return $this->error('请上传音频格式文件');
  101. }
  102. }
  103. $chapter = AddonsCourseChapter::findOne(["course_id" => $post["course_id"], "status" => StatusEnum::ENABLED, "name" => $post["name"], 'mall_id' => $this->mall_id]);
  104. if ((!empty($chapter) && empty($post['id'])) || (!empty($chapter) && $chapter->id != $post['id'])) {
  105. return $this->error('章节名称已经存在');
  106. }
  107. if(empty($post['thumb_url']))$post['thumb_url'] = '';
  108. if(empty($post['video_length'])) $post['video_length'] = '0';
  109. $res = AddonsCourseChapter::setData($this->mall_id, $post);
  110. if (false === $res) {
  111. return $this->error('操作失败'.AddonsCourseChapter::getStaticError());
  112. }
  113. return $this->success('操作成功',['id'=>$res['id']]);
  114. }
  115. }
  116. /**
  117. * @name:
  118. * @msg: 章节状态切换
  119. * @param {*}
  120. * @return {*}
  121. */
  122. public function actionSwitchStatus()
  123. {
  124. $id = \Yii::$app->request->post('id');
  125. $can_see = \Yii::$app->request->post('can_see');
  126. if (empty($id) || !isset($can_see)) {
  127. return $this->error('参数错误');
  128. }
  129. $chapter = AddonsCourseChapter::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id]);
  130. if (empty($chapter)) {
  131. return $this->error('章节不存在');
  132. }
  133. $chapter->can_see = $can_see;
  134. $res = $chapter->save();
  135. if (false === $res) {
  136. return $this->error('操作失败:' . $chapter->getErrorMessage());
  137. }
  138. return $this->success('操作成功');
  139. }
  140. /**
  141. * @Author: guyu
  142. * @Date:2021-03-19
  143. * @Note:章节删除
  144. * @return Yii\web\Response
  145. */
  146. public function actionDelete()
  147. {
  148. if (!\Yii::$app->request->isPost) {
  149. return $this->error('请求方式错误');
  150. }
  151. $course_id = \Yii::$app->request->post('course_id');
  152. $chapter_id = \Yii::$app->request->post('chapter_id');
  153. if (empty($course_id) || empty($chapter_id)) {
  154. return $this->error('参数错误');
  155. }
  156. if (is_array($chapter_id)) {
  157. // 批量删除
  158. $res = AddonsCourseChapter::updateAll(['status' => StatusEnum::DELETE, 'updated_at' => time()], ['mall_id' => $this->mall_id, 'id' => $chapter_id, 'course_id' => $course_id]);
  159. } else {
  160. $chapter_info = AddonsCourseChapter::findOne(['mall_id' => $this->mall_id, 'id' => $chapter_id, 'course_id' => $course_id]);
  161. // dd($chapter_info);
  162. if (empty($chapter_info)) {
  163. return $this->error('章节不存在或者已经删除');
  164. }
  165. // 单个删除
  166. $chapter_info->status = StatusEnum::DELETE;
  167. $chapter_info->updated_at = time();
  168. $res = $chapter_info->save();
  169. }
  170. if ($res) {
  171. return $this->success('删除成功');
  172. } else {
  173. return $this->error('删除章节失败');
  174. }
  175. }
  176. /**
  177. * @name:
  178. * @msg: 单独修改章节的排序
  179. * @param {*}
  180. * @return {*}
  181. */
  182. public function actionEditSort()
  183. {
  184. if (!\Yii::$app->request->isPost) {
  185. return $this->error('请求方式错误');
  186. }
  187. $id = \Yii::$app->request->post('id');
  188. $sort = \Yii::$app->request->post('sort');
  189. if (empty($id) || empty($sort)) {
  190. return $this->error('参数错误');
  191. }
  192. $model = AddonsCourseChapter::findOne(['id' => $id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
  193. if (empty($model)) {
  194. return $this->error('课程不存在或者已下架');
  195. }
  196. $model->sort = $sort;
  197. $model->updated_at = time();
  198. $res = $model->save();
  199. if ($res) {
  200. return $this->success('编辑成功');
  201. } else {
  202. return $this->error('编辑失败');
  203. }
  204. }
  205. public function actionUpdateChapterDuration(){
  206. $post = \Yii::$app->request->post();
  207. $res = \Yii::$app->services->validate->validate($post, [
  208. [['id','duration'], 'required'],
  209. ]);
  210. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  211. AddonsCourseChapter::updateAll(['video_length'=>$post['duration']],['id'=>$post['id'],'mall_id'=>$this->mall_id]);
  212. return $this->success();
  213. }
  214. }