request->get(); $res = \Yii::$app->services->validate->validate($get, [ [['course_id'], 'required'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); //课程详情 $list = AddonsCourse::getCourseListWithMemberprice($this->user, $this->mall_id, $get['course_id'], [], '*'); $course = $list['list'][0] ?? []; if (empty($course) || $course['status'] != StatusEnum::ENABLED) { return $this->error("课程不存在或已下架"); } //去掉不需要返回的数据字段 $course['created_at'] = date("m月d日", $course['created_at']); unset($course['is_open_vm'], $course['vm_shares'], $course['vm_likes'], $course['vm_downloads'], $course['vm_views'], $course['updated_at'], $course['status']); $can_play_res = AddonsCourse::courseCanPlay($get['course_id'], $this->mall_id, $this->user); $is_can_play = $can_play_res['is_can_play']; // 当前用户还需要转发的次数。如果不是转发观看的课程,need_relays = 0; $need_relays = $can_play_res['need_relays'] ?? 0; // 当前用户已经转发的次数。如果不是转发观看的课程,current_user_relayed_times = 0; $current_user_relayed_times = $can_play_res['current_user_relayed_times'] ?? 0; if (!empty($this->user_id)) { $is_bought = AddonsCourse::courseIsBought($get['course_id'], $this->mall_id, $this->user_id); //用户是否点赞 $is_likes = AddonsCourseChapterLikes::getUserIsLikes($get['course_id'], $this->user_id); } else { $is_bought = 0; $is_likes = 0; } //章节列表,只有一个,是点击课程进来之后默认播放的章节 $chapter_model = AddonsCourseChapter::find()->where(["mall_id" => $this->mall_id, "course_id" => $get['course_id'], "status" => StatusEnum::ENABLED]); $chapter_lists = $chapter_model->select("id,mall_id,course_id,name,type,goods_list,can_see,views,sort,video_length,thumb_url") ->orderBy("sort asc,created_at desc") ->asArray() ->all(); $chapter_list = $chapter_lists[0] ?? []; $goods_list = []; $goods_ids = []; if (!empty($chapter_lists)) { //获取课程所有商品 $good_id_strs = array_column($chapter_lists, 'goods_list'); if (!empty($good_id_strs)) { foreach ($good_id_strs as $id_str) { $goods_ids = array_merge($goods_ids, explode(',', $id_str)); } $goods_ids = array_unique($goods_ids); $goods_list = AddonsCourseChapter::getAllGoods($goods_ids, false); } } if (!empty($chapter_list)) { if ($chapter_list['type'] == CourseEnum::CHAPTER_TYPE_AUDIO) { $chapter_list['thumb_url'] = $course['logo']; } } if (1 !== $is_can_play) { $chapter_list = null; } return $this->success('success', compact("course", "chapter_list", "goods_list", "is_likes", 'is_can_play', 'is_bought', 'current_user_relayed_times', 'need_relays')); } /** * 获取章节列表 * @return mixed */ public function actionChapter() { $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()); $data['list'] = AddonsCourseChapter::find() ->select('id,mall_id,course_id,dir_id,name,can_see,views,sort, thumb_url,video_length,goods_list,video_length') ->where(['course_id' => $get['course_id'], 'mall_id' => $this->mall_id, "status" => StatusEnum::ENABLED]) ->orderBy('sort asc,created_at desc') ->asArray() ->all(); if (!empty($data['list'])) { foreach ($data['list'] as $dk => $dv) { $dv['video_length'] = ''; // if (!empty($dv['video_length'])) { // $video_lengths = explode(':', $dv['video_length']); // $video_lengths[2] = ceil($video_lengths[2]); // $dv['video_length'] = implode(':', $video_lengths); // } $dv['goods_list'] = AddonsCourseChapter::getAllGoods($dv["goods_list"], false); $chapter_list[$dv['dir_id']][] = $dv; } // 课程目录数据 $dir_info = AddonsCourseDirectorys::getDirTreeList($this->mall_id, $get['course_id'], 'id,course_id,name as directory_name,parent_id'); $dir_info = $dir_info['tree']; // dd($dir_info); if (!empty($dir_info)) { foreach ($dir_info as $dik => $div) { if (!empty($chapter_list[$div['id']])) { $dir_info[$dik]['chapter_list'] = $chapter_list[$div['id']]; } else { $dir_info[$dik]['chapter_list'] = []; } if (!empty($div['sub_dir'])) { foreach ($div['sub_dir'] as $dsk => $dsv) { if (!empty($chapter_list[$dsv['id']])) { $dir_info[$dik]['sub_dir'][$dsk]['chapter_list'] = $chapter_list[$dsv['id']]; } else { $dir_info[$dik]['sub_dir'][$dsk]['chapter_list'] = []; } } } } // dd($dir_info); } } else { $dir_info = []; } $data['list'] = $dir_info; // dd($data); return $this->success('success', $data); } /** * 课程下载 * @return mixed */ public function actionDownload() { $get = \Yii::$app->request->get(); $res = \Yii::$app->services->validate->validate($get, [ [['course_id', 'chapter_id'], 'required'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $course_id = \Yii::$app->request->get('course_id'); $chapter_id = \Yii::$app->request->get('chapter_id'); $course = AddonsCourse::getDetail($course_id, $this->mall_id, '*'); if (empty($course)) { return $this->error("课程不存在或已下架"); } $chapter = AddonsCourseChapter::getDetail($chapter_id); if (empty($chapter)) { return $this->error("课程不存在或已下架"); } //检查用户是否有查看章节的权限 $can_play_res = AddonsCourse::courseCanPlay($course_id, $this->mall_id, $this->user); if (empty($chapter->can_see) && empty($can_play_res['is_can_play'])) { return $this->error("您没有权限查看该课程"); } //设置课程视频下载次数 + 1 //$course->downloads = $course->downloads + 1; if (!$course->updateCounters(['downloads' => 1])) { return $this->error("保存课程下载次数失败"); } $data = ["video_url" => $chapter->video_url]; return $this->success('success', $data); } /** * 课程播放 * @Author: guyu * @DateTime:2021-03-22 14:55 * @return mixed */ public function actionPlay() { $get = \Yii::$app->request->get(); $res = \Yii::$app->services->validate->validate($get, [ [['course_id'], 'required'], [['course_id'], 'integer'], [['chapter_id'], 'safe'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $course = AddonsCourse::getDetail($get['course_id'], $this->mall_id, '*'); if (empty($course)) { return $this->error("课程不存在或已下架"); } $chapter = AddonsCourseChapter::getDetail($get['chapter_id']); if (empty($chapter)) { return $this->error("该课程无目录章节"); } if ($chapter->type == CourseEnum::CHAPTER_TYPE_AUDIO) { $chapter->thumb_url = $course->logo; } //检查用户是否有查看章节的权限 //检查用户是否有查看章节的权限 $can_play_res = AddonsCourse::courseCanPlay($get['course_id'], $this->mall_id, $this->user); if (empty($chapter->can_see) && empty($can_play_res['is_can_play'])) { return $this->error("您没有权限查看该课程"); } //增加章节的播放次数 +1 if (!AddonsCourseChapter::updateViews($get['chapter_id'], $get['course_id'])) { return $this->error("保存课程播放次数失败"); } //获取章节的所有商品 $goods_list = AddonsCourseChapter::getAllGoods($chapter->goods_list, false); $data = [ "video_url" => $chapter->video_url, "video_type" => $chapter->type, "video_length" => $chapter->video_length, "thumb_url" => $chapter->thumb_url, "goods_info" => $goods_list, "can_see_time" => $chapter->can_see_time, ]; return $this->success('success', $data); } /** * @name: * @msg: 已购买课程列表 * @param {*} * @return {*} */ public function actionCourseBought() { $where[] = ['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'is_pay' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED]; $params = [ 'where' => $where, 'with' => 'course', 'order' => 'created_at desc', ]; $list = AddonsCourseBought::listPage($params, false, true); if (!empty($list['list'])) { $discount = (string)AddonsCourse::getCurrMemberDiscount($this->user, $this->mall_id); foreach ($list['list'] as $k => &$v) { $v['course']['member_price'] = bcmul($v['course']['member_price'], $discount, 2); } } return $this->success('success', $list); } /** * @name: * @msg: 删除已购买的课程 * @param {int} $mall_id * @param {int} $user_id * @param {int} $id 数据记录 id * @return {*} */ public function actionDelBought() { $get = \Yii::$app->request->get(); $res = \Yii::$app->services->validate->validate($get, [ [['id'], 'required'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $model = AddonsCourseBought::getOne([['id' => $get['id'], 'mall_id' => $this->mall_id, 'user_id' => $this->user_id]]); if (!$model) { return $this->error('课程不存在'); } $model->status = StatusEnum::DELETE; $res = $model->save(); if ($res) { return $this->success('删除成功'); } else { return $this->error('删除失败'); } } /** * @name: * @msg: * @param {integer} $course_id 课程 id * @param {integer} $relay_type 转发方式,1:微信好友,2:微信朋友圈 * @return {*} */ public function actionCourseRelay() { $get = \Yii::$app->request->get(); $res = \Yii::$app->services->validate->validate($get, [ [['course_id', 'relay_type'], 'required'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $params['mall_id'] = $this->mall_id; $params['user_id'] = $this->user_id; $params['course_id'] = $get['course_id']; $params['relay_type'] = $get['relay_type']; $data = AddonsCourseRelays::setData($params); //检查用户是否有查看章节的权限 $can_play_res = AddonsCourse::courseCanPlay($get['course_id'], $this->mall_id, $this->user); if ($data) { return $this->success('转发成功', $can_play_res); } return $this->error('转发失败'); } //增加视频课程观看时长 public function actionAddWatchLog(){ try { $post = \Yii::$app->request->post(); $res = \Yii::$app->services->validate->validate($post, [ [['course_id', 'duration'], 'required'], ]); if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage()); $course = AddonsCourse::findOne(['mall_id'=>$this->mall_id,'id'=>$post['course_id'],'status'=>StatusEnum::ENABLED]); if(empty($course)) return $this->success(); $post['mall_id'] = $this->mall_id; $post['user_id'] = $this->user_id; $res = Watch::add($post); if($res==false) return $this->error(Watch::getStaticError()); if($post['duration']>20){ //发公众号模板消息 ToDO \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [ 'user_id'=>$this->user_id, 'mall_id'=>$this->mall_id ], Video::class, 'sendWechatMessage'); } return $this->success(); }catch (\Exception $e){ return $this->error($e->getMessage()); } } //观看进度 public function actionWatchLog(){ try { $res = CoursePromoter::findOne(['user_id'=>$this->user_id,'mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED]); if(empty($res)) return $this->error('您不是推广员'); $params = \Yii::$app->request->get(); $user_id = $params['user_id']; $user = User::findOne(['id'=>$user_id]); if(empty($user)||$user['parent_id']!=$this->user_id){ return $this->error('无查看权限'); } $params['where'][] = ['mall_id'=>$this->mall_id]; $params['where'][] = ['user_id'=>$user_id]; $params['where'][] = ['status'=> StatusEnum::ENABLED]; $params['order'] = 'id desc'; $params['select'] = 'id,mall_id,user_id,course_id,duration,nums'; $list = CourseWatchLog::listPage($params, false); if(!empty($list['list'])){ $course_ids = array_column($list['list'],'course_id'); $course_chapter_list = AddonsCourseChapter::lists(['where'=>[['course_id'=>$course_ids]]]); $course_list = AddonsCourse::lists(['where'=>[['id'=>$course_ids]],'select'=>'id,name,logo','index'=>'id']); $course_arr = []; foreach ($course_chapter_list as $item){ if(!empty($item['video_length'])){ $video_length_arr = explode(':',$item['video_length']); if(count($video_length_arr)>1){ $duration = $video_length_arr[0]*3600+$video_length_arr[1]*60+ bcadd($video_length_arr[2],0,0); }else{ $duration =$video_length_arr[0]; } if(isset($course_arr[$item['course_id']])){ $course_arr[$item['course_id']]+=$duration; }else{ $course_arr[$item['course_id']] = $duration; } } } foreach ($list['list'] as &$item){ $total_duration = $course_arr[$item['course_id']]??0; $schedule = 0; if($total_duration>0){ $schedule = bcmul($item['duration']/$total_duration,100,2); } $intention = '一般意向'; if($schedule>70){ $intention='高意向'; }elseif ($schedule>30){ $intention='中意向'; } $item['intention'] = $intention; $item['schedule'] = $schedule.'%'; $item['logo'] = ''; $item['name'] = ''; if(isset($course_list[$item['course_id']])){ $item['logo'] = $course_list[$item['course_id']]['logo']; $item['name'] = $course_list[$item['course_id']]['name']; } $item['text'] =Watch::getText($item['duration']); unset($item['duration']); } } return $this->success('成功', $list); }catch (\Exception $e){ return $this->error($e->getMessage()); } } //转发记录 public function actionForwardLog(){ try { $res = CoursePromoter::findOne(['user_id'=>$this->user_id,'mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED]); if(empty($res)) return $this->error('您不是推广员'); $params = \Yii::$app->request->get(); $user_id = $params['user_id']; $user = User::findOne(['id'=>$user_id]); if(empty($user)||$user['parent_id']!=$this->user_id){ return $this->error('无查看权限'); } $user_list = User::lists(['where'=>[['parent_id'=>$user_id]],'select'=>'id,nickname,avatar_url','index'=>'id']); $child_ids = array_column($user_list,'id'); $params['where'][] = ['mall_id'=>$this->mall_id]; $params['where'][] = ['user_id'=>$child_ids]; $params['where'][] = ['status'=> StatusEnum::ENABLED]; $params['order'] = 'id desc'; $params['select'] = 'id,mall_id,user_id,course_id,duration,updated_at'; $list = CourseWatchLog::listPage($params, false); if(!empty($list['list'])){ $course_ids = array_column($list['list'],'course_id'); $course_chapter_list = AddonsCourseChapter::lists(['where'=>[['course_id'=>$course_ids]]]); $course_list = AddonsCourse::lists(['where'=>[['id'=>$course_ids]],'select'=>'id,name,logo','index'=>'id']); $course_arr = []; foreach ($course_chapter_list as $item){ if(!empty($item['video_length'])){ $video_length_arr = explode(':',$item['video_length']); if(count($video_length_arr)>1){ $duration = $video_length_arr[0]*3600+$video_length_arr[1]*60+ bcadd($video_length_arr[2],0,0); }else{ $duration =$video_length_arr[0]; } if(isset($course_arr[$item['course_id']])){ $course_arr[$item['course_id']]+=$duration; }else{ $course_arr[$item['course_id']] = $duration; } } } foreach ($list['list'] as &$item){ $total_duration = $course_arr[$item['course_id']]??0; $schedule = 0; if($total_duration>0){ $schedule = bcmul($item['duration']/$total_duration,100,2); } $intention = '一般意向'; if($schedule>70){ $intention='高意向'; }elseif ($schedule>30){ $intention='中意向'; } $item['intention'] = $intention; $item['schedule'] = $schedule.'%'; $item['name'] = ''; $item['nickname'] = ''; $item['avatar_url'] = ''; if(isset($course_list[$item['course_id']])){ $item['name'] = $course_list[$item['course_id']]['name']; } if(isset($user_list[$item['user_id']])){ $item['nickname'] = $user_list[$item['user_id']]['nickname']; $item['avatar_url'] = $user_list[$item['user_id']]['avatar_url']; } unset($item['duration']); } } return $this->success('成功', $list); }catch (\Exception $e){ return $this->error($e->getMessage()); } } }