324 lines
11 KiB
PHP
324 lines
11 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller\course;
|
|
|
|
use app\common\controller\Api;
|
|
use app\common\model\user\Study as StudyModel;
|
|
use think\Cache;
|
|
/**
|
|
* 课程
|
|
*/
|
|
class Course extends Api
|
|
{
|
|
// 无需登录的接口,*表示全部
|
|
protected $noNeedLogin = ['*'];
|
|
protected $noNeedRight = '*';
|
|
protected $SubscriptionModel;
|
|
protected $groupBindModel;
|
|
protected $model;
|
|
|
|
public function _initialize()
|
|
{
|
|
header("Access-Control-Allow-Origin:*");
|
|
parent::_initialize();
|
|
|
|
$this->model = new \app\admin\model\course\Course;
|
|
$this->styleModel = new StudyModel();
|
|
$this->commentModel = new \app\common\model\course\Comment();
|
|
$this->groupBindModel = new \app\api\model\course\GroupBind();
|
|
$this->columnBindModel = new \app\api\model\course\ColumnBind();
|
|
$this->SubscriptionModel = new \app\common\model\user\Subscription();
|
|
}
|
|
/**
|
|
* 获取课程分组列表
|
|
* @return void
|
|
*/
|
|
public function getList(){
|
|
|
|
$limit = input('limit',10);
|
|
$page = input('page',1);
|
|
$groupId = input('group');
|
|
$isDeep = input('is_deep', 1);
|
|
|
|
$searchKey = input('searchKey','');
|
|
$sort = input('sort','time');
|
|
$type = input('type','');
|
|
|
|
if ($groupId) {
|
|
$groupModel = new \app\admin\model\course\Group();
|
|
$groupIds = [];
|
|
|
|
if ($isDeep) {
|
|
$groupIds = $this->getRelatedGroupIds($groupModel, $groupId);
|
|
} else {
|
|
$groupIds = [$groupId];
|
|
}
|
|
|
|
$list = $this->groupBindModel->getCourseListByGroupIds($groupIds, [
|
|
'limit'=>$limit,
|
|
'page'=>$page,
|
|
'searchKey'=>$searchKey,
|
|
'sort'=>$sort,
|
|
'type'=>$type,
|
|
]);
|
|
} else {
|
|
$list = $this->groupBindModel->getCourseList(0, [
|
|
'limit'=>$limit,
|
|
'page'=>$page,
|
|
'searchKey'=>$searchKey,
|
|
'sort'=>$sort,
|
|
'type'=>$type,
|
|
]);
|
|
}
|
|
|
|
$this->success('获取成功', $list);
|
|
}
|
|
|
|
/**
|
|
* 获取分组及其所有相关分组ID(包括上级和下级)
|
|
* @param object $groupModel
|
|
* @param int $groupId
|
|
* @return array
|
|
*/
|
|
protected function getRelatedGroupIds($groupModel, $groupId)
|
|
{
|
|
$groupIds = [$groupId];
|
|
|
|
$this->getParentGroupIds($groupModel, $groupId, $groupIds);
|
|
|
|
$this->getChildGroupIdsRecursive($groupModel, $groupId, $groupIds);
|
|
|
|
return array_unique($groupIds);
|
|
}
|
|
|
|
/**
|
|
* 递归获取上级分组ID
|
|
* @param object $groupModel
|
|
* @param int $groupId
|
|
* @param array $groupIds
|
|
*/
|
|
protected function getParentGroupIds($groupModel, $groupId, &$groupIds)
|
|
{
|
|
$group = $groupModel
|
|
->where('id', $groupId)
|
|
->where('status', 1)
|
|
->where('uniacid', UNIACID)
|
|
->find();
|
|
|
|
if ($group && $group['parent_id'] > 0) {
|
|
$parent = $groupModel
|
|
->where('id', $group['parent_id'])
|
|
->where('status', 1)
|
|
->where('uniacid', UNIACID)
|
|
->find();
|
|
|
|
if ($parent) {
|
|
$groupIds[] = $parent['id'];
|
|
$this->getParentGroupIds($groupModel, $parent['id'], $groupIds);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 递归获取下级分组ID
|
|
* @param object $groupModel
|
|
* @param int $parentId
|
|
* @param array $groupIds
|
|
*/
|
|
protected function getChildGroupIdsRecursive($groupModel, $parentId, &$groupIds)
|
|
{
|
|
$children = $groupModel
|
|
->where('parent_id', $parentId)
|
|
->where('status', 1)
|
|
->where('uniacid', UNIACID)
|
|
->column('id');
|
|
|
|
foreach ($children as $childId) {
|
|
$groupIds[] = $childId;
|
|
$this->getChildGroupIdsRecursive($groupModel, $childId, $groupIds);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* 获取课程详情
|
|
* @return void
|
|
*/
|
|
public function detail(){
|
|
$id = input('id',0);
|
|
$addViews = input('add_views',0);//是否增加流量,因为直播需要频繁刷新
|
|
|
|
|
|
$row = $this->model->get($id);
|
|
|
|
if (!$row) {
|
|
$this->error(__('No Results were found'));
|
|
}
|
|
$row = $row->toArray();
|
|
$row = $this->model->structrue($row, 'decode');
|
|
|
|
|
|
//增加浏览量
|
|
if($addViews){
|
|
$this->model->where(['id'=>$id])->setInc('views');
|
|
}
|
|
|
|
$buyData = \app\common\model\course\Course::getPrice($row['id']);
|
|
|
|
$row = array_merge($row,$buyData);
|
|
|
|
$row['subscription'] = \app\common\model\user\Subscription::getSubscriptionAuth($this->auth->id,$row['id']);
|
|
//是否拥有付费会员卡免费权益(用于前端显示【加入学习】按钮)
|
|
$row['vip_free_access'] = \app\common\model\user\Subscription::getVipFreeAccess($this->auth->id,$row['id']);
|
|
|
|
if($row['type'] == 'live'){
|
|
$row['try_read_content'] = $row['detail'];
|
|
$row['try_read_status'] = 1;
|
|
$row['live_start_time'] = strtotime($row['live_start_time']);
|
|
$row['live_end_time'] = strtotime($row['live_end_time']);
|
|
|
|
//获取直播地址
|
|
$row['live_room'] = (new \app\common\model\live\Room)->courseGetRoom($row['id']);
|
|
|
|
if($row['live_type'] == 2){
|
|
//要计算当前播放进度
|
|
$row['live_current_time'] = time() - $row['live_start_time'];
|
|
}
|
|
|
|
if($row['subscription']){
|
|
//判断有没有成为直播课程成员
|
|
$userInfo = (new \app\admin\model\live\User)->joinLive($this->auth->id,$row['id']);
|
|
|
|
if($userInfo['black']){
|
|
return $this->error("你已被管理员拉黑,无法观看直播");
|
|
}
|
|
}
|
|
|
|
}
|
|
$row = $this->courseDataLimit($row);
|
|
|
|
$row['is_virtual_pay'] = \app\common\library\pay\VirtualPayService::isUseVirtualPay($row['type']);
|
|
if ($row['is_virtual_pay']) {
|
|
$row = \app\common\library\pay\VirtualPayService::convertPriceFields($row, ['price', 'price_marking']);
|
|
}
|
|
|
|
return $this->success("获取成功", $row);
|
|
}
|
|
|
|
|
|
/**
|
|
* 课程数据限制
|
|
* 根据是否已订阅返回相关数据
|
|
* @param $courseInfo
|
|
* @return void
|
|
*/
|
|
public function courseDataLimit($courseInfo){
|
|
|
|
$showField = ['sales_type','type','live_type','live_screen_ratio','live_current_time','live_room','limit_copy','bullet_screen','pay_type','warm_up_cover','type_text','views','video_path','video_patch','name','briefing','live_start_time','live_end_time','price','price_marking','createtime','cover','bind_data','audio_path','detail','id','try_listen_content','try_read_status','try_watch_status','try_watch_content','try_read_content','try_listen_status','subscription','vip_free_access'];
|
|
|
|
foreach ($courseInfo as $key => &$item){
|
|
if(!in_array($key,$showField)){
|
|
unset($courseInfo[$key]);
|
|
}
|
|
}
|
|
|
|
if(!$courseInfo['subscription']){
|
|
$courseInfo['detail'] = '';
|
|
$courseInfo['audio_path'] = [];
|
|
$courseInfo['video_path'] = [];
|
|
|
|
$bindDataFiled = ['id','filename','filesize','filetype','auth'];
|
|
foreach ($courseInfo['bind_data'] as &$item){
|
|
if(!isset($item['auth'])){
|
|
$item['auth'] = 'previewdown';
|
|
}
|
|
foreach ($item as $key => $value){
|
|
if(!in_array($key,$bindDataFiled)){
|
|
unset($item[$key]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
$courseInfo['study_num'] = $this->styleModel->getCourseStudyNum($courseInfo['id']);
|
|
$courseInfo['comment_num'] = $this->commentModel->getCourseCommentCount($courseInfo['id']);
|
|
|
|
$courseInfo['try_listen_status'] = !empty($courseInfo['try_listen_status']) ? true:false;
|
|
$courseInfo['try_read_status'] = !empty($courseInfo['try_read_status']) ? true:false;
|
|
$courseInfo['try_watch_status'] = !empty($courseInfo['try_watch_status']) ? true:false;
|
|
//跑马灯
|
|
$courseInfo['bullet_screen'] = $courseInfo['bullet_screen'] == 1 ? $this->auth->username : 0;
|
|
|
|
$vodFileType = ['audio_path','video_path','try_listen_content','try_watch_content'];
|
|
foreach ($vodFileType as $vodFile){
|
|
if(isset($courseInfo[$vodFile]) && $courseInfo[$vodFile] && $courseInfo[$vodFile]['storage'] == 'alivod'){
|
|
$courseInfo[$vodFile]['fullurl'] = \addons\alivod\library\Alivod::parseAliovdTag($courseInfo[$vodFile]['fullurl']);
|
|
}
|
|
}
|
|
|
|
$courseInfo['detail'] = \addons\alivod\library\Alivod::parseContentAliovdTag($courseInfo['detail']);
|
|
|
|
$courseInfo['try_read_content'] = \addons\alivod\library\Alivod::parseContentAliovdTag($courseInfo['try_read_content']);
|
|
|
|
if($courseInfo['bind_data']){
|
|
foreach ($courseInfo['bind_data'] as &$item){
|
|
if(isset($item['fullurl'])){
|
|
try{
|
|
$parseVodTag = \addons\alivod\library\Alivod::getMezzanineInfo($item['fullurl']);
|
|
if($parseVodTag && isset($parseVodTag->Mezzanine) && isset($parseVodTag->Mezzanine->FileURL)){
|
|
$item['fullurl'] = $parseVodTag->Mezzanine->FileURL;
|
|
}
|
|
}catch (\Exception $e){}
|
|
}
|
|
}
|
|
}
|
|
|
|
return $courseInfo;
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
* 获取课程所属专栏
|
|
* @return void
|
|
*/
|
|
public function getBelongsColumn($course_id)
|
|
{
|
|
|
|
$cacheData = Cache::get(\app\common\constant\cache\Keys::COURSE_BELONGS_COLUMN.$course_id);
|
|
|
|
if($cacheData){
|
|
return $this->success("获取成功", $cacheData);
|
|
}
|
|
|
|
$list = $this->columnBindModel
|
|
->with(['column'])
|
|
->where([
|
|
'course_id'=>$course_id,
|
|
'column.status'=>1,
|
|
'column_bind.type'=>1,
|
|
'column_bind.status'=>1
|
|
])
|
|
->select();
|
|
|
|
if($list){
|
|
foreach ($list as &$item){
|
|
$item->count = $this->columnBindModel->where([
|
|
'column_id'=>$item['column_id']
|
|
])->count();
|
|
$item->getRelation('column')->visible(['name','cover','id']);
|
|
$item->visible(['id','column','count']);
|
|
}
|
|
|
|
$list = collection($list)->toArray();
|
|
}
|
|
|
|
Cache::tag(\app\common\constant\cache\Keys::COURSE_TAG)->set(\app\common\constant\cache\Keys::COURSE_BELONGS_COLUMN.$course_id,$list);
|
|
|
|
|
|
return $this->success("获取成功", $list);
|
|
}
|
|
|
|
}
|