CommunityCategory.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 crmeb\basic\BaseController;
  13. use think\App;
  14. use app\validate\admin\StoreCategoryValidate;
  15. use app\common\repositories\community\CommunityCategoryRepository as repository;
  16. /**
  17. * 社区分类
  18. */
  19. class CommunityCategory extends BaseController
  20. {
  21. /**
  22. * @var CommunityCategoryRepository
  23. */
  24. protected $repository;
  25. /**
  26. * User constructor.
  27. * @param App $app
  28. * @param $repository
  29. */
  30. public function __construct(App $app, repository $repository)
  31. {
  32. parent::__construct($app);
  33. $this->repository = $repository;
  34. }
  35. /**
  36. * 列表
  37. * @return mixed
  38. * @author Qinii
  39. */
  40. public function lst()
  41. {
  42. $where = $this->request->params(['cate_name']);
  43. [$page, $limit] = $this->getPage();
  44. return app('json')->success($this->repository->getList($where, $page, $limit));
  45. }
  46. /**
  47. * 创建表单
  48. * @return \think\response\Json
  49. * @author Qinii
  50. * @day 10/26/21
  51. */
  52. public function createForm()
  53. {
  54. return app('json')->success(formToData($this->repository->form(null)));
  55. }
  56. /**
  57. * 创建
  58. * @return \think\response\Json
  59. * @author Qinii
  60. */
  61. public function create()
  62. {
  63. $data = $this->checkParams();
  64. $data['cate_name'] = trim($data['cate_name']);
  65. if ($this->repository->fieldExists('cate_name', $data['cate_name'], null))
  66. return app('json')->fail('分类名重复');
  67. $this->repository->create($data);
  68. return app('json')->success('添加成功');
  69. }
  70. /**
  71. * 更新表单
  72. * @param $id
  73. * @return \think\response\Json
  74. * @author Qinii
  75. * @day 10/26/21
  76. */
  77. public function updateForm($id)
  78. {
  79. if (!$this->repository->exists($id))
  80. return app('json')->fail('数据不存在');
  81. $this->repository->clearCahe();
  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('cate_name', $data['cate_name'], $id))
  96. return app('json')->fail('分类名重复');
  97. $this->repository->update($id, $data);
  98. $this->repository->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->delete($id);
  112. $this->repository->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, ['is_show' => $status]);
  127. $this->repository->clearCahe();
  128. return app('json')->success('修改成功');
  129. }
  130. public function checkParams()
  131. {
  132. $data = $this->request->params(['pid', 'cate_name', 'is_show', 'sort']);
  133. $data['pid'] = 0;
  134. app()->make(StoreCategoryValidate::class)->check($data);
  135. return $data;
  136. }
  137. /**
  138. * 获取分类全部数据
  139. * @return \think\response\Json
  140. * @author Qinii
  141. */
  142. public function getOptions()
  143. {
  144. return app('json')->success($this->repository->options());
  145. }
  146. }