ArticleCategory.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\v1\cms;
  12. use app\controller\admin\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\controller\admin\v1\cms
  20. */
  21. class ArticleCategory extends AuthController
  22. {
  23. protected $service;
  24. public function __construct(App $app, ArticleCategoryServices $service)
  25. {
  26. parent::__construct($app);
  27. $this->service = $service;
  28. }
  29. /**
  30. * 获取分类列表
  31. * @return mixed
  32. */
  33. public function index()
  34. {
  35. $where = $this->request->getMore([
  36. ['status', ''],
  37. ['title', ''],
  38. ['type', 0]
  39. ]);
  40. if ($where['status'] == 'all') $where['status'] = '';
  41. $type = $where['type'];
  42. unset($where['type']);
  43. if ($type == 1) {//后台选择分类使用
  44. //过滤下架的
  45. $where['status'] = 1;
  46. $data = $this->service->getList($where);
  47. $data = $data['list'];
  48. } else {
  49. $data = $this->service->getList($where);
  50. }
  51. return $this->success($data);
  52. }
  53. /**
  54. * 创建新增表单
  55. * @return mixed
  56. * @throws \FormBuilder\Exception\FormBuilderException
  57. */
  58. public function create()
  59. {
  60. return $this->success($this->service->createForm(0));
  61. }
  62. /**
  63. * 保存新建分类
  64. * @return mixed
  65. */
  66. public function save()
  67. {
  68. $data = $this->request->postMore([
  69. ['title', ''],
  70. ['intr', ''],
  71. ['image', ''],
  72. ['sort', 0],
  73. ['status', 0]
  74. ]);
  75. if (!$data['title']) {
  76. return $this->fail('请填写分类名称');
  77. }
  78. $data['add_time'] = time();
  79. $this->service->save($data);
  80. CacheService::delete('ARTICLE_CATEGORY');
  81. return $this->success('添加分类成功!');
  82. }
  83. /**
  84. * 创建修改表单
  85. * @param $id
  86. * @return mixed
  87. * @throws \FormBuilder\Exception\FormBuilderException
  88. */
  89. public function edit($id)
  90. {
  91. return $this->success($this->service->createForm($id));
  92. }
  93. /**
  94. * 保存修改分类
  95. * @param $id
  96. * @return mixed
  97. */
  98. public function update($id)
  99. {
  100. $data = $this->request->postMore([
  101. ['id', 0],
  102. ['title', ''],
  103. ['intr', ''],
  104. ['image', ''],
  105. ['sort', 0],
  106. ['status', 0]
  107. ]);
  108. $this->service->update($data);
  109. CacheService::delete('ARTICLE_CATEGORY');
  110. return $this->success('修改成功!');
  111. }
  112. /**
  113. * 删除文章分类
  114. * @param $id
  115. * @return mixed
  116. */
  117. public function delete($id)
  118. {
  119. $this->service->del($id);
  120. CacheService::delete('ARTICLE_CATEGORY');
  121. return $this->success('删除成功!');
  122. }
  123. /**
  124. * 修改文章分类状态
  125. * @param int $id
  126. * @param int $status
  127. * @return mixed
  128. * @throws \Psr\SimpleCache\InvalidArgumentException
  129. */
  130. public function set_status($id, $status)
  131. {
  132. if ($status == '' || $id == 0) return $this->fail('参数错误');
  133. $this->service->setStatus($id, $status);
  134. CacheService::delete('ARTICLE_CATEGORY');
  135. return $this->success($status == 0 ? '隐藏成功' : '显示成功');
  136. }
  137. /**
  138. * 获取文章分类
  139. * @return mixed
  140. */
  141. public function categoryList()
  142. {
  143. return $this->success($this->service->getArticleCategory());
  144. }
  145. }