ConfigClassify.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\system\config;
  12. use app\common\repositories\system\config\ConfigClassifyRepository;
  13. use app\common\repositories\system\config\ConfigRepository;
  14. use app\validate\admin\ConfigClassifyValidate;
  15. use crmeb\basic\BaseController;
  16. use FormBuilder\Exception\FormBuilderException;
  17. use think\App;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. /**
  22. * 配置分类
  23. */
  24. class ConfigClassify extends BaseController
  25. {
  26. /**
  27. * @var ConfigClassifyRepository
  28. */
  29. private $repository;
  30. /**
  31. * ConfigClassify constructor.
  32. * @param App $app
  33. * @param ConfigClassifyRepository $repository
  34. */
  35. public function __construct(App $app, ConfigClassifyRepository $repository)
  36. {
  37. parent::__construct($app);
  38. $this->repository = $repository;
  39. }
  40. /**
  41. * 列表
  42. * @return mixed
  43. * @throws DataNotFoundException
  44. * @throws DbException
  45. * @throws ModelNotFoundException
  46. * @author xaboy
  47. * @day 2020-03-31
  48. */
  49. public function lst()
  50. {
  51. $where = $this->request->params(['status', 'classify_name']);
  52. $lst = $this->repository->lst($where);
  53. $lst['list'] = formatCategory($lst['list']->toArray(), 'config_classify_id');
  54. return app('json')->success($lst);
  55. }
  56. /**
  57. * 获取子集
  58. * @param int $pid
  59. * @return mixed
  60. * @throws DataNotFoundException
  61. * @throws DbException
  62. * @throws ModelNotFoundException
  63. * @author xaboy
  64. * @day 2020-03-27
  65. */
  66. public function children($pid)
  67. {
  68. return app('json')->success($this->repository->children($pid));
  69. }
  70. /**
  71. * 创建配置分类
  72. * @return mixed
  73. * @throws FormBuilderException
  74. * @author xaboy
  75. * @day 2020-03-31
  76. */
  77. public function createTable()
  78. {
  79. $form = $this->repository->createForm();
  80. return app('json')->success(formToData($form));
  81. }
  82. /**
  83. * 修改配置分类
  84. * @param int $id
  85. * @return mixed
  86. * @throws DataNotFoundException
  87. * @throws DbException
  88. * @throws ModelNotFoundException
  89. * @throws FormBuilderException
  90. * @author xaboy
  91. * @day 2020-03-31
  92. */
  93. public function updateTable($id)
  94. {
  95. if (!$this->repository->exists($id)) app('json')->fail('数据不存在');
  96. $form = $this->repository->updateForm($id);
  97. return app('json')->success(formToData($form));
  98. }
  99. /**
  100. * 配置分类下拉
  101. * @return mixed
  102. * @author xaboy
  103. * @day 2020-03-27
  104. */
  105. public function options()
  106. {
  107. return app('json')->success($this->repository->options());
  108. }
  109. /**
  110. * 配置分类详情
  111. * @param int $id
  112. * @return mixed
  113. * @throws DataNotFoundException
  114. * @throws DbException
  115. * @throws ModelNotFoundException
  116. * @author xaboy
  117. * @day 2020-03-27
  118. */
  119. public function get($id)
  120. {
  121. $data = $this->repository->get($id);
  122. if (!$data)
  123. return app('json')->fail('分类不存在');
  124. else
  125. return app('json')->success($data);
  126. }
  127. /**
  128. * 创建配置分类
  129. * @param ConfigClassifyValidate $validate
  130. * @return mixed
  131. * @author xaboy
  132. * @day 2020-03-27
  133. */
  134. public function create(ConfigClassifyValidate $validate)
  135. {
  136. $data = $this->request->params(['pid', 'classify_name', 'classify_key', 'info', 'status', 'icon', 'sort']);
  137. $validate->check($data);
  138. if ($this->repository->keyExists($data['classify_key']))
  139. return app('json')->fail('配置分类key已存在');
  140. if ($data['pid'] && !$this->repository->pidExists($data['pid']))
  141. return app('json')->fail('上级分类不存在');
  142. $this->repository->create($data);
  143. return app('json')->success('添加成功');
  144. }
  145. /**
  146. * 修改配置分类
  147. * @param int $id
  148. * @param ConfigClassifyValidate $validate
  149. * @return mixed
  150. * @throws DbException
  151. * @author xaboy
  152. * @day 2020-03-27
  153. */
  154. public function update($id, ConfigClassifyValidate $validate)
  155. {
  156. $data = $this->request->params(['pid', 'classify_name', 'classify_key', 'info', 'status', 'icon', 'sort']);
  157. $validate->check($data);
  158. if (!$this->repository->exists($id))
  159. return app('json')->fail('分类不存在');
  160. if ($this->repository->keyExists($data['classify_key'], $id))
  161. return app('json')->fail('配置分类key已存在');
  162. if ($data['pid'] && !$this->repository->pidExists($data['pid'], $id))
  163. return app('json')->fail('上级分类不存在');
  164. $this->repository->update($id, $data);
  165. return app('json')->success('修改成功');
  166. }
  167. /**
  168. * 修改状态
  169. * @param int $id
  170. * @return mixed
  171. * @throws DbException
  172. * @author xaboy
  173. * @day 2020-03-31
  174. */
  175. public function switchStatus($id)
  176. {
  177. $status = $this->request->param('status', 0);
  178. if (!$this->repository->exists($id))
  179. return app('json')->fail('分类不存在');
  180. $this->repository->switchStatus($id, $status == 1 ? 1 : 0);
  181. return app('json')->success('修改成功');
  182. }
  183. /**
  184. * 删除配置分类
  185. * @param int $id
  186. * @param ConfigRepository $configRepository
  187. * @return mixed
  188. * @throws DbException
  189. * @author xaboy
  190. * @day 2020-03-27
  191. */
  192. public function delete($id, ConfigRepository $configRepository)
  193. {
  194. if ($this->repository->existsChild($id))
  195. return app('json')->fail('存在子级,无法删除');
  196. if ($configRepository->classifyIdExists($id))
  197. return app('json')->fail('分类下存在配置,无法删除');
  198. $this->repository->delete($id);
  199. return app('json')->success('删除成功');
  200. }
  201. /**
  202. * 获取配置分类
  203. * @return \think\response\Json
  204. * @author Qinii
  205. */
  206. public function getOptions()
  207. {
  208. $options = $this->repository->getOption();
  209. return app('json')->success(formatCategory($options, 'config_classify_id'));
  210. }
  211. }