CourseService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. $item['poster_error'] = 0;
  74. }
  75. RedisService::set($cacheKey, $datas, rand(3600, 7200));
  76. }
  77. return $datas;
  78. }
  79. /**
  80. * 获取分类下视频课
  81. * @param int
  82. * @return array|mixed
  83. */
  84. public function getListByCate($params)
  85. {
  86. $type = isset($params['type'])? $params['type'] : 0;
  87. $sc = isset($params['sc'])? $params['sc'] : 0;
  88. $cacheKey = "caches:videos:list_by_cate:{$type}_".md5(json_encode($params));
  89. $datas = RedisService::get($cacheKey);
  90. // 视频课访问次数统计
  91. if(empty($sc)){
  92. ExamAccessLogModel::saveLog(date('Y-m-d'), $type, 20);
  93. }
  94. if($datas){
  95. return $datas;
  96. }
  97. //
  98. $datas = VideoCategoryModel::with(['courses'=>function($query) use($params){
  99. $kw = isset($params['keyword'])? trim($params['keyword']) : '';
  100. if($kw){
  101. $query->where(function($query) use($kw){
  102. $query->where('video_name',"like","%{$kw}%")
  103. ->orwhere('description',"like","%{$kw}%");
  104. });
  105. }
  106. $type = isset($params['type'])? intval($params['type']) : 0;
  107. if($type>0){
  108. $query->where('type',$type);
  109. }
  110. }])->distinct()
  111. ->from('videos_categorys as a')
  112. ->where(['a.status'=>1,'a.mark'=>1])
  113. ->select(['a.id','a.name','a.sort','a.icon'])
  114. ->orderBy('a.sort','desc')
  115. ->get();
  116. $datas = $datas? $datas->toArray() : [];
  117. if($datas){
  118. foreach ($datas as &$item){
  119. $item['icon'] = $item['icon']? get_image_url($item['icon']) : '';
  120. }
  121. RedisService::set($cacheKey, $datas, rand(300,600));
  122. }
  123. return $datas;
  124. }
  125. /**
  126. * 获取视频集下课程列表
  127. * @param int
  128. * @return array|mixed
  129. */
  130. public function getListByGroup($groupId,$params,$pageSize)
  131. {
  132. $page = isset($params['page'])? $params['page'] : 1;
  133. $type = isset($params['type'])? $params['type'] : 0;
  134. $cid = isset($params['cid'])? $params['cid'] : 0;
  135. $sc = isset($params['sc'])? $params['sc'] : 0;
  136. $cacheKey = "caches:videos:list_by_group:{$groupId}_{$page}_{$pageSize}_{$type}".md5(json_encode($params));
  137. $datas = RedisService::get($cacheKey);
  138. // 视频课访问次数统计
  139. if(empty($sc)){
  140. ExamAccessLogModel::saveLog(date('Y-m-d'), $type, 20);
  141. }
  142. if($datas){
  143. return $datas;
  144. }
  145. $list = $this->model->leftJoin('videos as b','b.id','=','videos_courses.video_id')
  146. ->with(['vip'])
  147. ->where(['videos_courses.video_id'=> $groupId,'videos_courses.status'=>1,'videos_courses.mark'=>1,'b.status'=>1,'b.mark'=>1])
  148. ->where(function($query) use($params){
  149. $kw = isset($params['keyword'])? trim($params['keyword']) : '';
  150. if($kw){
  151. $query->where('videos_courses.course_name',"like","%{$kw}%")
  152. ->orwhere('videos_courses.description',"like","%{$kw}%");
  153. }
  154. })
  155. ->where(function($query) use($cid){
  156. if($cid){
  157. $query->whereNotIn('videos_courses.id', [$cid]);
  158. }
  159. })
  160. ->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'])
  161. ->withCount(['courses','learns'])
  162. ->orderBy('videos_courses.sort','desc')
  163. ->orderBy('videos_courses.id','asc')
  164. ->paginate($pageSize > 0 ? $pageSize : 9999999);
  165. $list = $list? $list->toArray() :[];
  166. if($list){
  167. foreach ($list['data'] as &$item){
  168. }
  169. }
  170. $rows = isset($list['data'])? $list['data'] : [];
  171. $datas = [
  172. 'pageSize'=> $pageSize,
  173. 'total'=> isset($list['total'])? $list['total'] : 0,
  174. 'list'=> $rows
  175. ];
  176. if($rows){
  177. RedisService::set($cacheKey, $datas, rand(300,600));
  178. }
  179. return $datas;
  180. }
  181. /**
  182. * 视频课详情
  183. * @param $userId 用户
  184. * @param $id 课程ID
  185. * @return array|mixed
  186. */
  187. public function getInfo($userId, $id)
  188. {
  189. $cacheKey = "caches:videos:info_{$userId}_{$id}";
  190. $data = RedisService::get($cacheKey, $cacheKey);
  191. if($data){
  192. return $data;
  193. }
  194. $data = $this->model->from('videos_courses as videos_courses')
  195. ->leftJoin('videos as b','b.id','=','videos_courses.video_id')
  196. ->leftJoin('videos_learn_logs as c',function($join) use($userId){
  197. $join->on('c.course_id','=','videos_courses.id')
  198. ->where(['c.user_id'=>$userId,'c.status'=>1,'c.mark'=>1]);
  199. })
  200. ->with(['vip'])
  201. ->where(['videos_courses.id'=>$id,'videos_courses.status'=>1,'videos_courses.mark'=>1])
  202. ->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','c.id as learn_id','b.type'])
  203. ->first();
  204. $data = $data? $data->toArray() : [];
  205. if($data){
  206. // 验证付费视频是否有播放权限
  207. $fee = isset($data['fee'])? $data['fee'] : 0;
  208. $data['learn_id'] = !empty($data['learn_id'])? $data['learn_id'] : 0;
  209. $buyVipData = isset($data['vip'])? $data['vip'] : [];
  210. $data['buy_vip'] = $buyVipData? 1 : 0;
  211. $data['can_play'] = $buyVipData || $fee<=0? 1 : 0;
  212. $data['preview_time'] = ConfigService::make()->getConfigByCode('course_play_preview_time', 3);
  213. $data['courses_count'] = $this->model->where(['video_id'=> $data['video_id'],'status'=>1,'mark'=>1])->count('id');
  214. $data['learns_count'] = VideoLearnLogModel::where(['video_id'=> $data['video_id'],'status'=>1,'mark'=>1])->count('id');
  215. // 播放
  216. RedisService::set($cacheKey, $data, rand(5, 10));
  217. }
  218. $this->model->where(['id'=> $id])->increment('views', 1);
  219. return $data;
  220. }
  221. /**
  222. * 课程学习记录
  223. * @param $userId 用户
  224. * @param $id 课程ID
  225. * @return array|false|mixed
  226. */
  227. public function learn($userId, $id)
  228. {
  229. $cacheKey = "caches:videos:learn_{$userId}_{$id}";
  230. $data = RedisService::get($cacheKey, $cacheKey);
  231. if($data){
  232. $this->error = '课程已学习过';
  233. return $data;
  234. }
  235. $info = $this->model->where(['id'=> $id,'mark'=>1])->select(['id','video_id'])->first();
  236. $videoId = isset($info['video_id'])? $info['video_id'] : 0;
  237. if(empty($info)){
  238. $this->error = '课程不存在';
  239. return false;
  240. }
  241. if($videoId<=0){
  242. $this->error = '课程集参数错误';
  243. return false;
  244. }
  245. // 是否已有学习记录
  246. if($learnInfo = VideoLearnLogModel::where(['user_id'=>$userId,'course_id'=>$id,'video_id'=>$videoId])->select(['id','user_id','course_id','video_id'])->first()){
  247. RedisService::set($cacheKey, $learnInfo->toArray(), rand(300, 600));
  248. $this->error = '课程已学习过';
  249. return false;
  250. }
  251. $data = ['user_id'=> $userId,'course_id'=>$id,'video_id'=>$videoId,'create_time'=>time(),'status'=>1,'mark'=>1];
  252. $data['id'] = VideoLearnLogModel::insertGetId($data);
  253. RedisService::set($cacheKey, $data, rand(300, 600));
  254. $this->error = '课程学习完成';
  255. return false;
  256. }
  257. }