ConfigClassify.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\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. * Class ConfigClassify
  23. * @package app\controller\admin\system\config
  24. * @author xaboy
  25. * @day 2020-03-27
  26. */
  27. class ConfigClassify extends BaseController
  28. {
  29. /**
  30. * @var ConfigClassifyRepository
  31. */
  32. private $repository;
  33. /**
  34. * ConfigClassify constructor.
  35. * @param App $app
  36. * @param ConfigClassifyRepository $repository
  37. */
  38. public function __construct(App $app, ConfigClassifyRepository $repository)
  39. {
  40. parent::__construct($app);
  41. $this->repository = $repository;
  42. }
  43. /**
  44. * @return mixed
  45. * @throws DataNotFoundException
  46. * @throws DbException
  47. * @throws ModelNotFoundException
  48. * @author xaboy
  49. * @day 2020-03-31
  50. */
  51. public function lst()
  52. {
  53. $where = $this->request->params(['status', 'classify_name']);
  54. $lst = $this->repository->lst($where);
  55. $lst['list'] = formatCategory($lst['list']->toArray(), 'config_classify_id');
  56. return app('json')->success($lst);
  57. }
  58. /**
  59. * @param int $pid
  60. * @return mixed
  61. * @throws DataNotFoundException
  62. * @throws DbException
  63. * @throws ModelNotFoundException
  64. * @author xaboy
  65. * @day 2020-03-27
  66. */
  67. public function children($pid)
  68. {
  69. return app('json')->success($this->repository->children($pid));
  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. * @param int $id
  84. * @return mixed
  85. * @throws DataNotFoundException
  86. * @throws DbException
  87. * @throws ModelNotFoundException
  88. * @throws FormBuilderException
  89. * @author xaboy
  90. * @day 2020-03-31
  91. */
  92. public function updateTable($id)
  93. {
  94. if (!$this->repository->exists($id)) app('json')->fail('数据不存在');
  95. $form = $this->repository->updateForm($id);
  96. return app('json')->success(formToData($form));
  97. }
  98. /**
  99. * @return mixed
  100. * @author xaboy
  101. * @day 2020-03-27
  102. */
  103. public function options()
  104. {
  105. return app('json')->success($this->repository->options());
  106. }
  107. /**
  108. * @param int $id
  109. * @return mixed
  110. * @throws DataNotFoundException
  111. * @throws DbException
  112. * @throws ModelNotFoundException
  113. * @author xaboy
  114. * @day 2020-03-27
  115. */
  116. public function get($id)
  117. {
  118. $data = $this->repository->get($id);
  119. if (!$data)
  120. return app('json')->fail('分类不存在');
  121. else
  122. return app('json')->success($data);
  123. }
  124. /**
  125. * @param ConfigClassifyValidate $validate
  126. * @return mixed
  127. * @author xaboy
  128. * @day 2020-03-27
  129. */
  130. public function create(ConfigClassifyValidate $validate)
  131. {
  132. $data = $this->request->params(['pid', 'classify_name', 'classify_key', 'info', 'status', 'icon', 'sort']);
  133. $validate->check($data);
  134. if ($this->repository->keyExists($data['classify_key']))
  135. return app('json')->fail('配置分类key已存在');
  136. if ($data['pid'] && !$this->repository->pidExists($data['pid']))
  137. return app('json')->fail('上级分类不存在');
  138. $this->repository->create($data);
  139. return app('json')->success('添加成功');
  140. }
  141. /**
  142. * @param int $id
  143. * @param ConfigClassifyValidate $validate
  144. * @return mixed
  145. * @throws DbException
  146. * @author xaboy
  147. * @day 2020-03-27
  148. */
  149. public function update($id, ConfigClassifyValidate $validate)
  150. {
  151. $data = $this->request->params(['pid', 'classify_name', 'classify_key', 'info', 'status', 'icon', 'sort']);
  152. $validate->check($data);
  153. if (!$this->repository->exists($id))
  154. return app('json')->fail('分类不存在');
  155. if ($this->repository->keyExists($data['classify_key'], $id))
  156. return app('json')->fail('配置分类key已存在');
  157. if ($data['pid'] && !$this->repository->pidExists($data['pid'], $id))
  158. return app('json')->fail('上级分类不存在');
  159. $this->repository->update($id, $data);
  160. return app('json')->success('修改成功');
  161. }
  162. /**
  163. * @param int $id
  164. * @return mixed
  165. * @throws DbException
  166. * @author xaboy
  167. * @day 2020-03-31
  168. */
  169. public function switchStatus($id)
  170. {
  171. $status = $this->request->param('status', 0);
  172. if (!$this->repository->exists($id))
  173. return app('json')->fail('分类不存在');
  174. $this->repository->switchStatus($id, $status == 1 ? 1 : 0);
  175. return app('json')->success('修改成功');
  176. }
  177. /**
  178. * @param int $id
  179. * @param ConfigRepository $configRepository
  180. * @return mixed
  181. * @throws DbException
  182. * @author xaboy
  183. * @day 2020-03-27
  184. */
  185. public function delete($id, ConfigRepository $configRepository)
  186. {
  187. if ($this->repository->existsChild($id))
  188. return app('json')->fail('存在子级,无法删除');
  189. if ($configRepository->classifyIdExists($id))
  190. return app('json')->fail('分类下存在配置,无法删除');
  191. $this->repository->delete($id);
  192. return app('json')->success('删除成功');
  193. }
  194. public function getOptions()
  195. {
  196. $options = $this->repository->getOption();
  197. return app('json')->success(formatCategory($options, 'config_classify_id'));
  198. }
  199. }