Category.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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\points;
  12. use app\common\repositories\store\StoreCategoryRepository;
  13. use app\validate\admin\StoreCategoryValidate as validate;
  14. use think\App;
  15. use crmeb\basic\BaseController;
  16. /**
  17. * 商品分类
  18. */
  19. class Category extends BaseController
  20. {
  21. protected $repository;
  22. public function __construct(App $app, StoreCategoryRepository $repository)
  23. {
  24. parent::__construct($app);
  25. $this->repository = $repository;
  26. }
  27. /**
  28. * 列表
  29. * @return \think\response\Json
  30. * @author Qinii
  31. */
  32. public function lst()
  33. {
  34. return app('json')->success($this->repository->getFormatList($this->request->merId(), null, 1));
  35. }
  36. /**
  37. * 创建表单
  38. * @Author:Qinii
  39. * @Date: 2020/5/11
  40. * @return mixed
  41. */
  42. public function createForm()
  43. {
  44. return app('json')->success(formToData($this->repository->pointsForm(null)));
  45. }
  46. /**
  47. * 创建
  48. * @Author:Qinii
  49. * @Date: 2020/5/11
  50. * @param validate $validate
  51. * @return mixed
  52. */
  53. public function create(validate $validate)
  54. {
  55. $data = $this->checkParams($validate);
  56. $data['cate_name'] = trim($data['cate_name']);
  57. if ($data['cate_name'] == '') return app('json')->fail('分类名不可为空');
  58. $data['mer_id'] = $this->request->merId();
  59. $this->repository->create($data);
  60. return app('json')->success('添加成功');
  61. }
  62. /**
  63. * 编辑表单
  64. * @param $id
  65. * @return \think\response\Json
  66. * @author Qinii
  67. */
  68. public function updateForm($id)
  69. {
  70. if (!$this->repository->merExists($this->request->merId(), $id))
  71. return app('json')->fail('数据不存在');
  72. return app('json')->success(formToData($this->repository->pointsForm($id)));
  73. }
  74. /**
  75. * 编辑
  76. * @param $id
  77. * @param validate $validate
  78. * @return \think\response\Json
  79. * @author Qinii
  80. */
  81. public function update($id, validate $validate)
  82. {
  83. $data = $this->checkParams($validate);
  84. if (!$this->repository->merExists($this->request->merId(), $id))
  85. return app('json')->fail('数据不存在');
  86. $this->repository->update($id, $data);
  87. return app('json')->success('编辑成功');
  88. }
  89. /**
  90. * 状态修改
  91. * @param $id
  92. * @return \think\response\Json
  93. * @author Qinii
  94. */
  95. public function switchStatus($id)
  96. {
  97. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  98. if (!$this->repository->merExists($this->request->merId(), $id))
  99. return app('json')->fail('数据不存在');
  100. $this->repository->switchStatus($id, $status);
  101. return app('json')->success('修改成功');
  102. }
  103. /**
  104. * 删除
  105. * @param $id
  106. * @return \think\response\Json
  107. * @author Qinii
  108. */
  109. public function delete($id)
  110. {
  111. if (!$this->repository->merExists($this->request->merId(), $id))
  112. return app('json')->fail('数据不存在');
  113. if ($this->repository->hasChild($id))
  114. return app('json')->fail('该分类存在子集,请先处理子集');
  115. $this->repository->delete($id);
  116. return app('json')->success('删除成功');
  117. }
  118. /**
  119. * 所有数据,下啦筛选
  120. * @return \think\response\Json
  121. * @author Qinii
  122. */
  123. public function select()
  124. {
  125. return app('json')->success($this->repository->getAll(0, 1, 1));
  126. }
  127. public function checkParams(validate $validate)
  128. {
  129. $data = $this->request->params(['cate_name', 'is_show', 'pic', 'sort', 'type', ['pid', 0]]);
  130. $validate->check($data);
  131. return $data;
  132. }
  133. }