CourseService.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Api;
  12. use App\Models\ExamAccessLogModel;
  13. use App\Models\VideoCategoryModel;
  14. use App\Models\VideoCoursesModel;
  15. use App\Models\VideoLearnLogModel;
  16. use App\Models\VideoModel;
  17. use App\Services\BaseService;
  18. use App\Services\ConfigService;
  19. use App\Services\RedisService;
  20. /**
  21. * 视频课服务-服务类
  22. * @author laravel开发员
  23. * @since 2020/11/11
  24. * @package App\Services\Api
  25. */
  26. class CourseService extends BaseService
  27. {
  28. // 静态对象
  29. protected static $instance = null;
  30. /**
  31. * 构造函数
  32. * @author laravel开发员
  33. * @since 2020/11/11
  34. */
  35. public function __construct()
  36. {
  37. $this->model = new VideoCoursesModel();
  38. }
  39. /**
  40. * 静态入口
  41. */
  42. public static function make()
  43. {
  44. if (!self::$instance) {
  45. self::$instance = new static();
  46. }
  47. return self::$instance;
  48. }
  49. /**
  50. * 获取
  51. * @param $type
  52. * @param int $num
  53. * @return array|mixed
  54. */
  55. public function getListByType($type, $num = 0)
  56. {
  57. $num = $num? $num : \App\Services\ConfigService::make()->getConfigByCode('show_course_num', 4);
  58. $cacheKey = "caches:videos:type_list_{$type}_{$num}";
  59. $datas = RedisService::get($cacheKey);
  60. if($datas){
  61. return $datas;
  62. }
  63. $datas = VideoModel::where(['type'=>$type,'status'=>1,'mark'=>1])
  64. ->select(['id','video_name','poster','type','description','is_recommend','status'])
  65. ->orderBy('is_recommend','asc')
  66. ->orderBy('create_time','desc')
  67. ->limit($num)
  68. ->get();
  69. $datas = $datas? $datas->toArray() : [];
  70. if($datas){
  71. foreach ($datas as &$item){
  72. $item['poster'] = $item['poster'] ? get_image_url($item['poster']) : '';
  73. }
  74. RedisService::set($cacheKey, $datas, rand(3600, 7200));
  75. }
  76. return $datas;
  77. }
  78. /**
  79. * 获取分类下视频课
  80. * @param int
  81. * @return array|mixed
  82. */
  83. public function getListByCate($params)
  84. {
  85. $type = isset($params['type'])? $params['type'] : 0;
  86. $sc = isset($params['sc'])? $params['sc'] : 0;
  87. $cacheKey = "caches:videos:list_by_cate:{$type}_".md5(json_encode($params));
  88. $datas = RedisService::get($cacheKey);
  89. // 视频课访问次数统计
  90. if(empty($sc)){
  91. ExamAccessLogModel::saveLog(date('Y-m-d'), $type, 20);
  92. }
  93. if($datas){
  94. return $datas;
  95. }
  96. //
  97. $datas = VideoCategoryModel::with(['courses'=>function($query) use($params){
  98. $kw = isset($params['keyword'])? trim($params['keyword']) : '';
  99. if($kw){
  100. $query->where(function($query) use($kw){
  101. $query->where('video_name',"like","%{$kw}%")
  102. ->orwhere('description',"like","%{$kw}%");
  103. });
  104. }
  105. $type = isset($params['type'])? intval($params['type']) : 0;
  106. if($type>0){
  107. $query->where('type',$type);
  108. }
  109. }])->distinct()
  110. ->from('videos_categorys as a')
  111. ->where(['a.status'=>1,'a.mark'=>1])
  112. ->select(['a.id','a.name','a.sort','a.icon'])
  113. ->orderBy('a.sort','desc')
  114. ->get();
  115. $datas = $datas? $datas->toArray() : [];
  116. if($datas){
  117. foreach ($datas as &$item){
  118. $item['icon'] = $item['icon']? get_image_url($item['icon']) : '';
  119. }
  120. RedisService::set($cacheKey, $datas, rand(300,600));
  121. }
  122. return $datas;
  123. }
  124. /**
  125. * 获取视频集下课程列表
  126. * @param int
  127. * @return array|mixed
  128. */
  129. public function getListByGroup($groupId,$params,$pageSize)
  130. {
  131. $page = isset($params['page'])? $params['page'] : 1;
  132. $type = isset($params['type'])? $params['type'] : 0;
  133. $cid = isset($params['cid'])? $params['cid'] : 0;
  134. $sc = isset($params['sc'])? $params['sc'] : 0;
  135. $cacheKey = "caches:videos:list_by_group:{$groupId}_{$page}_{$pageSize}_{$type}".md5(json_encode($params));
  136. $datas = RedisService::get($cacheKey);
  137. // 视频课访问次数统计
  138. if(empty($sc)){
  139. ExamAccessLogModel::saveLog(date('Y-m-d'), $type, 20);
  140. }
  141. if($datas){
  142. return $datas;
  143. }
  144. $list = $this->model->leftJoin('videos as b','b.id','=','videos_courses.video_id')
  145. ->with(['vip'])
  146. ->where(['videos_courses.video_id'=> $groupId,'videos_courses.status'=>1,'videos_courses.mark'=>1,'b.status'=>1,'b.mark'=>1])
  147. ->where(function($query) use($params){
  148. $kw = isset($params['keyword'])? trim($params['keyword']) : '';
  149. if($kw){
  150. $query->where('videos_courses.course_name',"like","%{$kw}%")
  151. ->orwhere('videos_courses.description',"like","%{$kw}%");
  152. }
  153. })
  154. ->where(function($query) use($cid){
  155. if($cid){
  156. $query->whereNotIn('videos_courses.id', [$cid]);
  157. }
  158. })
  159. ->select(['videos_courses.id','videos_courses.video_id','videos_courses.course_name','videos_courses.course_url','videos_courses.fee','videos_courses.poster','videos_courses.description','videos_courses.sort'])
  160. ->withCount(['courses','learns'])
  161. ->orderBy('videos_courses.sort','desc')
  162. ->orderBy('videos_courses.id','asc')
  163. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  164. $list = $list? $list->toArray() :[];
  165. if($list){
  166. foreach ($list['data'] as &$item){
  167. }
  168. }
  169. $rows = isset($list['data'])? $list['data'] : [];
  170. $datas = [
  171. 'pageSize'=> $pageSize,
  172. 'total'=> isset($list['total'])? $list['total'] : 0,
  173. 'list'=> $rows
  174. ];
  175. if($rows){
  176. RedisService::set($cacheKey, $datas, rand(300,600));
  177. }
  178. return $datas;
  179. }
  180. /**
  181. * 视频课详情
  182. * @param $userId 用户
  183. * @param $id 课程ID
  184. * @return array|mixed
  185. */
  186. public function getInfo($userId, $id)
  187. {
  188. $cacheKey = "caches:videos:info_{$userId}_{$id}";
  189. $data = RedisService::get($cacheKey, $cacheKey);
  190. if($data){
  191. return $data;
  192. }
  193. $data = $this->model->from('videos_courses as videos_courses')
  194. ->leftJoin('videos as b','b.id','=','videos_courses.video_id')
  195. /*->leftJoin('videos_learn_logs as c',function($join) use($userId){
  196. $join->on('c.course_id','=','videos_courses.id')
  197. ->where(['c.user_id'=>$userId,'c.status'=>1,'c.mark'=>1]);
  198. })*/
  199. ->with(['vip'])
  200. ->where(['videos_courses.id'=>$id,'videos_courses.status'=>1,'videos_courses.mark'=>1])
  201. ->select(['videos_courses.id','videos_courses.video_id','videos_courses.course_name','videos_courses.course_url','videos_courses.fee','videos_courses.poster','videos_courses.description','videos_courses.sort','b.type'])
  202. ->first();
  203. $data = $data? $data->toArray() : [];
  204. if($data){
  205. // 验证付费视频是否有播放权限
  206. $fee = isset($data['fee'])? $data['fee'] : 0;
  207. $buyVipData = isset($data['vip'])? $data['vip'] : [];
  208. $data['buy_vip'] = $buyVipData? 1 : 0;
  209. $data['can_play'] = $buyVipData || $fee<=0? 1 : 0;
  210. $data['preview_time'] = ConfigService::make()->getConfigByCode('course_play_preview_time', 3);
  211. $data['courses_count'] = $this->model->where(['video_id'=> $data['video_id'],'status'=>1,'mark'=>1])->count('id');
  212. $data['learns_count'] = VideoLearnLogModel::where(['video_id'=> $data['video_id'],'status'=>1,'mark'=>1])->count('id');
  213. // 播放
  214. RedisService::set($cacheKey, $data, rand(5, 10));
  215. }
  216. $this->model->where(['id'=> $id])->increment('views', 1);
  217. return $data;
  218. }
  219. }