lszyh
haoyuntao 7 months ago
parent 685c14e29f
commit db4ca90183
  1. 5
      app/admin/controller/Course.php
  2. 14
      app/admin/model/Course.php
  3. 9
      app/api/controller/Course.php
  4. 15
      app/common/model/Course.php

@ -30,9 +30,10 @@ class Course extends Controller{
//分页查询课程列表
public function getCourseList(): Json
{
$courseCategoryId =input('courseCategoryId');
$pageSize = input('pageSize');
$model = new CourseModel();
$list = $model->getList();
$list = $model->getList(intval($courseCategoryId),intval($pageSize));
if(empty($list)){
return $this->renderError("没有课程");
}

@ -22,10 +22,16 @@ class Course extends CourseModel
* @return \think\Paginator
* @throws \think\db\exception\DbException
*/
public function getList(): \think\Paginator
{
return $this->order(['sort' => 'asc', 'create_time' => 'desc'])->paginate(15);
public function getList(int $courseCategoryId, int $pageSize ): \think\Paginator
{
if(empty($courseCategoryId)){
$list = $this->where('status',1)->order(['sort' => 'asc', 'create_time' => 'desc'])->paginate($pageSize);
} else {
$list = $this->where('course_category_id',$courseCategoryId)
->where('status',1)->order(['sort' => 'asc', 'create_time' => 'desc'])->paginate($pageSize);
}
return $list;
}
}

@ -23,8 +23,13 @@ class Course extends Controller
//根据分类查询
public function getCourseList(): Json
{
$course_Category_id = input('courseCategoryId');
$list = CourseModel::withoutGlobalScope()->where('course_category_id',$course_Category_id)->select();
$courseCategoryId = input('courseCategoryId');
$pageSize = input('pageSize');
$list = CourseModel::withoutGlobalScope()
->when($courseCategoryId, function ($query) use ($courseCategoryId) {
return $query->where('course_category_id', $courseCategoryId);
})->paginate($pageSize);
if(empty($list)){
return $this->renderError("没有课程");
}

@ -47,4 +47,19 @@ class Course extends BaseModel
return self::create($date);
}
public static function getCouser(int $courseCategoryId, int $pageSize): \think\Paginator
{
if (empty($courseCategoryId)) {
$list = static::where('status', 1)
->order(['sort' => 'asc', 'create_time' => 'desc'])
->paginate($pageSize);
} else {
// 注意:这里的逻辑实际上与if分支重复,可以考虑合并条件以减少代码重复
$list = static::where(['course_category_id' => $courseCategoryId, 'status' => 1])
->order(['sort' => 'asc', 'create_time' => 'desc'])
->paginate($pageSize);
}
return $list;
}
}
Loading…
Cancel
Save