CommunityTopic.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\community;
  12. use app\common\repositories\community\CommunityCategoryRepository;
  13. use app\validate\admin\CommunityTopicValidate;
  14. use crmeb\basic\BaseController;
  15. use think\App;
  16. use app\common\repositories\community\CommunityTopicRepository as repository;
  17. /**
  18. * 社区话题
  19. */
  20. class CommunityTopic extends BaseController
  21. {
  22. /**
  23. * @var CommunityTopicRepository
  24. */
  25. protected $repository;
  26. /**
  27. * CommunityTopicRepository
  28. * @param App $app
  29. * @param $repository
  30. */
  31. public function __construct(App $app, repository $repository)
  32. {
  33. parent::__construct($app);
  34. $this->repository = $repository;
  35. }
  36. /**
  37. * 列表
  38. * @return mixed
  39. * @author Qinii
  40. */
  41. public function lst()
  42. {
  43. $where = $this->request->params(['topic_name', 'category_id', 'status', 'is_hot', 'is_del']);
  44. [$page, $limit] = $this->getPage();
  45. return app('json')->success($this->repository->getList($where, $page, $limit));
  46. }
  47. /**
  48. * 创建表单
  49. * @return \think\response\Json
  50. * @author Qinii
  51. * @day 10/26/21
  52. */
  53. public function createForm()
  54. {
  55. return app('json')->success(formToData($this->repository->form(null)));
  56. }
  57. /**
  58. * 创建
  59. * @return \think\response\Json
  60. * @author Qinii
  61. */
  62. public function create()
  63. {
  64. $data = $this->checkParams();
  65. $data['topic_name'] = trim($data['topic_name']);
  66. if ($this->repository->fieldExists('topic_name', $data['topic_name'], null))
  67. return app('json')->fail('话题重复');
  68. $this->repository->create($data);
  69. app()->make(CommunityCategoryRepository::class)->clearCahe();
  70. return app('json')->success('添加成功');
  71. }
  72. /**
  73. * 更新表单
  74. * @param $id
  75. * @return \think\response\Json
  76. * @author Qinii
  77. */
  78. public function updateForm($id)
  79. {
  80. if (!$this->repository->exists($id))
  81. return app('json')->fail('数据不存在');
  82. return app('json')->success(formToData($this->repository->form($id)));
  83. }
  84. /**
  85. * 编辑
  86. * @param $id
  87. * @return \think\response\Json
  88. * @author Qinii
  89. */
  90. public function update($id)
  91. {
  92. $data = $this->checkParams();
  93. if (!$this->repository->exists($id))
  94. return app('json')->fail('数据不存在');
  95. if ($this->repository->fieldExists('topic_name', $data['topic_name'], $id))
  96. return app('json')->fail('话题重复');
  97. $this->repository->update($id, $data);
  98. app()->make(CommunityCategoryRepository::class)->clearCahe();
  99. return app('json')->success('编辑成功');
  100. }
  101. /**
  102. * 删除
  103. * @param $id
  104. * @return mixed
  105. * @author Qinii
  106. */
  107. public function delete($id)
  108. {
  109. if (!$this->repository->exists($id))
  110. return app('json')->fail('数据不存在');
  111. $this->repository->update($id, ['is_del' => 1]);
  112. app()->make(CommunityCategoryRepository::class)->clearCahe();
  113. return app('json')->success('删除成功');
  114. }
  115. /**
  116. * 状态修改
  117. * @param $id
  118. * @return \think\response\Json
  119. * @author Qinii
  120. */
  121. public function switchStatus($id)
  122. {
  123. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  124. if (!$this->repository->exists($id))
  125. return app('json')->fail('数据不存在');
  126. $this->repository->update($id, ['status' => $status]);
  127. app()->make(CommunityCategoryRepository::class)->clearCahe();
  128. return app('json')->success('修改成功');
  129. }
  130. public function checkParams()
  131. {
  132. $data = $this->request->params(['category_id', 'topic_name', 'is_hot', 'status', 'sort', 'pic']);
  133. app()->make(CommunityTopicValidate::class)->check($data);
  134. return $data;
  135. }
  136. /**
  137. * 获取话题列表
  138. * @return \think\response\Json
  139. * @author Qinii
  140. */
  141. public function getOptions()
  142. {
  143. return app('json')->success($this->repository->options());
  144. }
  145. /**
  146. * 推荐
  147. * @param $id
  148. * @return \think\response\Json
  149. * @author Qinii
  150. */
  151. public function switchHot($id)
  152. {
  153. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  154. if (!$this->repository->exists($id))
  155. return app('json')->fail('数据不存在');
  156. $this->repository->update($id, ['is_hot' => $status]);
  157. app()->make(CommunityCategoryRepository::class)->clearCahe();
  158. return app('json')->success('修改成功');
  159. }
  160. }