| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- // +----------------------------------------------------------------------
- // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
- // +----------------------------------------------------------------------
- // | 版权所有 2017~2021 LARAVEL研发中心
- // +----------------------------------------------------------------------
- // | 官方网站: http://www.laravel.cn
- // +----------------------------------------------------------------------
- // | Author: laravel开发员 <laravel.qq.com>
- // +----------------------------------------------------------------------
- namespace App\Services\Api;
- use App\Models\ExamAccessLogModel;
- use App\Models\VideoCategoryModel;
- use App\Models\VideoCoursesModel;
- use App\Models\VideoModel;
- use App\Services\BaseService;
- use App\Services\ConfigService;
- use App\Services\RedisService;
- /**
- * 视频课服务-服务类
- * @author laravel开发员
- * @since 2020/11/11
- * @package App\Services\Api
- */
- class CourseService extends BaseService
- {
- // 静态对象
- protected static $instance = null;
- /**
- * 构造函数
- * @author laravel开发员
- * @since 2020/11/11
- */
- public function __construct()
- {
- $this->model = new VideoCoursesModel();
- }
- /**
- * 静态入口
- */
- public static function make()
- {
- if (!self::$instance) {
- self::$instance = new static();
- }
- return self::$instance;
- }
- /**
- * 获取
- * @param $type
- * @param int $num
- * @return array|mixed
- */
- public function getListByType($type, $num = 0)
- {
- $num = $num? $num : \App\Services\ConfigService::make()->getConfigByCode('show_course_num', 4);
- $cacheKey = "caches:videos:type_list_{$type}_{$num}";
- $datas = RedisService::get($cacheKey);
- if($datas){
- return $datas;
- }
- $datas = VideoModel::where(['type'=>$type,'status'=>1,'mark'=>1])
- ->select(['id','video_name','poster','type','description','is_recommend','status'])
- ->orderBy('is_recommend','asc')
- ->orderBy('create_time','desc')
- ->limit($num)
- ->get();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- foreach ($datas as &$item){
- $item['poster'] = $item['poster'] ? get_image_url($item['poster']) : '';
- }
- RedisService::set($cacheKey, $datas, rand(3600, 7200));
- }
- return $datas;
- }
- /**
- * 获取分类下视频课
- * @param int
- * @return array|mixed
- */
- public function getListByCate($params)
- {
- $type = isset($params['type'])? $params['type'] : 0;
- $sc = isset($params['sc'])? $params['sc'] : 0;
- $cacheKey = "caches:videos:list_by_cate:{$type}_".md5(json_encode($params));
- $datas = RedisService::get($cacheKey);
- // 视频课访问次数统计
- if(empty($sc)){
- ExamAccessLogModel::saveLog(date('Y-m-d'), $type, 20);
- }
- if($datas){
- return $datas;
- }
- //
- $datas = VideoCategoryModel::with(['courses'=>function($query) use($params){
- $kw = isset($params['keyword'])? trim($params['keyword']) : '';
- if($kw){
- $query->where(function($query) use($kw){
- $query->where('video_name',"like","%{$kw}%")
- ->orwhere('description',"like","%{$kw}%");
- });
- }
- $type = isset($params['type'])? intval($params['type']) : 0;
- if($type>0){
- $query->where('type',$type);
- }
- }])->distinct()
- ->from('videos_categorys as a')
- ->where(['a.status'=>1,'a.mark'=>1])
- ->select(['a.id','a.name','a.sort','a.icon'])
- ->orderBy('a.sort','desc')
- ->get();
- $datas = $datas? $datas->toArray() : [];
- if($datas){
- foreach ($datas as &$item){
- $item['icon'] = $item['icon']? get_image_url($item['icon']) : '';
- }
- RedisService::set($cacheKey, $datas, rand(300,600));
- }
- return $datas;
- }
- /**
- * 获取视频集下课程列表
- * @param int
- * @return array|mixed
- */
- public function getListByGroup($groupId,$params,$pageSize)
- {
- $page = isset($params['page'])? $params['page'] : 1;
- $type = isset($params['type'])? $params['type'] : 0;
- $cid = isset($params['cid'])? $params['cid'] : 0;
- $sc = isset($params['sc'])? $params['sc'] : 0;
- $cacheKey = "caches:videos:list_by_group:{$groupId}_{$page}_{$pageSize}_{$type}".md5(json_encode($params));
- $datas = RedisService::get($cacheKey);
- // 视频课访问次数统计
- if(empty($sc)){
- ExamAccessLogModel::saveLog(date('Y-m-d'), $type, 20);
- }
- if($datas){
- return $datas;
- }
- $list = $this->model->leftJoin('videos as b','b.id','=','videos_courses.video_id')
- ->with(['vip'])
- ->where(['videos_courses.video_id'=> $groupId,'videos_courses.status'=>1,'videos_courses.mark'=>1,'b.status'=>1,'b.mark'=>1])
- ->where(function($query) use($params){
- $kw = isset($params['keyword'])? trim($params['keyword']) : '';
- if($kw){
- $query->where('videos_courses.course_name',"like","%{$kw}%")
- ->orwhere('videos_courses.description',"like","%{$kw}%");
- }
- })
- ->where(function($query) use($cid){
- if($cid){
- $query->whereNotIn('videos_courses.id', [$cid]);
- }
- })
- ->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'])
- ->withCount(['courses','learns'])
- ->orderBy('videos_courses.sort','desc')
- ->orderBy('videos_courses.id','asc')
- ->paginate($pageSize > 0 ? $pageSize : 9999999);
- $list = $list? $list->toArray() :[];
- if($list){
- foreach ($list['data'] as &$item){
- }
- }
- $rows = isset($list['data'])? $list['data'] : [];
- $datas = [
- 'pageSize'=> $pageSize,
- 'total'=> isset($list['total'])? $list['total'] : 0,
- 'list'=> $rows
- ];
- if($rows){
- RedisService::set($cacheKey, $datas, rand(300,600));
- }
- return $datas;
- }
- /**
- * 视频课详情
- * @param $userId 用户
- * @param $id 课程ID
- * @return array|mixed
- */
- public function getInfo($userId, $id)
- {
- $cacheKey = "caches:videos:info_{$userId}_{$id}";
- $data = RedisService::get($cacheKey, $cacheKey);
- if($data){
- return $data;
- }
- $data = $this->model->from('videos_courses as videos_courses')
- ->leftJoin('videos as b','b.id','=','videos_courses.video_id')
- /*->leftJoin('videos_learn_logs as c',function($join) use($userId){
- $join->on('c.course_id','=','videos_courses.id')
- ->where(['c.user_id'=>$userId,'c.status'=>1,'c.mark'=>1]);
- })*/
- ->with(['vip'])
- ->withCount(['courses','learns'])
- ->where(['videos_courses.id'=>$id,'videos_courses.status'=>1,'videos_courses.mark'=>1])
- ->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'])
- ->first();
- $data = $data? $data->toArray() : [];
- if($data){
- // 验证付费视频是否有播放权限
- $fee = isset($data['fee'])? $data['fee'] : 0;
- $buyVipData = isset($data['vip'])? $data['vip'] : [];
- $data['buy_vip'] = $buyVipData? 1 : 0;
- $data['can_play'] = $buyVipData || $fee<=0? 1 : 0;
- $data['preview_time'] = ConfigService::make()->getConfigByCode('course_play_preview_time', 3);
- // 播放
- RedisService::set($cacheKey, $data, rand(5, 10));
- }
- $this->model->where(['id'=> $id])->increment('views', 1);
- return $data;
- }
- }
|