Grade.php 5.6 KB

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