Course.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\special;
  12. use app\admin\controller\AuthController;
  13. use app\admin\model\special\SpecialCourse;
  14. use service\JsonService;
  15. use service\FormBuilder as Form;
  16. use app\admin\model\special\Special;
  17. use think\response\Json;
  18. use think\Url;
  19. /**
  20. * 课程控制器
  21. * Class Grade
  22. * @package app\admin\controller\special
  23. */
  24. class Course extends AuthController
  25. {
  26. /**
  27. * 课程列表
  28. * @param int $special_id
  29. * @return mixed
  30. */
  31. public function index($special_id = 0)
  32. {
  33. $this->assign('special_id', $special_id);
  34. return $this->fetch();
  35. }
  36. /**
  37. * 添加课程
  38. * @param int $special_id
  39. * @param int $id
  40. * @return mixed|void
  41. * @throws \think\exception\DbException
  42. */
  43. public function add_course($special_id = 0, $id = 0)
  44. {
  45. if (!$special_id) return $this->failed('缺少参数');
  46. $special = Special::get($special_id);
  47. if (!$special) return $this->failed('并没有查到相关专题');
  48. if ($special->is_del) return $this->failed('此专题已被删除');
  49. if ($id) $course = SpecialCourse::get($id);
  50. $form = [
  51. Form::input('title', '专题名称', $special->title)->disabled(true),
  52. Form::input('course_name', '课程名称', isset($course) ? $course->course_name : ''),
  53. Form::number('sort', '排序', isset($course) ? $course->sort : ''),
  54. Form::radio('is_show', '状态', isset($course) ? $course->is_show : 1)->options([['label' => '显示', 'value' => 1], ['label' => '隐藏', 'value' => 0]])
  55. ];
  56. $form = Form::make_post_form('添加课程', $form, Url::build('save_course', ['special_id' => $special_id, 'id' => $id]), 3);
  57. $this->assign(compact('form'));
  58. return $this->fetch('public/form-builder');
  59. }
  60. /**
  61. * 保存课程
  62. * @param int $special_id
  63. * @param int $id
  64. */
  65. public function save_course($special_id = 0, $id = 0)
  66. {
  67. if (!$special_id) return JsonService::fail('缺少参数');
  68. $post = parent::postMore([
  69. ['course_name', ''],
  70. ['sort', 0],
  71. ['is_show', 1],
  72. ]);
  73. if (!$post['course_name']) return JsonService::fail('请输入课程名称');
  74. $post['special_id'] = $special_id;
  75. if ($id) {
  76. SpecialCourse::update($post, ['id' => $id]);
  77. return JsonService::successful('修改成功');
  78. } else {
  79. $post['add_time'] = time();
  80. if (SpecialCourse::set($post))
  81. return JsonService::successful('添加成功');
  82. else
  83. return JsonService::fail('添加失败');
  84. }
  85. }
  86. /**
  87. * 专题列表
  88. */
  89. public function course_list()
  90. {
  91. $where = parent::getMore([
  92. ['special_id', 0],
  93. ['is_show', ''],
  94. ['course_name', ''],
  95. ['page', 1],
  96. ['limit', 20],
  97. ]);
  98. return JsonService::successlayui(SpecialCourse::getCourseList($where));
  99. }
  100. /**
  101. * 删除课程
  102. * @param int $id
  103. */
  104. public function delete($id = 0)
  105. {
  106. if (!$id) return JsonService::fail('缺少参数');
  107. if (SpecialCourse::DelCourse($id))
  108. return JsonService::successful('删除成功');
  109. else
  110. return JsonService::fail(SpecialCourse::getErrorInfo('删除失败'));
  111. }
  112. /**
  113. * 设置单个产品上架|下架
  114. *
  115. * @return json
  116. */
  117. public function set_show($is_show = '', $id = '')
  118. {
  119. ($is_show == '' || $id == '') && JsonService::fail('缺少参数');
  120. $res = SpecialCourse::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  121. if ($res) {
  122. return JsonService::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  123. } else {
  124. return JsonService::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  125. }
  126. }
  127. /**
  128. * 快速编辑
  129. *
  130. * @return json
  131. */
  132. public function set_value($field = '', $id = '', $value = '')
  133. {
  134. $field == '' || $id == '' || $value == '' && JsonService::fail('缺少参数');
  135. if (SpecialCourse::where(['id' => $id])->update([$field => $value]))
  136. return JsonService::successful('保存成功');
  137. else
  138. return JsonService::fail('保存失败');
  139. }
  140. }