ArticleCategory.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\adminapi\controller\v1\cms;
  12. use app\adminapi\controller\AuthController;
  13. use app\services\article\ArticleCategoryServices;
  14. use crmeb\services\CacheService;
  15. use think\facade\App;
  16. /**
  17. * 文章分类管理
  18. * Class ArticleCategory
  19. * @package app\adminapi\controller\v1\cms
  20. */
  21. class ArticleCategory extends AuthController
  22. {
  23. /**
  24. * @var ArticleCategoryServices
  25. */
  26. protected $service;
  27. /**
  28. * ArticleCategory constructor.
  29. * @param App $app
  30. * @param ArticleCategoryServices $service
  31. */
  32. public function __construct(App $app, ArticleCategoryServices $service)
  33. {
  34. parent::__construct($app);
  35. $this->service = $service;
  36. }
  37. /**
  38. * 获取分类列表
  39. * @return mixed
  40. */
  41. public function index()
  42. {
  43. $where = $this->request->getMore([
  44. ['status', ''],
  45. ['title', ''],
  46. ['type', 0]
  47. ]);
  48. $type = $where['type'];
  49. unset($where['type']);
  50. $data = $this->service->getList($where);
  51. if ($type == 1) $data = $data['list'];
  52. return app('json')->success($data);
  53. }
  54. /**
  55. * 创建新增表单
  56. * @return mixed
  57. * @throws \FormBuilder\Exception\FormBuilderException
  58. */
  59. public function create()
  60. {
  61. return app('json')->success($this->service->createForm(0));
  62. }
  63. /**
  64. * 保存新建分类
  65. * @return mixed
  66. * @throws \Psr\SimpleCache\InvalidArgumentException
  67. */
  68. public function save()
  69. {
  70. $data = $this->request->postMore([
  71. ['title', ''],
  72. ['pid', 0],
  73. ['intr', ''],
  74. ['image', ''],
  75. ['sort', 0],
  76. ['status', 0]
  77. ]);
  78. if (!$data['title']) {
  79. return app('json')->fail(400100);
  80. }
  81. $data['add_time'] = time();
  82. $this->service->save($data);
  83. CacheService::delete('ARTICLE_CATEGORY');
  84. return app('json')->success(100021);
  85. }
  86. /**
  87. * 创建修改表单
  88. * @param int $id
  89. * @return mixed
  90. * @throws \FormBuilder\Exception\FormBuilderException
  91. */
  92. public function edit($id = 0)
  93. {
  94. if (!$id) return app('json')->fail(100100);
  95. return app('json')->success($this->service->createForm($id));
  96. }
  97. /**
  98. * 保存修改分类
  99. * @param $id
  100. * @return mixed
  101. * @throws \Psr\SimpleCache\InvalidArgumentException
  102. */
  103. public function update($id)
  104. {
  105. $data = $this->request->postMore([
  106. ['id', 0],
  107. ['title', ''],
  108. ['pid', 0],
  109. ['intr', ''],
  110. ['image', ''],
  111. ['sort', 0],
  112. ['status', 0]
  113. ]);
  114. $this->service->update($data);
  115. CacheService::delete('ARTICLE_CATEGORY');
  116. return app('json')->success(100001);
  117. }
  118. /**
  119. * 删除文章分类
  120. * @param $id
  121. * @return mixed
  122. * @throws \Psr\SimpleCache\InvalidArgumentException
  123. */
  124. public function delete($id)
  125. {
  126. if (!$id) return app('json')->fail(100100);
  127. $this->service->del($id);
  128. CacheService::delete('ARTICLE_CATEGORY');
  129. return app('json')->success(100002);
  130. }
  131. /**
  132. * 修改文章分类状态
  133. * @param int $id
  134. * @param int $status
  135. * @return mixed
  136. * @throws \Psr\SimpleCache\InvalidArgumentException
  137. */
  138. public function set_status($id, $status)
  139. {
  140. if ($status == '' || $id == 0) return app('json')->fail(100100);
  141. $this->service->setStatus($id, $status);
  142. CacheService::delete('ARTICLE_CATEGORY');
  143. return app('json')->success(100014);
  144. }
  145. /**
  146. * 获取文章分类
  147. * @return mixed
  148. */
  149. public function categoryList()
  150. {
  151. return app('json')->success($this->service->getArticleTwoCategory());
  152. }
  153. /**
  154. * 树形列表
  155. * @return mixed
  156. * @throws \ReflectionException
  157. */
  158. public function getTreeList()
  159. {
  160. $list = $this->service->getTreeList();
  161. return app('json')->success($list);
  162. }
  163. }