Course.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace app\adminapi\controller\v1\course;
  3. use app\adminapi\controller\AuthController;
  4. use app\models\course\CourseModel;
  5. use crmeb\services\{UtilService as Util};
  6. use think\Request;
  7. class Course extends AuthController
  8. {
  9. /**
  10. * 显示资源列表
  11. *
  12. * @return \think\Response
  13. */
  14. public function index()
  15. {
  16. $where = Util::getMore([
  17. ['page', 1],
  18. ['limit', 10],
  19. ['area', ''],
  20. ['title', '']
  21. ], $this->request);
  22. $list = CourseModel::systemPage($where);
  23. return $this->success($list);
  24. }
  25. /**
  26. * 保存新建的资源
  27. *
  28. * @param \think\Request $request
  29. * @return \think\Response
  30. */
  31. public function save(Request $request)
  32. {
  33. $data = Util::postMore([
  34. ['id', 0],
  35. ['area', ''],
  36. ['title', ''],
  37. ['image', ''],
  38. ['intro', '']
  39. ]);
  40. $data['add_time'] = time();
  41. if ($data['id']) {
  42. $id = $data['id'];
  43. unset($data['id']);
  44. $res = CourseModel::edit($data, $id, 'id');
  45. if($res)
  46. return $this->success('修改成功!', ['id' => $id]);
  47. else
  48. return $this->fail('修改失败!');
  49. } else {
  50. $res = CourseModel::create($data);
  51. return $this->success('添加成功!', ['id' => $res->id]);
  52. }
  53. }
  54. /**
  55. * 显示指定的资源
  56. *
  57. * @param int $id
  58. * @return \think\Response
  59. */
  60. public function read($id)
  61. {
  62. $info = CourseModel::getOne($id);
  63. return $this->success(compact('info'));
  64. }
  65. /**
  66. * 删除指定资源
  67. *
  68. * @param int $id
  69. * @return \think\Response
  70. */
  71. public function delete($id)
  72. {
  73. if (!$id) return $this->fail('数据不存在');
  74. $res = CourseModel::get($id);
  75. if (!$res) return $this->fail('数据不存在!');
  76. if ($res['is_del']) return $this->fail('已删除!');
  77. $data['is_del'] = 1;
  78. if (CourseModel::edit($data, $id))
  79. return $this->success('删除成功!');
  80. else
  81. return $this->fail('删除失败,请稍候再试!');
  82. }
  83. }