| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?php
- namespace addons\Course\backend\controllers;
- use addons\Course\common\enums\CourseEnum;
- use addons\Course\common\logic\Video;
- use addons\Course\common\models\AddonsCourse;
- use addons\Course\common\models\AddonsCourseChapter;
- use addons\Course\common\models\AddonsCourseDirectorys;
- use addons\Course\common\models\CourseCategory;
- use common\enums\RabbitMqEnum;
- use common\enums\StatusEnum;
- use common\models\goods\Goods;
- use common\models\user\UserLevel;
- /**
- * 默认控制器
- *
- * Class DefaultController
- * @package addons\Course\backend\controllers
- */
- class ChapterController extends BaseController
- {
- /**
- * @name:
- * @msg: 添加/编辑章节
- * @param {*}
- * @return {*}
- */
- public function actionStorage()
- {
- if (\Yii::$app->request->isGet) {
- $id = \Yii::$app->request->get('id');
- $course_id = \Yii::$app->request->get('course_id');
- $courses = AddonsCourse::find()
- ->select('id,name')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED])
- ->asArray()
- ->all();
- $return_data = [];
- $return_data['course_list'] = $courses;
- if (!empty($id)) {
- // 编辑章节
- if (empty($course_id)) {
- return $this->error('参数错误');
- }
- $directorys = AddonsCourseDirectorys::find()
- ->select('id,name')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'course_id' => $course_id])
- ->asArray()
- ->all();
- $chapter = AddonsCourseChapter::find()
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id])
- ->asArray()
- ->one();
- if (!empty($chapter['goods_list'])) {
- $good_ids = array_unique(explode(',', $chapter['goods_list']));
- } else {
- $good_ids = [];
- }
- $goods = Goods::find()
- ->select('id,goods_name,cover_pic,price')
- ->where(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $good_ids])
- ->asArray()
- ->all();
- $chapter['select_type']= 2;
- $return_data['directory_list'] = $directorys;
- $return_data['chapter'] = $chapter;
- $return_data['goods_list'] = $good_ids;
- $return_data['goods'] = $goods;
- if(!empty($chapter)&&$chapter['type']==1){
- \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
- 'id'=>$id,
- 'mall_id'=>$this->mall_id
- ], Video::class, 'updateChapterDuration');
- }
- }
- return $this->success('success', $return_data);
- } else {
- $post = \Yii::$app->request->post();
- $rules = [
- [['course_id', 'name', 'video_url', 'type'], 'required'],
- [['course_id', 'type' ,'can_see_time'], 'integer'],
- [['thumb_url'], 'string', 'max' => 255],
- [['video_url'], 'string', 'max' => 1000],
- ['name', 'string', 'max' => 20, 'message' => '章节名称不能超过20个字符'],
- [['goods_list', 'sort', 'can_see', 'id', 'dir_id','select_type','video_length'], 'safe']
- ];
- $res = \Yii::$app->services->validate->validate($post, $rules);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- if (empty($post['goods_list'])) {
- $post['goods_list'] = '';
- }
- $extension = strtolower(pathinfo($post['video_url'], PATHINFO_EXTENSION));
- if ($post['type'] == CourseEnum::CHAPTER_TYPE_VIDEO) {
- if($post['select_type']==1){
- if (!in_array($extension, CourseEnum::ALLOW_VIDEO_TYPE)) {
- return $this->error('请上传视频格式文件');
- }
- }
- } else {
- if (!in_array($extension, CourseEnum::ALLOW_AUDIO_TYPE)) {
- return $this->error('请上传音频格式文件');
- }
- }
- $chapter = AddonsCourseChapter::findOne(["course_id" => $post["course_id"], "status" => StatusEnum::ENABLED, "name" => $post["name"], 'mall_id' => $this->mall_id]);
- if ((!empty($chapter) && empty($post['id'])) || (!empty($chapter) && $chapter->id != $post['id'])) {
- return $this->error('章节名称已经存在');
- }
- if(empty($post['thumb_url']))$post['thumb_url'] = '';
- if(empty($post['video_length'])) $post['video_length'] = '0';
- $res = AddonsCourseChapter::setData($this->mall_id, $post);
- if (false === $res) {
- return $this->error('操作失败'.AddonsCourseChapter::getStaticError());
- }
- return $this->success('操作成功',['id'=>$res['id']]);
- }
- }
- /**
- * @name:
- * @msg: 章节状态切换
- * @param {*}
- * @return {*}
- */
- public function actionSwitchStatus()
- {
- $id = \Yii::$app->request->post('id');
- $can_see = \Yii::$app->request->post('can_see');
- if (empty($id) || !isset($can_see)) {
- return $this->error('参数错误');
- }
- $chapter = AddonsCourseChapter::findOne(['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $id]);
- if (empty($chapter)) {
- return $this->error('章节不存在');
- }
- $chapter->can_see = $can_see;
- $res = $chapter->save();
- if (false === $res) {
- return $this->error('操作失败:' . $chapter->getErrorMessage());
- }
- return $this->success('操作成功');
- }
- /**
- * @Author: guyu
- * @Date:2021-03-19
- * @Note:章节删除
- * @return Yii\web\Response
- */
- public function actionDelete()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $course_id = \Yii::$app->request->post('course_id');
- $chapter_id = \Yii::$app->request->post('chapter_id');
- if (empty($course_id) || empty($chapter_id)) {
- return $this->error('参数错误');
- }
- if (is_array($chapter_id)) {
- // 批量删除
- $res = AddonsCourseChapter::updateAll(['status' => StatusEnum::DELETE, 'updated_at' => time()], ['mall_id' => $this->mall_id, 'id' => $chapter_id, 'course_id' => $course_id]);
- } else {
- $chapter_info = AddonsCourseChapter::findOne(['mall_id' => $this->mall_id, 'id' => $chapter_id, 'course_id' => $course_id]);
- // dd($chapter_info);
- if (empty($chapter_info)) {
- return $this->error('章节不存在或者已经删除');
- }
- // 单个删除
- $chapter_info->status = StatusEnum::DELETE;
- $chapter_info->updated_at = time();
- $res = $chapter_info->save();
- }
- if ($res) {
- return $this->success('删除成功');
- } else {
- return $this->error('删除章节失败');
- }
- }
- /**
- * @name:
- * @msg: 单独修改章节的排序
- * @param {*}
- * @return {*}
- */
- public function actionEditSort()
- {
- if (!\Yii::$app->request->isPost) {
- return $this->error('请求方式错误');
- }
- $id = \Yii::$app->request->post('id');
- $sort = \Yii::$app->request->post('sort');
- if (empty($id) || empty($sort)) {
- return $this->error('参数错误');
- }
- $model = AddonsCourseChapter::findOne(['id' => $id, 'mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED]);
- if (empty($model)) {
- return $this->error('课程不存在或者已下架');
- }
- $model->sort = $sort;
- $model->updated_at = time();
- $res = $model->save();
- if ($res) {
- return $this->success('编辑成功');
- } else {
- return $this->error('编辑失败');
- }
- }
- public function actionUpdateChapterDuration(){
- $post = \Yii::$app->request->post();
- $res = \Yii::$app->services->validate->validate($post, [
- [['id','duration'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- AddonsCourseChapter::updateAll(['video_length'=>$post['duration']],['id'=>$post['id'],'mall_id'=>$this->mall_id]);
- return $this->success();
- }
- }
|