CourseService.php 11 KB

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