ArticleCategory.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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\article;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\article\ArticleCategoryRepository;
  14. use app\validate\admin\ArticleCategoryValidate;
  15. use FormBuilder\Exception\FormBuilderException;
  16. use think\App;
  17. use think\db\exception\DataNotFoundException;
  18. use think\db\exception\DbException;
  19. use think\db\exception\ModelNotFoundException;
  20. /**
  21. * Class ArticleCategory
  22. * @package app\controller\admin\article
  23. * @author xaboy
  24. * @day 2020-04-20
  25. */
  26. class ArticleCategory extends BaseController
  27. {
  28. /**
  29. * @var ArticleCategoryRepository
  30. */
  31. protected $repository;
  32. /**
  33. * ArticleCategory constructor.
  34. * @param App $app
  35. * @param ArticleCategoryRepository $repository
  36. */
  37. public function __construct(App $app, ArticleCategoryRepository $repository)
  38. {
  39. parent::__construct($app);
  40. $this->repository = $repository;
  41. }
  42. /**
  43. * @return mixed
  44. * @throws DataNotFoundException
  45. * @throws DbException
  46. * @throws ModelNotFoundException
  47. * @author xaboy
  48. * @day 2020-04-20
  49. */
  50. public function lst()
  51. {
  52. $result = $this->repository->getFormatList();
  53. return app('json')->success($result);
  54. }
  55. /**
  56. * @return mixed
  57. * @throws FormBuilderException
  58. * @author xaboy
  59. * @day 2020-04-15
  60. */
  61. public function createForm()
  62. {
  63. return app('json')->success(formToData($this->repository->form(0)));
  64. }
  65. /**
  66. * @param $id
  67. * @return mixed
  68. * @throws DataNotFoundException
  69. * @throws DbException
  70. * @throws FormBuilderException
  71. * @throws ModelNotFoundException
  72. * @author xaboy
  73. * @day 2020-04-15
  74. */
  75. public function updateForm($id)
  76. {
  77. if (!$this->repository->merExists(0, $id))
  78. return app('json')->fail('数据不存在');
  79. return app('json')->success(formToData($this->repository->updateForm(0, $id)));
  80. }
  81. /**
  82. * @param ArticleCategoryValidate $validate
  83. * @return mixed
  84. * @author xaboy
  85. * @day 2020-04-20
  86. */
  87. public function create(ArticleCategoryValidate $validate)
  88. {
  89. $data = $this->checkParams($validate);
  90. if ($data['pid'] && !$this->repository->merExists(0, $data['pid']))
  91. return app('json')->fail('上级分类不存在');
  92. $data['mer_id'] = 0;
  93. $this->repository->create($data);
  94. return app('json')->success('添加成功');
  95. }
  96. /**
  97. * @param $id
  98. * @param ArticleCategoryValidate $validate
  99. * @return mixed
  100. * @throws DbException
  101. * @author xaboy
  102. * @day 2020-04-20
  103. */
  104. public function update($id, ArticleCategoryValidate $validate)
  105. {
  106. $data = $this->checkParams($validate);
  107. if (!$this->repository->merExists(0, $id))
  108. return app('json')->fail('数据不存在');
  109. if ($data['pid'] && !$this->repository->merExists(0, $data['pid']))
  110. return app('json')->fail('上级分类不存在');
  111. $this->repository->update($id, $data);
  112. return app('json')->success('编辑成功');
  113. }
  114. /**
  115. * @param int $id
  116. * @return mixed
  117. * @throws DbException
  118. * @author xaboy
  119. * @day 2020-04-20
  120. */
  121. public function switchStatus($id)
  122. {
  123. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  124. if (!$this->repository->exists($id))
  125. return app('json')->fail('分类不存在');
  126. $this->repository->update($id, compact('status'));
  127. return app('json')->success('修改成功');
  128. }
  129. /**
  130. * @param $id
  131. * @return mixed
  132. * @throws DbException
  133. * @author xaboy
  134. * @day 2020-04-20
  135. */
  136. public function delete($id)
  137. {
  138. if ($this->repository->merFieldExists(0, 'pid', $id))
  139. return app('json')->fail('存在子级,无法删除');
  140. $this->repository->delete($id, 0);
  141. return app('json')->success('删除成功');
  142. }
  143. /**
  144. * 详情
  145. * @param $id
  146. * @return mixed
  147. * @throws DataNotFoundException
  148. * @throws DbException
  149. * @throws ModelNotFoundException
  150. * @author Qinii
  151. */
  152. public function detail($id)
  153. {
  154. if (!$this->repository->exists($id))
  155. return app('json')->fail('分类不存在');
  156. return app('json')->success($this->repository->get($id,0));
  157. }
  158. /**
  159. * @param ArticleCategoryValidate $validate
  160. * @return array
  161. * @author xaboy
  162. * @day 2020-04-20
  163. */
  164. public function checkParams(ArticleCategoryValidate $validate)
  165. {
  166. $data = $this->request->params([['pid', 0], 'title', 'info', 'status', 'image', 'sort']);
  167. $validate->check($data);
  168. return $data;
  169. }
  170. }