ArticleCategory.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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. * 文章内容分类
  22. */
  23. class ArticleCategory extends BaseController
  24. {
  25. /**
  26. * @var ArticleCategoryRepository
  27. */
  28. protected $repository;
  29. /**
  30. * ArticleCategory constructor.
  31. * @param App $app
  32. * @param ArticleCategoryRepository $repository
  33. */
  34. public function __construct(App $app, ArticleCategoryRepository $repository)
  35. {
  36. parent::__construct($app);
  37. $this->repository = $repository;
  38. }
  39. /**
  40. * 文章分类列表
  41. * @return mixed
  42. * @throws DataNotFoundException
  43. * @throws DbException
  44. * @throws ModelNotFoundException
  45. * @author xaboy
  46. * @day 2020-04-20
  47. */
  48. public function lst()
  49. {
  50. $result = $this->repository->getFormatList();
  51. return app('json')->success($result);
  52. }
  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(0)));
  63. }
  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. * 新增分类
  83. * @param ArticleCategoryValidate $validate
  84. * @return mixed
  85. * @author xaboy
  86. * @day 2020-04-20
  87. */
  88. public function create(ArticleCategoryValidate $validate)
  89. {
  90. $data = $this->checkParams($validate);
  91. if ($data['pid'] && !$this->repository->merExists(0, $data['pid']))
  92. return app('json')->fail('上级分类不存在');
  93. $data['mer_id'] = 0;
  94. $this->repository->create($data);
  95. return app('json')->success('添加成功');
  96. }
  97. /**
  98. * 更新分类
  99. * @param $id
  100. * @param ArticleCategoryValidate $validate
  101. * @return mixed
  102. * @throws DbException
  103. * @author xaboy
  104. * @day 2020-04-20
  105. */
  106. public function update($id, ArticleCategoryValidate $validate)
  107. {
  108. $data = $this->checkParams($validate);
  109. if (!$this->repository->merExists(0, $id))
  110. return app('json')->fail('数据不存在');
  111. if ($data['pid'] && !$this->repository->merExists(0, $data['pid']))
  112. return app('json')->fail('上级分类不存在');
  113. $this->repository->update($id, $data);
  114. return app('json')->success('编辑成功');
  115. }
  116. /**
  117. * 切换分类状态
  118. * @param int $id
  119. * @return mixed
  120. * @throws DbException
  121. * @author xaboy
  122. * @day 2020-04-20
  123. */
  124. public function switchStatus($id)
  125. {
  126. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  127. if (!$this->repository->exists($id))
  128. return app('json')->fail('分类不存在');
  129. $this->repository->update($id, compact('status'));
  130. return app('json')->success('修改成功');
  131. }
  132. /**
  133. * 删除分类
  134. * @param $id
  135. * @return mixed
  136. * @throws DbException
  137. * @author xaboy
  138. * @day 2020-04-20
  139. */
  140. public function delete($id)
  141. {
  142. if ($this->repository->merFieldExists(0, 'pid', $id))
  143. return app('json')->fail('存在子级,无法删除');
  144. $this->repository->delete($id, 0);
  145. return app('json')->success('删除成功');
  146. }
  147. /**
  148. * 分类详情
  149. * @param $id
  150. * @return mixed
  151. * @throws DataNotFoundException
  152. * @throws DbException
  153. * @throws ModelNotFoundException
  154. * @author Qinii
  155. */
  156. public function detail($id)
  157. {
  158. if (!$this->repository->exists($id))
  159. return app('json')->fail('分类不存在');
  160. return app('json')->success($this->repository->get($id, 0));
  161. }
  162. /**
  163. * 验证数据
  164. * @param ArticleCategoryValidate $validate
  165. * @return array
  166. * @author xaboy
  167. * @day 2020-04-20
  168. */
  169. public function checkParams(ArticleCategoryValidate $validate)
  170. {
  171. $data = $this->request->params([['pid', 0], 'title', 'info', 'status', 'image', 'sort']);
  172. $validate->check($data);
  173. return $data;
  174. }
  175. /**
  176. * 获取显示的分类
  177. * @return \think\response\Json
  178. * @throws DataNotFoundException
  179. * @throws DbException
  180. * @throws ModelNotFoundException
  181. * @author wuhaotian
  182. * @email 442384644@qq.com
  183. * @date 2024/7/4
  184. */
  185. public function select()
  186. {
  187. $result = $this->repository->getFormatList(0, 1);
  188. return app('json')->success($result);
  189. }
  190. }