StoreCategory.php 8.0 KB

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