StoreCategory.php 5.7 KB

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