ConfigClassify.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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\controller\admin\system\config;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\system\config\ConfigClassifyRepository;
  14. use app\common\repositories\system\config\ConfigRepository;
  15. use app\validate\admin\ConfigClassifyValidate;
  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. $lst = $this->repository->lst();
  54. $lst['list'] = formatCategory($lst['list']->toArray(), 'config_classify_id');
  55. return app('json')->success($lst);
  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. * @return mixed
  72. * @throws FormBuilderException
  73. * @author xaboy
  74. * @day 2020-03-31
  75. */
  76. public function createTable()
  77. {
  78. $form = $this->repository->createForm();
  79. return app('json')->success(formToData($form));
  80. }
  81. /**
  82. * @param int $id
  83. * @return mixed
  84. * @throws DataNotFoundException
  85. * @throws DbException
  86. * @throws ModelNotFoundException
  87. * @throws FormBuilderException
  88. * @author xaboy
  89. * @day 2020-03-31
  90. */
  91. public function updateTable($id)
  92. {
  93. if (!$this->repository->exists($id)) app('json')->fail('数据不存在');
  94. $form = $this->repository->updateForm($id);
  95. return app('json')->success(formToData($form));
  96. }
  97. /**
  98. * @return mixed
  99. * @author xaboy
  100. * @day 2020-03-27
  101. */
  102. public function options()
  103. {
  104. return app('json')->success($this->repository->options());
  105. }
  106. /**
  107. * @param int $id
  108. * @return mixed
  109. * @throws DataNotFoundException
  110. * @throws DbException
  111. * @throws ModelNotFoundException
  112. * @author xaboy
  113. * @day 2020-03-27
  114. */
  115. public function get($id)
  116. {
  117. $data = $this->repository->get($id);
  118. if (!$data)
  119. return app('json')->fail('分类不存在');
  120. else
  121. return app('json')->success($data);
  122. }
  123. /**
  124. * @param ConfigClassifyValidate $validate
  125. * @return mixed
  126. * @author xaboy
  127. * @day 2020-03-27
  128. */
  129. public function create(ConfigClassifyValidate $validate)
  130. {
  131. $data = $this->request->params(['pid', 'classify_name', 'classify_key', 'info', 'status', 'icon', 'sort']);
  132. $validate->check($data);
  133. if ($this->repository->keyExists($data['classify_key']))
  134. return app('json')->fail('配置分类key已存在');
  135. if ($data['pid'] && !$this->repository->pidExists($data['pid']))
  136. return app('json')->fail('上级分类不存在');
  137. $this->repository->create($data);
  138. return app('json')->success('添加成功');
  139. }
  140. /**
  141. * @param int $id
  142. * @param ConfigClassifyValidate $validate
  143. * @return mixed
  144. * @throws DbException
  145. * @author xaboy
  146. * @day 2020-03-27
  147. */
  148. public function update($id, ConfigClassifyValidate $validate)
  149. {
  150. $data = $this->request->params(['pid', 'classify_name', 'classify_key', 'info', 'status', 'icon', 'sort']);
  151. $validate->check($data);
  152. if (!$this->repository->exists($id))
  153. return app('json')->fail('分类不存在');
  154. if ($this->repository->keyExists($data['classify_key'], $id))
  155. return app('json')->fail('配置分类key已存在');
  156. if ($data['pid'] && !$this->repository->pidExists($data['pid'], $id))
  157. return app('json')->fail('上级分类不存在');
  158. $this->repository->update($id, $data);
  159. return app('json')->success('修改成功');
  160. }
  161. /**
  162. * @param int $id
  163. * @return mixed
  164. * @throws DbException
  165. * @author xaboy
  166. * @day 2020-03-31
  167. */
  168. public function switchStatus($id)
  169. {
  170. $status = $this->request->param('status', 0);
  171. if (!$this->repository->exists($id))
  172. return app('json')->fail('分类不存在');
  173. $this->repository->switchStatus($id, $status == 1 ? 1 : 0);
  174. return app('json')->success('修改成功');
  175. }
  176. /**
  177. * @param int $id
  178. * @param ConfigRepository $configRepository
  179. * @return mixed
  180. * @throws DbException
  181. * @author xaboy
  182. * @day 2020-03-27
  183. */
  184. public function delete($id, ConfigRepository $configRepository)
  185. {
  186. if ($this->repository->existsChild($id))
  187. return app('json')->fail('存在子级,无法删除');
  188. if ($configRepository->classifyIdExists($id))
  189. return app('json')->fail('分类下存在配置,无法删除');
  190. $this->repository->delete($id);
  191. return app('json')->success('删除成功');
  192. }
  193. }