StoreCategory.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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\store;
  12. use app\validate\admin\StoreCategoryValidate as validate;
  13. use think\App;
  14. use crmeb\basic\BaseController;
  15. use app\common\repositories\store\StoreCategoryRepository as repository;
  16. class StoreCategory extends BaseController
  17. {
  18. /**
  19. * @var repository|ArticleCategoryRepository
  20. */
  21. protected $repository;
  22. /**
  23. * ArticleCategory constructor.
  24. * @param App $app
  25. * @param ArticleCategoryRepository $repository
  26. */
  27. public function __construct(App $app, repository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. }
  32. /**
  33. * 列表
  34. * @return mixed
  35. * @author Qinii
  36. */
  37. public function lst()
  38. {
  39. return app('json')->success($this->repository->getFormatList($this->request->merId()));
  40. }
  41. /**
  42. * @Author:Qinii
  43. * @Date: 2020/5/11
  44. * @return mixed
  45. */
  46. public function createForm()
  47. {
  48. return app('json')->success(formToData($this->repository->form($this->request->merId())));
  49. }
  50. /**
  51. * @Author:Qinii
  52. * @Date: 2020/5/11
  53. * @param $id
  54. * @return mixed
  55. */
  56. public function updateForm($id)
  57. {
  58. if (!$this->repository->merExists($this->request->merId(), $id))
  59. return app('json')->fail('数据不存在');
  60. return app('json')->success(formToData($this->repository->updateForm($this->request->merId(),$id)));
  61. }
  62. /**
  63. * @Author:Qinii
  64. * @Date: 2020/5/11
  65. * @param validate $validate
  66. * @return mixed
  67. */
  68. public function create(validate $validate)
  69. {
  70. $data = $this->checkParams($validate);
  71. $data['cate_name'] = trim($data['cate_name']);
  72. if($data['cate_name'] == '') return app('json')->fail('分类名不可为空');
  73. if ($data['pid'] && !$this->repository->merExists($this->request->merId(), $data['pid']))
  74. return app('json')->fail('上级分类不存在');
  75. if ($data['pid'] && !$this->repository->checkLevel($data['pid'],0,$this->request->merId()))
  76. return app('json')->fail('不可添加更低阶分类');
  77. $data['mer_id'] = $this->request->merId();
  78. $this->repository->create($data);
  79. return app('json')->success('添加成功');
  80. }
  81. /**
  82. * @Author:Qinii
  83. * @Date: 2020/5/11
  84. * @param $id
  85. * @param validate $validate
  86. * @return mixed
  87. */
  88. public function update($id,validate $validate)
  89. {
  90. $data = $this->checkParams($validate);
  91. if(!$this->repository->checkUpdate($id,$data['pid'])){
  92. if (!$this->repository->merExists($this->request->merId(), $id))
  93. return app('json')->fail('数据不存在');
  94. if ($data['pid'] && !$this->repository->merExists($this->request->merId(), $data['pid']))
  95. return app('json')->fail('上级分类不存在');
  96. if ($data['pid'] && !$this->repository->checkLevel($data['pid'],0,$this->request->merId()))
  97. return app('json')->fail('不可添加更低阶分类');
  98. if (!$this->repository->checkChangeToChild($id,$data['pid']))
  99. return app('json')->fail('无法修改到当前分类到子集,请先修改子类');
  100. if (!$this->repository->checkChildLevel($id,$data['pid']))
  101. return app('json')->fail('子类超过最低限制,请先修改子类');
  102. }
  103. $this->repository->update($id,$data);
  104. return app('json')->success('编辑成功');
  105. }
  106. /**
  107. * 修改状态
  108. * @param int $id
  109. * @return mixed
  110. * @author Qinii
  111. */
  112. public function switchStatus($id)
  113. {
  114. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  115. if (!$this->repository->merExists($this->request->merId(), $id))
  116. return app('json')->fail('数据不存在');
  117. $this->repository->switchStatus($id, $status);
  118. return app('json')->success('修改成功');
  119. }
  120. /**
  121. * @Author:Qinii
  122. * @Date: 2020/5/11
  123. * @param $id
  124. * @return mixed
  125. */
  126. public function delete($id)
  127. {
  128. if (!$this->repository->merExists($this->request->merId(), $id))
  129. return app('json')->fail('数据不存在');
  130. if ($this->repository->hasChild($id))
  131. return app('json')->fail('该分类存在子集,请先处理子集');
  132. $this->repository->delete($id);
  133. return app('json')->success('删除成功');
  134. }
  135. /**
  136. * @Author:Qinii
  137. * @Date: 2020/5/11
  138. * @param $id
  139. * @return mixed
  140. */
  141. public function detail($id)
  142. {
  143. if (!$this->repository->merExists($this->request->merId(), $id))
  144. return app('json')->fail('数据不存在');
  145. return app('json')->success($this->repository->get($id));
  146. }
  147. /**
  148. * 验证
  149. * @param WechatNewsValidate $validate
  150. * @param bool $isCreate
  151. * @return array
  152. * @author Qinii
  153. */
  154. public function checkParams(validate $validate)
  155. {
  156. $data = $this->request->params(['pid','cate_name','is_show','pic','sort']);
  157. $validate->check($data);
  158. return $data;
  159. }
  160. /**
  161. * @Author:Qinii
  162. * @Date: 2020/5/16
  163. * @return mixed
  164. */
  165. public function getTreeList()
  166. {
  167. return app('json')->success($this->repository->getTreeList($this->request->merId()));
  168. }
  169. /**
  170. * @Author:Qinii
  171. * @Date: 2020/5/18
  172. * @return mixed
  173. */
  174. public function getList()
  175. {
  176. return app('json')->success($this->repository->getList());
  177. }
  178. /**
  179. * @Author:Qinii
  180. * @Date: 2020/5/18
  181. * @return mixed
  182. */
  183. public function BrandList()
  184. {
  185. return app('json')->success($this->repository->getBrandList());
  186. }
  187. }