StoreCategory.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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\api\server;
  12. use app\common\repositories\store\StoreCategoryRepository;
  13. use think\App;
  14. use crmeb\basic\BaseController;
  15. use think\exception\HttpResponseException;
  16. use app\validate\admin\StoreCategoryValidate;
  17. use app\common\repositories\store\service\StoreServiceRepository;
  18. /**
  19. * Class StoreCategory
  20. * app\controller\api\server
  21. * 移动客服 商品分类管理
  22. */
  23. class StoreCategory extends BaseController
  24. {
  25. protected $merId;
  26. protected $repository;
  27. public function __construct(App $app, StoreCategoryRepository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. $this->merId = $this->request->route('merId');
  32. }
  33. /**
  34. * 列表
  35. * @param $merId
  36. * @return \think\response\Json
  37. * @author Qinii
  38. * @day 8/24/21
  39. */
  40. public function lst($merId)
  41. {
  42. return app('json')->success($this->repository->getFormatList($merId));
  43. }
  44. /**
  45. * 创建
  46. * @param $merId
  47. * @param StoreCategoryValidate $validate
  48. * @return \think\response\Json
  49. * @author Qinii
  50. * @day 8/24/21
  51. */
  52. public function create($merId, StoreCategoryValidate $validate)
  53. {
  54. $data = $this->checkParams($validate);
  55. $data['cate_name'] = trim($data['cate_name']);
  56. if($data['cate_name'] == '') return app('json')->fail('分类名不可为空');
  57. if ($data['pid'] && !$this->repository->merExists($merId, $data['pid']))
  58. return app('json')->fail('上级分类不存在');
  59. if ($data['pid'] && !$this->repository->checkLevel($data['pid'],0,$merId))
  60. return app('json')->fail('不可添加更低阶分类');
  61. $data['mer_id'] = $merId;
  62. $this->repository->create($data);
  63. return app('json')->success('添加成功');
  64. }
  65. /**
  66. * 编辑
  67. * @param $merId
  68. * @param $id
  69. * @param StoreCategoryValidate $validate
  70. * @return \think\response\Json
  71. * @author Qinii
  72. * @day 8/24/21
  73. */
  74. public function update($merId, $id, StoreCategoryValidate $validate)
  75. {
  76. $data = $this->checkParams($validate);
  77. if(!$this->repository->checkUpdate($id,$data['pid'])){
  78. if (!$this->repository->merExists($merId, $id))
  79. return app('json')->fail('数据不存在');
  80. if ($data['pid'] && !$this->repository->merExists($merId, $data['pid']))
  81. return app('json')->fail('上级分类不存在');
  82. if ($data['pid'] && !$this->repository->checkLevel($data['pid'],0, $merId))
  83. return app('json')->fail('不可添加更低阶分类');
  84. if (!$this->repository->checkChangeToChild($id,$data['pid']))
  85. return app('json')->fail('无法修改到当前分类到子集,请先修改子类');
  86. if (!$this->repository->checkChildLevel($id,$data['pid'],$merId))
  87. return app('json')->fail('子类超过最低限制,请先修改子类');
  88. }
  89. $this->repository->update($id,$data);
  90. return app('json')->success('编辑成功');
  91. }
  92. /**
  93. * 修改状态
  94. * @param $id
  95. * @return \think\response\Json
  96. * @author Qinii
  97. * @day 8/24/21
  98. */
  99. public function switchStatus($id)
  100. {
  101. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  102. if (!$this->repository->merExists($this->merId, $id))
  103. return app('json')->fail('数据不存在');
  104. $this->repository->switchStatus($id, $status);
  105. return app('json')->success('修改成功');
  106. }
  107. /**
  108. * 详情
  109. * @param $id
  110. * @return \think\response\Json
  111. * @author Qinii
  112. * @day 8/24/21
  113. */
  114. public function detail($id)
  115. {
  116. if (!$this->repository->merExists($this->merId, $id))
  117. return app('json')->fail('数据不存在');
  118. return app('json')->success($this->repository->get($id));
  119. }
  120. public function delete($id)
  121. {
  122. if (!$this->repository->merExists($this->merId, $id))
  123. return app('json')->fail('数据不存在');
  124. if ($this->repository->hasChild($id))
  125. return app('json')->fail('该分类存在子集,请先处理子集');
  126. $this->repository->delete($id);
  127. return app('json')->success('删除成功');
  128. }
  129. /**
  130. * 列表 树形结构
  131. * @return \think\response\Json
  132. * @author Qinii
  133. * @day 8/24/21
  134. */
  135. public function getTreeList()
  136. {
  137. $data = $this->repository->getTreeList($this->merId,1);
  138. $ret = [];
  139. foreach ($data as $datum) {
  140. if (isset($datum['children'])) {
  141. $ret[] = $datum;
  142. }
  143. }
  144. return app('json')->success($ret);
  145. }
  146. /**
  147. * 列表 - 必须三级
  148. * @return \think\response\Json
  149. * @author Qinii
  150. * @day 8/24/21
  151. */
  152. public function getList()
  153. {
  154. $data = $this->repository->getList(1);
  155. $ret = [];
  156. foreach ($data as $key => $value) {
  157. if (isset($value['children'])) {
  158. $level = [];
  159. foreach ($value['children'] as $child) {
  160. if (isset($child['children'])) {
  161. $level[] = $child;
  162. }
  163. }
  164. if (isset($level) && !empty($level)) {
  165. $value['children'] = $level;
  166. $ret[] = $value;
  167. }
  168. }
  169. }
  170. return app('json')->success($ret);
  171. }
  172. /**
  173. * 品牌列表
  174. * @return \think\response\Json
  175. * @author Qinii
  176. * @day 8/24/21
  177. */
  178. public function BrandList()
  179. {
  180. return app('json')->success($this->repository->getBrandList());
  181. }
  182. /**
  183. * 参数验证
  184. * @param StoreCategoryValidate $validate
  185. * @return array
  186. * @author Qinii
  187. * @day 8/24/21
  188. */
  189. public function checkParams(StoreCategoryValidate $validate)
  190. {
  191. $data = $this->request->params(['pid','cate_name','is_show','pic','sort']);
  192. $validate->check($data);
  193. return $data;
  194. }
  195. }