CommunityTopic.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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. class CommunityTopic extends BaseController
  18. {
  19. /**
  20. * @var CommunityTopicRepository
  21. */
  22. protected $repository;
  23. /**
  24. * User constructor.
  25. * @param App $app
  26. * @param $repository
  27. */
  28. public function __construct(App $app, repository $repository)
  29. {
  30. parent::__construct($app);
  31. $this->repository = $repository;
  32. }
  33. /**
  34. * @return mixed
  35. * @author Qinii
  36. */
  37. public function lst()
  38. {
  39. $where = $this->request->params(['topic_name', 'category_id', 'status','is_hot','is_del']);
  40. [$page, $limit] = $this->getPage();
  41. return app('json')->success($this->repository->getList($where, $page, $limit));
  42. }
  43. /**
  44. * TODO
  45. * @return \think\response\Json
  46. * @author Qinii
  47. * @day 10/26/21
  48. */
  49. public function createForm()
  50. {
  51. return app('json')->success(formToData($this->repository->form(null)));
  52. }
  53. public function create()
  54. {
  55. $data = $this->checkParams();
  56. $data['topic_name'] = trim($data['topic_name']);
  57. if ($this->repository->fieldExists('topic_name', $data['topic_name'],null))
  58. return app('json')->fail('话题重复');
  59. $this->repository->create($data);
  60. app()->make(CommunityCategoryRepository::class)->clearCahe();
  61. return app('json')->success('添加成功');
  62. }
  63. public function updateForm($id)
  64. {
  65. if (!$this->repository->exists($id))
  66. return app('json')->fail('数据不存在');
  67. return app('json')->success(formToData($this->repository->form($id)));
  68. }
  69. public function update($id)
  70. {
  71. $data = $this->checkParams();
  72. if (!$this->repository->exists($id))
  73. return app('json')->fail('数据不存在');
  74. if ($this->repository->fieldExists('topic_name', $data['topic_name'],$id))
  75. return app('json')->fail('话题重复');
  76. $this->repository->update($id,$data);
  77. app()->make(CommunityCategoryRepository::class)->clearCahe();
  78. return app('json')->success('编辑成功');
  79. }
  80. /**
  81. * @param $id
  82. * @return mixed
  83. * @author Qinii
  84. */
  85. public function delete($id)
  86. {
  87. if (!$this->repository->exists($id))
  88. return app('json')->fail('数据不存在');
  89. $this->repository->update($id,['is_del' => 1]);
  90. app()->make(CommunityCategoryRepository::class)->clearCahe();
  91. return app('json')->success('删除成功');
  92. }
  93. public function switchStatus($id)
  94. {
  95. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  96. if (!$this->repository->exists($id))
  97. return app('json')->fail('数据不存在');
  98. $this->repository->update($id,['status' => $status]);
  99. app()->make(CommunityCategoryRepository::class)->clearCahe();
  100. return app('json')->success('修改成功');
  101. }
  102. public function checkParams()
  103. {
  104. $data = $this->request->params(['category_id','topic_name','is_hot','status','sort','pic']);
  105. app()->make(CommunityTopicValidate::class)->check($data);
  106. return $data;
  107. }
  108. public function getOptions()
  109. {
  110. return app('json')->success($this->repository->options());
  111. }
  112. public function switchHot($id)
  113. {
  114. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  115. if (!$this->repository->exists($id))
  116. return app('json')->fail('数据不存在');
  117. $this->repository->update($id,['is_hot' => $status]);
  118. app()->make(CommunityCategoryRepository::class)->clearCahe();
  119. return app('json')->success('修改成功');
  120. }
  121. }