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(); } }