DataDownloadCategpry.php 5.3 KB

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