ArticleCategory.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. CacheService::delete('ARTICLE_CATEGORY_PC');
  85. return app('json')->success(100021);
  86. }
  87. /**
  88. * 创建修改表单
  89. * @param int $id
  90. * @return mixed
  91. * @throws \FormBuilder\Exception\FormBuilderException
  92. */
  93. public function edit($id = 0)
  94. {
  95. if (!$id) return app('json')->fail(100100);
  96. return app('json')->success($this->service->createForm($id));
  97. }
  98. /**
  99. * 保存修改分类
  100. * @param $id
  101. * @return mixed
  102. * @throws \Psr\SimpleCache\InvalidArgumentException
  103. */
  104. public function update($id)
  105. {
  106. $data = $this->request->postMore([
  107. ['id', 0],
  108. ['title', ''],
  109. ['pid', 0],
  110. ['intr', ''],
  111. ['image', ''],
  112. ['sort', 0],
  113. ['status', 0]
  114. ]);
  115. $this->service->update($data);
  116. CacheService::delete('ARTICLE_CATEGORY');
  117. CacheService::delete('ARTICLE_CATEGORY_PC');
  118. return app('json')->success(100001);
  119. }
  120. /**
  121. * 删除文章分类
  122. * @param $id
  123. * @return mixed
  124. * @throws \Psr\SimpleCache\InvalidArgumentException
  125. */
  126. public function delete($id)
  127. {
  128. if (!$id) return app('json')->fail(100100);
  129. $this->service->del($id);
  130. CacheService::delete('ARTICLE_CATEGORY');
  131. CacheService::delete('ARTICLE_CATEGORY_PC');
  132. return app('json')->success(100002);
  133. }
  134. /**
  135. * 修改文章分类状态
  136. * @param int $id
  137. * @param int $status
  138. * @return mixed
  139. * @throws \Psr\SimpleCache\InvalidArgumentException
  140. */
  141. public function set_status($id, $status)
  142. {
  143. if ($status == '' || $id == 0) return app('json')->fail(100100);
  144. $this->service->setStatus($id, $status);
  145. CacheService::delete('ARTICLE_CATEGORY');
  146. CacheService::delete('ARTICLE_CATEGORY_PC');
  147. return app('json')->success(100014);
  148. }
  149. /**
  150. * 获取文章分类
  151. * @return mixed
  152. */
  153. public function categoryList()
  154. {
  155. return app('json')->success($this->service->getArticleTwoCategory());
  156. }
  157. /**
  158. * 树形列表
  159. * @return mixed
  160. * @throws \ReflectionException
  161. */
  162. public function getTreeList()
  163. {
  164. $list = $this->service->getTreeList();
  165. return app('json')->success($list);
  166. }
  167. }