CommunityCategory.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 crmeb\basic\BaseController;
  13. use crmeb\traits\CategoresRepository;
  14. use think\App;
  15. use app\validate\admin\StoreCategoryValidate;
  16. use app\common\repositories\community\CommunityCategoryRepository as repository;
  17. class CommunityCategory extends BaseController
  18. {
  19. /**
  20. * @var CommunityCategoryRepository
  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(['cate_name']);
  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['cate_name'] = trim($data['cate_name']);
  57. if ($this->repository->fieldExists('cate_name', $data['cate_name'],null))
  58. return app('json')->fail('分类名重复');
  59. $this->repository->create($data);
  60. return app('json')->success('添加成功');
  61. }
  62. /**
  63. * TODO
  64. * @param $id
  65. * @return \think\response\Json
  66. * @author Qinii
  67. * @day 10/26/21
  68. */
  69. public function updateForm($id)
  70. {
  71. if (!$this->repository->exists($id))
  72. return app('json')->fail('数据不存在');
  73. $this->repository->clearCahe();
  74. return app('json')->success(formToData($this->repository->form($id)));
  75. }
  76. public function update($id)
  77. {
  78. $data = $this->checkParams();
  79. if (!$this->repository->exists($id))
  80. return app('json')->fail('数据不存在');
  81. if ($this->repository->fieldExists('cate_name', $data['cate_name'],$id))
  82. return app('json')->fail('分类名重复');
  83. $this->repository->update($id,$data);
  84. $this->repository->clearCahe();
  85. return app('json')->success('编辑成功');
  86. }
  87. /**
  88. * @param $id
  89. * @return mixed
  90. * @author Qinii
  91. */
  92. public function delete($id)
  93. {
  94. if (!$this->repository->exists($id))
  95. return app('json')->fail('数据不存在');
  96. $this->repository->delete($id);
  97. $this->repository->clearCahe();
  98. return app('json')->success('删除成功');
  99. }
  100. public function switchStatus($id)
  101. {
  102. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  103. if (!$this->repository->exists($id))
  104. return app('json')->fail('数据不存在');
  105. $this->repository->update($id,['is_show' => $status]);
  106. $this->repository->clearCahe();
  107. return app('json')->success('修改成功');
  108. }
  109. public function checkParams()
  110. {
  111. $data = $this->request->params(['pid','cate_name','is_show','sort']);
  112. $data['pid'] = 0;
  113. app()->make(StoreCategoryValidate::class)->check($data);
  114. return $data;
  115. }
  116. public function getOptions()
  117. {
  118. return app('json')->success($this->repository->options());
  119. }
  120. }