ArticleCategory.php 5.1 KB

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