初始化项目:添加后端代码、ThinkPHP框架、前端资源
This commit is contained in:
@@ -0,0 +1,248 @@
|
||||
<?php
|
||||
|
||||
namespace app\api\controller\course;
|
||||
|
||||
use app\common\controller\Api;
|
||||
use mysql_xdevapi\Collection;
|
||||
|
||||
/**
|
||||
* 课程
|
||||
*/
|
||||
class Column extends Api
|
||||
{
|
||||
// 无需登录的接口,*表示全部
|
||||
protected $noNeedLogin = ['*'];
|
||||
protected $noNeedRight = '*';
|
||||
public function _initialize()
|
||||
{
|
||||
parent::_initialize();
|
||||
$this->columnDirModel = new \app\admin\model\course\ColumnDir;
|
||||
$this->model = new \app\admin\model\course\ColumnBind;
|
||||
$this->courseModel = new \app\admin\model\course\Course;
|
||||
}
|
||||
/**
|
||||
* 获取专栏目录
|
||||
* @return void
|
||||
*/
|
||||
public function getDir(){
|
||||
|
||||
$courseId = input("id");
|
||||
$row = $this->columnDirModel->courseIdGetList($courseId);
|
||||
return $this->success("获取成功",$row);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取专栏所属课程
|
||||
* @return mixed
|
||||
*/
|
||||
public function getCourse(){
|
||||
$courseId = input('id');
|
||||
$sort = input('sort','asc');
|
||||
|
||||
$courseInfo = $this->courseModel->get($courseId);
|
||||
|
||||
if(!$courseInfo){
|
||||
return $this->error("获取课程信息错误");
|
||||
}
|
||||
|
||||
$list = $this->model
|
||||
->with(['course'])
|
||||
->where([
|
||||
'column_bind.column_id'=>$courseId,
|
||||
'course.status'=>1
|
||||
])
|
||||
->order("column_bind.sort {$sort}, column_bind.createtime {$courseInfo['column_update']}")
|
||||
->select();
|
||||
|
||||
//判断有没有订阅
|
||||
$isSubscription = \app\common\model\user\Subscription::getSubscriptionAuth($this->auth->id,$courseId);
|
||||
|
||||
if(!empty($list)){
|
||||
foreach ($list as $index => $item){
|
||||
$item['course'] = $item['course']->toArray();
|
||||
$item->getRelation('course')->visible(['name','cover','id','updatetime','views','type','type_text','pay_type']);
|
||||
$item['course'] = $this->courseModel->structrue($item['course'],'decode');
|
||||
$item->visible(['id','course','try','subscription']);
|
||||
$isCourseSubscription = \app\common\model\user\Subscription::getSubscriptionAuth($this->auth->id,$item['course']['id']);
|
||||
|
||||
if($isSubscription || $isCourseSubscription || $item->try){
|
||||
$list[$index]['subscription'] = 1;
|
||||
}else{
|
||||
$list[$index]['subscription'] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->success("获取成功",$list);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取专栏目录 新
|
||||
* @return void
|
||||
*/
|
||||
public function getColumnDir(){
|
||||
$courseId = input('id');
|
||||
$sort = input('sort','asc');
|
||||
|
||||
$courseInfo = $this->courseModel->get($courseId);
|
||||
|
||||
if(!$courseInfo){
|
||||
return $this->error("获取课程信息错误");
|
||||
}
|
||||
|
||||
$list = $this->model
|
||||
->with(['bindcourse'])
|
||||
->where([
|
||||
'column_bind.column_id'=>$courseId,
|
||||
])
|
||||
->order("column_bind.sort {$sort}, column_bind.createtime {$courseInfo['column_update']}")
|
||||
->select();
|
||||
|
||||
|
||||
//判断有没有订阅
|
||||
$isSubscription = \app\common\model\user\Subscription::getSubscriptionAuth($this->auth->id,$courseId);
|
||||
|
||||
if(!empty($list)){
|
||||
foreach ($list as $index => &$item){
|
||||
$item->visible(['id','bindcourse','try','subscription','type','p_id','title']);
|
||||
$item->getRelation('bindcourse')->visible(['name','cover','id','updatetime','views','type','type_text','status','pay_type','no']);
|
||||
$item = $item->toArray();
|
||||
if($item['type'] == 1){
|
||||
if(!$item['bindcourse']['id'] || $item['bindcourse']['status']!=1){
|
||||
unset($list[$index]);
|
||||
continue;
|
||||
}
|
||||
$item['bindcourse'] = $this->courseModel->structrue($item['bindcourse'],'decode');
|
||||
$isCourseSubscription = \app\common\model\user\Subscription::getSubscriptionAuth($this->auth->id,$item['bindcourse']['id']);
|
||||
if($isSubscription || $isCourseSubscription || $item['try']){
|
||||
$list[$index]['subscription'] = 1;
|
||||
}else{
|
||||
$list[$index]['subscription'] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
//重新给数组排序
|
||||
$list = array_values($list);
|
||||
}
|
||||
|
||||
return $this->success("获取成功",$list);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下一课程
|
||||
* @return mixed
|
||||
*/
|
||||
public function getNextCourse() {
|
||||
$courseId = input('course_id');
|
||||
$columnId = input('column_id');
|
||||
|
||||
// 1. 验证专栏和课程是否存在
|
||||
$columnInfo = $this->courseModel->get($columnId);
|
||||
if (!$columnInfo) {
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
$courseInfo = $this->model->where(['column_id' => $columnId, 'course_id' => $courseId])->find();
|
||||
if (!$courseInfo) {
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
// 2. 关键修正:查询专栏下所有课程和目录(包含type=1和type=2)
|
||||
$allItems = $this->model
|
||||
->with(['course'])
|
||||
->where([
|
||||
'course.status'=>1
|
||||
])
|
||||
->where('column_id', $columnId)
|
||||
->field('column_bind.id, column_bind.course_id, column_bind.type, column_bind.sort, column_bind.p_id, column_bind.createtime')
|
||||
->select();
|
||||
|
||||
if (empty($allItems)) {
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
// 3. 构建映射和子元素分组(方便快速查找父元素和子课程)
|
||||
$itemMap = []; // 按 id 映射所有元素(id是column_bind表的主键)
|
||||
$childrenGroups = []; // 按 p_id 分组(p_id对应父目录的id)
|
||||
$courseIdToItemId = []; // 课程id -> column_bind.id 的映射(用于快速定位当前课程)
|
||||
foreach ($allItems as $item) {
|
||||
$itemMap[$item['id']] = $item;
|
||||
$pId = $item['p_id'];
|
||||
if (!isset($childrenGroups[$pId])) {
|
||||
$childrenGroups[$pId] = [];
|
||||
}
|
||||
$childrenGroups[$pId][] = $item;
|
||||
// 关联课程id和column_bind的id(因为传入的是course_id,不是column_bind的id)
|
||||
if ($item['type'] == 1) { // 只有课程有course_id
|
||||
$courseIdToItemId[$item['course_id']] = $item['id'];
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 筛选一级元素(p_id=0的课程/目录)并排序
|
||||
$level1Items = $childrenGroups[0] ?? [];
|
||||
usort($level1Items, function($a, $b) {
|
||||
// 排序规则:sort升序 -> createtime升序
|
||||
if ($a['sort'] != $b['sort']) {
|
||||
return $a['sort'] - $b['sort'];
|
||||
}
|
||||
return strcmp($a['createtime'], $b['createtime']);
|
||||
});
|
||||
|
||||
// 5. 构建最终排序列表(一级元素 + 对应二级课程)
|
||||
$finalSortedList = [];
|
||||
foreach ($level1Items as $item) {
|
||||
$finalSortedList[] = $item;
|
||||
// 如果是目录,且有子课程,将子课程排序后追加
|
||||
if ($item['type'] == 2 && isset($childrenGroups[$item['id']])) {
|
||||
$children = $childrenGroups[$item['id']];
|
||||
// 子课程排序规则:sort升序 -> createtime升序
|
||||
usort($children, function($a, $b) {
|
||||
if ($a['sort'] != $b['sort']) {
|
||||
return $a['sort'] - $b['sort'];
|
||||
}
|
||||
return strcmp($a['createtime'], $b['createtime']);
|
||||
});
|
||||
$finalSortedList = array_merge($finalSortedList, $children);
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 找到当前课程在排序列表中的位置
|
||||
$currentItemId = $courseIdToItemId[$courseId] ?? null;
|
||||
$currentIndex = -1;
|
||||
foreach ($finalSortedList as $index => $item) {
|
||||
if ($item['id'] == $currentItemId) {
|
||||
$currentIndex = $index;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 7. 查找下一个课程(跳过目录,只找type=1的)
|
||||
$nextCourse = null;
|
||||
$listCount = count($finalSortedList);
|
||||
for ($i = $currentIndex + 1; $i < $listCount; $i++) {
|
||||
$candidate = $finalSortedList[$i];
|
||||
if ($candidate['type'] == 1) { // 只取课程
|
||||
$nextCourse = $candidate;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!$nextCourse) {
|
||||
return $this->success('');
|
||||
}
|
||||
|
||||
// 8. 关联课程详情并返回
|
||||
$nextCourseInfo = $this->courseModel->get($nextCourse['course_id']);
|
||||
if (!$nextCourseInfo) {
|
||||
return $this->success();
|
||||
}
|
||||
|
||||
// 整理返回字段
|
||||
$result = $nextCourseInfo->visible(['name', 'column_id', 'cover', 'id', 'updatetime', 'views', 'type', 'type_text', 'status', 'pay_type', 'no'])->toArray();
|
||||
$result['column_id'] = $columnId;
|
||||
|
||||
return $this->success("获取成功", $result);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user