// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\admin\controller; use think\response\Json; use app\admin\controller\Controller; use app\common\model\UploadFile; use app\admin\model\Course as CourseModel; use think\facade\Db; class Course extends Controller{ //查看所有分类列表 public function getCourseType(): Json { $courseCategory = Db::name('course_category')->select(); return $this->renderSuccess(compact('courseCategory')); } //分页查询课程列表 public function list(): Json { $courseCategoryId =input('courseCategoryId'); $pageSize = input('pageSize'); $model = new CourseModel(); $list = $model->getList(intval($courseCategoryId),intval($pageSize)); if(empty($list)){ return $this->renderError("没有课程"); } foreach($list as $value){ $value['image_url'] = UploadFile::withoutGlobalScope()->where('file_id', '=',$value['image_id'])->find(); $value['video_url'] = UploadFile::withoutGlobalScope()->where('file_id', '=',$value['video_id'])->find(); } return $this->renderSuccess(compact('list')); } //分页查询课程列表 public function getCourseList(): Json { $model = new CourseModel(); $list = $model->list($this->request->param()); if(empty($list)){ return $this->renderError("没有课程"); } foreach($list as $value){ $value['image_url'] = UploadFile::withoutGlobalScope()->where('file_id', '=',$value['image_id'])->find(); $value['video_url'] = UploadFile::withoutGlobalScope()->where('file_id', '=',$value['video_id'])->find(); } return $this->renderSuccess(compact('list')); } //新增课程 public function add(): Json { $params = $this->postForm(); // $params['create_time'] = time(); $model = new CourseModel(); $result = $model->save($params); return $this->renderSuccess('新增成功'); return $this->renderSuccess('添加成功'); } //更新课程 public function edit(int $courseId): Json { //获取课程id $course = CourseModel::withoutGlobalScope()->where('course_id',$courseId)->find(); $params = $this->postForm(); if(empty($course)){ return $this->renderError("没有课程"); } $result = CourseModel::withoutGlobalScope()->where('course_id',$courseId)->update($params); return $this->renderSuccess('更新成功'); } //删除课程 public function delete(int $courseId): Json{ $params = $this->postForm(); $course = CourseModel::withoutGlobalScope()->where('course_id',$courseId)->find(); //print_r($course); if(empty($course)){ return $this->renderError("没有课程"); } CourseModel::withoutGlobalScope()->delete($courseId); return $this->renderSuccess('删除成功'); } }