ArticleCategory.php 4.6 KB

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