Subject.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\model\special\SpecialSubject;
  13. use app\admin\model\special\Special;
  14. use think\Url;
  15. use service\FormBuilder as Form;
  16. use service\JsonService as Json;
  17. use app\admin\controller\AuthController;
  18. /**
  19. * 课程分类控制器
  20. * Class Subject
  21. * @package app\admin\controller\special
  22. */
  23. class Subject extends AuthController
  24. {
  25. public function index()
  26. {
  27. return $this->fetch();
  28. }
  29. public function get_subject_list()
  30. {
  31. $where = parent::getMore([
  32. ['page', 1],
  33. ['limit', 20],
  34. ['pid', $this->request->param('pid', '')],
  35. ['name', ''],
  36. ]);
  37. return Json::successful(SpecialSubject::get_subject_list($where));
  38. }
  39. /**
  40. * 创建分类
  41. * @param int $id
  42. * @return mixed
  43. * @throws \think\exception\DbException
  44. */
  45. public function create($id = 0)
  46. {
  47. $cate = $id > 0 ? SpecialSubject::get($id) : [];
  48. $this->assign(['cate' => json_encode($cate), 'id' => $id]);
  49. return $this->fetch();
  50. }
  51. /**获取一级分类
  52. * @param int $sid
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\ModelNotFoundException
  55. * @throws \think\exception\DbException
  56. */
  57. public function get_cate_list()
  58. {
  59. $cate = SpecialSubject::specialCategoryAll(1);
  60. $array = [];
  61. $oneCate['id'] = 0;
  62. $oneCate['name'] = '顶级分类';
  63. array_push($array, $oneCate);
  64. foreach ($cate as $key => $value) {
  65. array_push($array, $value);
  66. }
  67. return Json::successful($array);
  68. }
  69. /**
  70. * 新增或者修改
  71. *
  72. * @return json
  73. */
  74. public function save($id = 0)
  75. {
  76. $post = parent::postMore([
  77. ['name', ''],
  78. ['pic', ''],
  79. ['grade_id', 0],
  80. ['sort', 0],
  81. ['is_show', 0],
  82. ]);
  83. if (!$post['name']) return Json::fail('请输入分类名称');
  84. if ($post['grade_id'] && !$post['pic']) return Json::fail('请选择分类图标');
  85. if ($id) {
  86. $cate = SpecialSubject::get($id);
  87. if (!$cate['grade_id'] && $post['grade_id'] && SpecialSubject::be(['grade_id' => $id, 'is_del' => 0])) return Json::fail('无法移动有下级的分类');
  88. if (SpecialSubject::where(['name' => $post['name'], 'is_del' => 0])->where('id', '<>', $id)->count() >= 1) return Json::fail('分类名称已存在');
  89. $res = SpecialSubject::edit($post, $id);
  90. if ($res)
  91. return Json::successful('修改成功');
  92. else
  93. return Json::fail('修改失败');
  94. } else {
  95. $post['add_time'] = time();
  96. if (SpecialSubject::be(['name' => $post['name'], 'is_del' => 0])) {
  97. return Json::fail('分类名称已存在!');
  98. }
  99. $res = SpecialSubject::set($post);
  100. if ($res)
  101. return Json::successful('添加成功');
  102. else
  103. return Json::fail('添加失败');
  104. }
  105. }
  106. /**
  107. * 快速编辑
  108. *
  109. * @return json
  110. */
  111. public function set_value($field = '', $id = '', $value = '')
  112. {
  113. ($field == '' || $id == '' || $value == '') && Json::fail('缺少参数');
  114. $res = parent::getDataModification('subject', $id, $field, $value);
  115. if ($res)
  116. return Json::successful('保存成功');
  117. else
  118. return Json::fail('保存失败');
  119. }
  120. /**二级分是否显示快捷操作
  121. * @param string $is_show
  122. * @param string $id
  123. * @return mixed
  124. */
  125. public function set_show($is_show = '', $id = '')
  126. {
  127. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  128. $res = parent::getDataModification('subject', $id, 'is_show', (int)$is_show);
  129. if ($res) {
  130. return Json::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  131. } else {
  132. return Json::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  133. }
  134. }
  135. /**
  136. * 删除
  137. *
  138. * @return json
  139. */
  140. public function delete($id = 0)
  141. {
  142. if (!$id) return Json::fail('缺少参数');
  143. $subject = SpecialSubject::get($id);
  144. if ($subject['grade_id']) {
  145. if (Special::where('subject_id', $id)->where('is_del', 0)->count()) return Json::fail('暂无法删除,请先去除专题关联');
  146. } else {
  147. if (SpecialSubject::where('grade_id', $id)->where('is_del', 0)->count()) return Json::fail('暂无法删除,请删除下级分类');
  148. }
  149. $res = parent::getDataModification('subject', $id, 'is_del', 1);
  150. if ($res)
  151. return Json::successful('删除成功');
  152. else
  153. return Json::fail('删除成功');
  154. }
  155. }