| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- <?php
- /**
- * Created by PhpStorm.
- * User: wniu
- * Date: 2022/3/14
- * Time: 2:43 下午
- */
- namespace addons\Course\api\modules\v1\controllers;
- use addons\Course\common\enums\CourseEnum;
- use addons\Course\common\logic\Watch;
- use addons\Course\common\models\AddonsCourse;
- use addons\Course\common\models\AddonsCourseActivity;
- use addons\Course\common\models\AddonsCourseBanners;
- use addons\Course\common\models\AddonsCourseBought;
- use addons\Course\common\models\AddonsCourseChapter;
- use addons\Course\common\models\AddonsCourseGroups;
- use addons\Course\common\models\CourseCategory;
- use addons\Course\common\models\CoursePromoter;
- use addons\Course\common\models\CourseUserWatch;
- use addons\Course\common\models\CourseWatchLog;
- use addons\Course\common\services\CourseActivityService;
- use api\controllers\OnAuthController;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\models\user\User;
- class HomeController extends OnAuthController
- {
- public $modelClass = '';
- /**
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['index', 'all-course-in-category', 'all-course-in-group', 'search', 'get-config'];
- public function actionIndex()
- {
- $params['where'] = [['mall_id' => $this->mall_id], ['=', 'status', StatusEnum::ENABLED], ['is_show' => CourseEnum::IS_SHOW]];
- $params['order'] = 'sort desc,created_at desc';
- $params['select'] = 'id,sort,title,pic_link as link,pic_path';
- $banner = AddonsCourseBanners::lists($params);
- $cate = CourseCategory::lists(['select' => 'id,sort,cname as cate_name,logo', 'where' => [['mall_id' => $this->mall_id], ['=', 'status', StatusEnum::ENABLED], ['is_navicat' => CourseEnum::IS_NAVICAT]], 'order' => 'sort asc,created_at desc']);
- $group = AddonsCourseGroups::lists(['select' => 'id,cid as course_id,sort,gname as group_name,logo', 'where' => [['mall_id' => $this->mall_id], ['=', 'status', StatusEnum::ENABLED]], 'order' => 'sort asc,created_at desc']);
- $activity = AddonsCourseActivity::lists([
- 'select' => 'id,name,logo,goods_id',
- 'where' => [
- ['mall_id' => $this->mall_id],
- ['=', 'status', StatusEnum::ENABLED]
- ],
- 'order' => 'id desc',
- 'limit' => 3
- ]);
- if ($activity) {
- $activity = CourseActivityService::getList($activity, $this->mall_id);
- }
- if ($banner) {
- foreach ($banner as $k => &$v) {
- if (!empty($v['link'])) {
- $v['link'] = json_decode($v['link']);
- }
- }
- }
- if ($group) {
- foreach ($group as $item => &$val) {
- if (empty($val['course_id'])) {
- $val['course_info'][] = [];
- } else {
- $course_ids = \Qiniu\json_decode($val['course_id']);
- $is_bought_list = AddonsCourseBought::checkIsBought($course_ids, $this->user_id);
- $course_info = AddonsCourse::getCourseListWithMemberprice($this->user, $this->mall_id, $course_ids, [], 'sell_type,allow_member_levels', 8);
- if ($course_info['list']) {
- foreach ($course_info['list'] as $k => &$v) {
- $v['is_bought'] = 0;
- if (isset($is_bought_list[$v['id']])) {
- $v['is_bought'] = 1;
- }
- }
- }
- $val['course_info'] = $course_info['list'];
- }
- }
- }
- $index_title = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_COURSE, 'basic.index_title');
- $is_promoter = 0;
- if(!empty($this->user_id)){
- $res = CoursePromoter::findOne(['user_id'=>$this->user_id,'mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED]);
- if(!empty($res)) $is_promoter= 1;
- }
- return $this->success('操作成功', compact('banner', 'group', 'cate', 'index_title','is_promoter', 'activity'));
- }
- /**
- * @name: actionAllCourseInCategory
- * @msg: 获取某一个分类下的所有课程
- * @param integer $id 课程分类id
- * @return {*}
- */
- public function actionAllCourseInCategory()
- {
- $get = \Yii::$app->request->get();
- $res = \Yii::$app->services->validate->validate($get, [
- [['cate_id'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- if ($get['cate_id'] == 0) {
- $cateWhere = [];
- } else {
- $cateWhere = ['cat_id' => $get['cate_id']];
- }
- $list = AddonsCourse::getCourseListWithMemberPrice($this->user, $this->mall_id, null, $cateWhere, 'sell_type,allow_member_levels');
- if($list['list']){
- $course_ids = array_column($list['list'],'id');
- $is_bought_list = AddonsCourseBought::checkIsBought($course_ids, $this->user_id);
- foreach ($list['list'] as $k => &$v) {
- $v['is_bought'] = 0;
- if (isset($is_bought_list[$v['id']])) {
- $v['is_bought'] = 1;
- }
- $v['views'] += $v['vm_views'];
- }
- }
- return $this->success('获取数据成功', $list);
- }
- /**
- * @name: actionAllCourseInCategory
- * @msg: 获取某一个分组下的所有的课程
- * @param integer $id 课程分组 id
- * @return {*}
- */
- public function actionAllCourseInGroup()
- {
- $get = \Yii::$app->request->get();
- $res = \Yii::$app->services->validate->validate($get, [
- [['group_id'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $course_info = AddonsCourseGroups::getOne([['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $get['group_id']]]);
- $course_ids = \Qiniu\json_decode($course_info->cid);
- $list = AddonsCourse::getCourseListWithMemberprice($this->user, $this->mall_id, $course_ids, [], 'sell_type,allow_member_levels');
- if($list['list']){
- $is_bought_list = AddonsCourseBought::checkIsBought($course_ids, $this->user_id);
- foreach ($list['list'] as $k => &$v) {
- $v['is_bought'] = 0;
- if (isset($is_bought_list[$v['id']])) {
- $v['is_bought'] = 1;
- }
- $v['views'] += $v['vm_views'];
- }
- }
- return $this->success('获取数据成功', $list);
- }
- /**
- * @name:
- * @msg: 课程搜索
- * @param {string} $key_word 搜索关键字
- * @return {*}
- */
- public function actionSearch()
- {
- $get = \Yii::$app->request->get();
- $res = \Yii::$app->services->validate->validate($get, [
- [['key_word'], 'required'],
- ]);
- if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
- $andWhere = ['or', ['like', 'name', $get['key_word']], ['like', 'introduce', $get['key_word']]];
- $list = AddonsCourse::getCourseListWithMemberprice($this->user, $this->mall_id, null, $andWhere, 'sell_type,allow_member_levels');
- if($list['list']){
- $course_ids = array_column($list['list'],'id');
- $is_bought_list = AddonsCourseBought::checkIsBought($course_ids, $this->user_id);
- foreach ($list['list'] as $k => &$v) {
- $v['is_bought'] = 0;
- if (isset($is_bought_list[$v['id']])) {
- $v['is_bought'] = 1;
- }
- }
- }
- return $this->success('获取数据成功', $list);
- }
- public function actionGetConfig()
- {
- $settings = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_COURSE, 'basic');
- return $this->success('success', $settings);
- }
- public function actionMyDirectUser(){
- try {
- $params = \Yii::$app->request->get();
- $res = CoursePromoter::findOne(['user_id'=>$this->user_id,'mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED]);
- if(empty($res)) return $this->error('您不是推广员');
- $where = [];
- $where[] = ['mall_id'=>$this->mall_id];
- $where[] = ['parent_id'=>$this->user_id];
- $where[] = ['status'=> [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $user_list = User::lists(['where'=>$where,'index'=>'id','select'=>'id,avatar_url,mobile,nickname']);
- $user_ids = array_column($user_list,'id');
- $params['where'][] = ['mall_id'=>$this->mall_id];
- $params['where'][] = ['user_id'=>$user_ids];
- $params['where'][] = ['status'=> [StatusEnum::ENABLED, StatusEnum::DISABLED]];
- $order = 'duration desc';
- if(!empty($params['order_type'])){
- if($params['order_type']==2){
- $order = 'register_time desc';
- }
- }
- $params['order'] = $order;
- $params['select'] = 'id,mall_id,user_id,register_time,duration';
- $list = CourseUserWatch::listPage($params);
- if(!empty($list['list'])){
- foreach ($list['list'] as &$item){
- $item['avatar_url'] = $item['nickname']=$item['mobile']='';
- if(isset($user_list[$item['user_id']])){
- $item['avatar_url'] = $user_list[$item['user_id']]['avatar_url'];
- $item['mobile'] = $user_list[$item['user_id']]['mobile'];
- $item['nickname'] = $user_list[$item['user_id']]['nickname'];
- }
- $item['text'] = Watch::getText($item['duration']);
- }
- }
- return $this->success('成功', $list);
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- public function actionMyDirectUserIntention(){
- try {
- $params = \Yii::$app->request->get();
- $res = CoursePromoter::findOne(['user_id'=>$this->user_id,'mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED]);
- if(empty($res)) return $this->error('您不是推广员');
- $user_id = $params['user_id'];
- $list = CourseWatchLog::lists(['where'=>[['user_id'=>$user_id]]]);
- $course_ids = array_column($list,'course_id');
- $watch_duration = array_sum(array_column($list,'duration'));
- $course_chapter_list = AddonsCourseChapter::lists(['where'=>[['course_id'=>$course_ids]]]);
- $total_duration = 0;
- 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];
- }
- $total_duration+=$duration;
- }
- }
- $intention = '一般意向';
- if(!empty($total_duration)&&!empty($watch_duration)){
- $schedule = bcmul($watch_duration/$total_duration,100,2);
- if($schedule>70){
- $intention='高意向';
- }elseif ($schedule>30){
- $intention='中意向';
- }
- }
- return $this->success('成功', ['intention'=>$intention,'watch_duration'=>$watch_duration,'total_duration'=>$total_duration]);
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- public function actionGetSetting()
- {
- try {
- $config = \Yii::$app->services->setting->addons->get($this->mall_id, CourseEnum::ADDONS_NAME, 'basic');
- if (empty($config['style'])) $config['style'] = 1;
- return $this->success('获取成功', $config);
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
|