Grade.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\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_name', ''],
  36. ]);
  37. return Json::successlayui(GradeModel::getAllList($where));
  38. }
  39. /**
  40. * 创建分类
  41. * @param int $id
  42. * @return mixed
  43. * @throws \think\exception\DbException
  44. */
  45. public function create($id = 0,$sid=0,$level=1,$pid=0)
  46. {
  47. $cate=[];
  48. if ($id && $sid==0){
  49. $cate = GradeModel::get($id);
  50. }else if($id==0 && $sid){
  51. $cate = SpecialSubject::get($sid);
  52. }
  53. $this->assign(['cate'=>json_encode($cate),'id'=>$id,'sid'=>$sid,'level'=>$level,'pid'=>$pid]);
  54. return $this->fetch();
  55. }
  56. /**获取一级分类
  57. * @param int $sid
  58. * @throws \think\db\exception\DataNotFoundException
  59. * @throws \think\db\exception\ModelNotFoundException
  60. * @throws \think\exception\DbException
  61. */
  62. public function get_cate_list($level = 0)
  63. {
  64. $cate= GradeModel::where('is_del',0)->where('is_show',1)->field('id,name')->order('sort desc')->select();
  65. $cate=count($cate) >0 ? $cate->toArray() : [];
  66. $array=[];
  67. $oneCate['id']=0;
  68. $oneCate['name']='顶级分类';
  69. array_push($array,$oneCate);
  70. foreach ($cate as $key=>$value){
  71. array_push($array,$value);
  72. }
  73. return Json::successful($array);
  74. }
  75. /**
  76. * 快速编辑
  77. *
  78. * @return json
  79. */
  80. public function set_value($field = '', $id = '', $value = '')
  81. {
  82. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  83. if (GradeModel::where(['id' => $id])->update([$field => $value]))
  84. return Json::successful('保存成功');
  85. else
  86. return Json::fail('保存失败');
  87. }
  88. /**是否显示快捷操作
  89. * @param string $is_show
  90. * @param string $id
  91. * @return mixed
  92. */
  93. public function set_show($is_show = '', $id = '')
  94. {
  95. ($is_show == '' || $id == '') && Json::fail('缺少参数');
  96. $res = GradeModel::where(['id' => $id])->update(['is_show' => (int)$is_show]);
  97. if ($res) {
  98. return Json::successful($is_show == 1 ? '显示成功' : '隐藏成功');
  99. } else {
  100. return Json::fail($is_show == 1 ? '显示失败' : '隐藏失败');
  101. }
  102. }
  103. /**
  104. * 新增或者修改
  105. *
  106. * @return json
  107. */
  108. public function save($id = 0,$sid=0)
  109. {
  110. $post = parent::postMore([
  111. ['name', ''],
  112. ['pic', ''],
  113. ['grade_id', 0],
  114. ['sort', 0],
  115. ['is_show', 0],
  116. ]);
  117. if (!$post['name']) return Json::fail('请输入分类名称');
  118. if($post['grade_id']){
  119. if (!$post['pic']) return Json::fail('请选择分类图标');
  120. }
  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. $data['is_del']=1;
  163. if (GradeModel::update($data,['id'=>$id]))
  164. return Json::successful('删除成功');
  165. else
  166. return Json::fail('删除失败');
  167. }
  168. }