SpecialTaskCategory.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 think\Url;
  13. use service\FormBuilder as Form;
  14. use service\JsonService as Json;
  15. use app\admin\controller\AuthController;
  16. use app\admin\model\special\SpecialTaskCategory as SpecialTaskCategoryModel;
  17. /**
  18. * 素材分类控制器
  19. * Class Grade
  20. * @package app\admin\controller\special
  21. */
  22. class SpecialTaskCategory extends AuthController
  23. {
  24. public function index($pid=0)
  25. {
  26. $this->assign(['category'=>SpecialTaskCategoryModel::taskCategoryAll(1),'pid'=>$pid]);
  27. return $this->fetch();
  28. }
  29. public function get_category_list()
  30. {
  31. $where = parent::getMore([
  32. ['page', 1],
  33. ['limit', 20],
  34. ['pid', 0],
  35. ['cate_name', ''],
  36. ]);
  37. return Json::successlayui(SpecialTaskCategoryModel::getAllList($where));
  38. }
  39. /**
  40. * 创建分类
  41. * @param int $id
  42. * @return mixed
  43. * @throws \think\exception\DbException
  44. */
  45. public function create($id = 0,$pid=0)
  46. {
  47. $cate=[];
  48. if($id){
  49. $cate = SpecialTaskCategoryModel::get($id);
  50. }
  51. $this->assign(['cate'=>json_encode($cate),'id'=>$id,'pid'=>$pid]);
  52. return $this->fetch();
  53. }
  54. public function get_cate_list()
  55. {
  56. $category=SpecialTaskCategoryModel::taskCategoryAll(2);
  57. return Json::successful($category);
  58. }
  59. public function add_cate_list()
  60. {
  61. $category=SpecialTaskCategoryModel::where(['pid'=>0,'is_del'=>0])->select();
  62. $category=count($category) >0 ? $category->toArray() : [];
  63. $array=[];
  64. $oneCate['id']=0;
  65. $oneCate['title']='顶级分类';
  66. array_push($array,$oneCate);
  67. foreach ($category as $key=>$value){
  68. array_push($array,$value);
  69. }
  70. return Json::successful($array);
  71. }
  72. /**
  73. * 快速编辑
  74. *
  75. * @return json
  76. */
  77. public function set_value($field = '', $id = '', $value = '')
  78. {
  79. $field == '' || $id == '' || $value == '' && Json::fail('缺少参数');
  80. if (SpecialTaskCategoryModel::where(['id' => $id])->update([$field => $value]))
  81. return Json::successful('保存成功');
  82. else
  83. return Json::fail('保存失败');
  84. }
  85. /**
  86. * 新增或者修改
  87. *
  88. * @return json
  89. */
  90. public function save($id = 0)
  91. {
  92. $post = parent::postMore([
  93. ['title', ''],
  94. ['pid', ''],
  95. ['sort', 0],
  96. ]);
  97. if (!$post['title']) return Json::fail('请输入分类名称');
  98. if ($id) {
  99. $res=SpecialTaskCategoryModel::edit($post,$id);
  100. if ($res)
  101. return Json::successful('修改成功');
  102. else
  103. return Json::fail('修改失败');
  104. } else {
  105. $post['add_time'] = time();
  106. if(SpecialTaskCategoryModel::be(['title'=>$post['title'],'is_del'=>0])){
  107. return Json::fail('分类名称已存在!');
  108. }
  109. $res=SpecialTaskCategoryModel::set($post);
  110. if ($res)
  111. return Json::successful('添加成功');
  112. else
  113. return Json::fail('添加失败');
  114. }
  115. }
  116. /**
  117. * 删除
  118. *
  119. * @return json
  120. */
  121. public function delete($id = 0)
  122. {
  123. if (!$id) return Json::fail('缺少参数');
  124. $count=SpecialTaskCategoryModel::where('pid',$id)->where('is_del',0)->count();
  125. if ($count) return Json::fail('请先删除下级分类');
  126. $data['is_del']=1;
  127. if (SpecialTaskCategoryModel::update($data,['id'=>$id]))
  128. return Json::successful('删除成功');
  129. else
  130. return Json::fail('删除失败');
  131. }
  132. }