SpecialTaskCategory.php 5.0 KB

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