HomeController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wniu
  5. * Date: 2022/3/14
  6. * Time: 2:43 下午
  7. */
  8. namespace addons\Course\api\modules\v1\controllers;
  9. use addons\Course\common\enums\CourseEnum;
  10. use addons\Course\common\logic\Watch;
  11. use addons\Course\common\models\AddonsCourse;
  12. use addons\Course\common\models\AddonsCourseActivity;
  13. use addons\Course\common\models\AddonsCourseBanners;
  14. use addons\Course\common\models\AddonsCourseBought;
  15. use addons\Course\common\models\AddonsCourseChapter;
  16. use addons\Course\common\models\AddonsCourseGroups;
  17. use addons\Course\common\models\CourseCategory;
  18. use addons\Course\common\models\CoursePromoter;
  19. use addons\Course\common\models\CourseUserWatch;
  20. use addons\Course\common\models\CourseWatchLog;
  21. use addons\Course\common\services\CourseActivityService;
  22. use api\controllers\OnAuthController;
  23. use common\enums\AddonsEnum;
  24. use common\enums\StatusEnum;
  25. use common\models\user\User;
  26. class HomeController extends OnAuthController
  27. {
  28. public $modelClass = '';
  29. /**
  30. * 不用进行登录验证的方法
  31. *
  32. * 例如: ['index', 'update', 'create', 'view', 'delete']
  33. * 默认全部需要验证
  34. *
  35. * @var array
  36. */
  37. protected $authOptional = ['index', 'all-course-in-category', 'all-course-in-group', 'search', 'get-config'];
  38. public function actionIndex()
  39. {
  40. $params['where'] = [['mall_id' => $this->mall_id], ['=', 'status', StatusEnum::ENABLED], ['is_show' => CourseEnum::IS_SHOW]];
  41. $params['order'] = 'sort desc,created_at desc';
  42. $params['select'] = 'id,sort,title,pic_link as link,pic_path';
  43. $banner = AddonsCourseBanners::lists($params);
  44. $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']);
  45. $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']);
  46. $activity = AddonsCourseActivity::lists([
  47. 'select' => 'id,name,logo,goods_id',
  48. 'where' => [
  49. ['mall_id' => $this->mall_id],
  50. ['=', 'status', StatusEnum::ENABLED]
  51. ],
  52. 'order' => 'id desc',
  53. 'limit' => 3
  54. ]);
  55. if ($activity) {
  56. $activity = CourseActivityService::getList($activity, $this->mall_id);
  57. }
  58. if ($banner) {
  59. foreach ($banner as $k => &$v) {
  60. if (!empty($v['link'])) {
  61. $v['link'] = json_decode($v['link']);
  62. }
  63. }
  64. }
  65. if ($group) {
  66. foreach ($group as $item => &$val) {
  67. if (empty($val['course_id'])) {
  68. $val['course_info'][] = [];
  69. } else {
  70. $course_ids = \Qiniu\json_decode($val['course_id']);
  71. $is_bought_list = AddonsCourseBought::checkIsBought($course_ids, $this->user_id);
  72. $course_info = AddonsCourse::getCourseListWithMemberprice($this->user, $this->mall_id, $course_ids, [], 'sell_type,allow_member_levels', 8);
  73. if ($course_info['list']) {
  74. foreach ($course_info['list'] as $k => &$v) {
  75. $v['is_bought'] = 0;
  76. if (isset($is_bought_list[$v['id']])) {
  77. $v['is_bought'] = 1;
  78. }
  79. }
  80. }
  81. $val['course_info'] = $course_info['list'];
  82. }
  83. }
  84. }
  85. $index_title = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_COURSE, 'basic.index_title');
  86. $is_promoter = 0;
  87. if(!empty($this->user_id)){
  88. $res = CoursePromoter::findOne(['user_id'=>$this->user_id,'mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED]);
  89. if(!empty($res)) $is_promoter= 1;
  90. }
  91. return $this->success('操作成功', compact('banner', 'group', 'cate', 'index_title','is_promoter', 'activity'));
  92. }
  93. /**
  94. * @name: actionAllCourseInCategory
  95. * @msg: 获取某一个分类下的所有课程
  96. * @param integer $id 课程分类id
  97. * @return {*}
  98. */
  99. public function actionAllCourseInCategory()
  100. {
  101. $get = \Yii::$app->request->get();
  102. $res = \Yii::$app->services->validate->validate($get, [
  103. [['cate_id'], 'required'],
  104. ]);
  105. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  106. if ($get['cate_id'] == 0) {
  107. $cateWhere = [];
  108. } else {
  109. $cateWhere = ['cat_id' => $get['cate_id']];
  110. }
  111. $list = AddonsCourse::getCourseListWithMemberPrice($this->user, $this->mall_id, null, $cateWhere, 'sell_type,allow_member_levels');
  112. if($list['list']){
  113. $course_ids = array_column($list['list'],'id');
  114. $is_bought_list = AddonsCourseBought::checkIsBought($course_ids, $this->user_id);
  115. foreach ($list['list'] as $k => &$v) {
  116. $v['is_bought'] = 0;
  117. if (isset($is_bought_list[$v['id']])) {
  118. $v['is_bought'] = 1;
  119. }
  120. $v['views'] += $v['vm_views'];
  121. }
  122. }
  123. return $this->success('获取数据成功', $list);
  124. }
  125. /**
  126. * @name: actionAllCourseInCategory
  127. * @msg: 获取某一个分组下的所有的课程
  128. * @param integer $id 课程分组 id
  129. * @return {*}
  130. */
  131. public function actionAllCourseInGroup()
  132. {
  133. $get = \Yii::$app->request->get();
  134. $res = \Yii::$app->services->validate->validate($get, [
  135. [['group_id'], 'required'],
  136. ]);
  137. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  138. $course_info = AddonsCourseGroups::getOne([['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED, 'id' => $get['group_id']]]);
  139. $course_ids = \Qiniu\json_decode($course_info->cid);
  140. $list = AddonsCourse::getCourseListWithMemberprice($this->user, $this->mall_id, $course_ids, [], 'sell_type,allow_member_levels');
  141. if($list['list']){
  142. $is_bought_list = AddonsCourseBought::checkIsBought($course_ids, $this->user_id);
  143. foreach ($list['list'] as $k => &$v) {
  144. $v['is_bought'] = 0;
  145. if (isset($is_bought_list[$v['id']])) {
  146. $v['is_bought'] = 1;
  147. }
  148. $v['views'] += $v['vm_views'];
  149. }
  150. }
  151. return $this->success('获取数据成功', $list);
  152. }
  153. /**
  154. * @name:
  155. * @msg: 课程搜索
  156. * @param {string} $key_word 搜索关键字
  157. * @return {*}
  158. */
  159. public function actionSearch()
  160. {
  161. $get = \Yii::$app->request->get();
  162. $res = \Yii::$app->services->validate->validate($get, [
  163. [['key_word'], 'required'],
  164. ]);
  165. if ($res === false) return $this->error(\Yii::$app->services->validate->getErrorMessage());
  166. $andWhere = ['or', ['like', 'name', $get['key_word']], ['like', 'introduce', $get['key_word']]];
  167. $list = AddonsCourse::getCourseListWithMemberprice($this->user, $this->mall_id, null, $andWhere, 'sell_type,allow_member_levels');
  168. if($list['list']){
  169. $course_ids = array_column($list['list'],'id');
  170. $is_bought_list = AddonsCourseBought::checkIsBought($course_ids, $this->user_id);
  171. foreach ($list['list'] as $k => &$v) {
  172. $v['is_bought'] = 0;
  173. if (isset($is_bought_list[$v['id']])) {
  174. $v['is_bought'] = 1;
  175. }
  176. }
  177. }
  178. return $this->success('获取数据成功', $list);
  179. }
  180. public function actionGetConfig()
  181. {
  182. $settings = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_COURSE, 'basic');
  183. return $this->success('success', $settings);
  184. }
  185. public function actionMyDirectUser(){
  186. try {
  187. $params = \Yii::$app->request->get();
  188. $res = CoursePromoter::findOne(['user_id'=>$this->user_id,'mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED]);
  189. if(empty($res)) return $this->error('您不是推广员');
  190. $where = [];
  191. $where[] = ['mall_id'=>$this->mall_id];
  192. $where[] = ['parent_id'=>$this->user_id];
  193. $where[] = ['status'=> [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  194. $user_list = User::lists(['where'=>$where,'index'=>'id','select'=>'id,avatar_url,mobile,nickname']);
  195. $user_ids = array_column($user_list,'id');
  196. $params['where'][] = ['mall_id'=>$this->mall_id];
  197. $params['where'][] = ['user_id'=>$user_ids];
  198. $params['where'][] = ['status'=> [StatusEnum::ENABLED, StatusEnum::DISABLED]];
  199. $order = 'duration desc';
  200. if(!empty($params['order_type'])){
  201. if($params['order_type']==2){
  202. $order = 'register_time desc';
  203. }
  204. }
  205. $params['order'] = $order;
  206. $params['select'] = 'id,mall_id,user_id,register_time,duration';
  207. $list = CourseUserWatch::listPage($params);
  208. if(!empty($list['list'])){
  209. foreach ($list['list'] as &$item){
  210. $item['avatar_url'] = $item['nickname']=$item['mobile']='';
  211. if(isset($user_list[$item['user_id']])){
  212. $item['avatar_url'] = $user_list[$item['user_id']]['avatar_url'];
  213. $item['mobile'] = $user_list[$item['user_id']]['mobile'];
  214. $item['nickname'] = $user_list[$item['user_id']]['nickname'];
  215. }
  216. $item['text'] = Watch::getText($item['duration']);
  217. }
  218. }
  219. return $this->success('成功', $list);
  220. }catch (\Exception $e){
  221. return $this->error($e->getMessage());
  222. }
  223. }
  224. public function actionMyDirectUserIntention(){
  225. try {
  226. $params = \Yii::$app->request->get();
  227. $res = CoursePromoter::findOne(['user_id'=>$this->user_id,'mall_id'=>$this->mall_id,'status'=>StatusEnum::ENABLED]);
  228. if(empty($res)) return $this->error('您不是推广员');
  229. $user_id = $params['user_id'];
  230. $list = CourseWatchLog::lists(['where'=>[['user_id'=>$user_id]]]);
  231. $course_ids = array_column($list,'course_id');
  232. $watch_duration = array_sum(array_column($list,'duration'));
  233. $course_chapter_list = AddonsCourseChapter::lists(['where'=>[['course_id'=>$course_ids]]]);
  234. $total_duration = 0;
  235. foreach ($course_chapter_list as $item){
  236. if(!empty($item['video_length'])){
  237. $video_length_arr = explode(':',$item['video_length']);
  238. if(count($video_length_arr)>1){
  239. $duration = $video_length_arr[0]*3600+$video_length_arr[1]*60+ bcadd($video_length_arr[2],0,0);
  240. }else{
  241. $duration =$video_length_arr[0];
  242. }
  243. $total_duration+=$duration;
  244. }
  245. }
  246. $intention = '一般意向';
  247. if(!empty($total_duration)&&!empty($watch_duration)){
  248. $schedule = bcmul($watch_duration/$total_duration,100,2);
  249. if($schedule>70){
  250. $intention='高意向';
  251. }elseif ($schedule>30){
  252. $intention='中意向';
  253. }
  254. }
  255. return $this->success('成功', ['intention'=>$intention,'watch_duration'=>$watch_duration,'total_duration'=>$total_duration]);
  256. }catch (\Exception $e){
  257. return $this->error($e->getMessage());
  258. }
  259. }
  260. public function actionGetSetting()
  261. {
  262. try {
  263. $config = \Yii::$app->services->setting->addons->get($this->mall_id, CourseEnum::ADDONS_NAME, 'basic');
  264. if (empty($config['style'])) $config['style'] = 1;
  265. return $this->success('获取成功', $config);
  266. } catch (\Exception $e) {
  267. return $this->error($e->getMessage());
  268. }
  269. }
  270. }