StoreCategory.php 7.7 KB

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