Product.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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\merchant\store\product;
  12. use app\common\repositories\store\order\StoreCartRepository;
  13. use crmeb\jobs\ChangeSpuStatusJob;
  14. use crmeb\services\UploadService;
  15. use think\App;
  16. use crmeb\basic\BaseController;
  17. use app\validate\merchant\StoreProductValidate as validate;
  18. use app\common\repositories\store\product\ProductRepository as repository;
  19. use think\exception\ValidateException;
  20. class Product extends BaseController
  21. {
  22. protected $repository ;
  23. /**
  24. * Product constructor.
  25. * @param App $app
  26. * @param repository $repository
  27. */
  28. public function __construct(App $app ,repository $repository)
  29. {
  30. parent::__construct($app);
  31. $this->repository = $repository;
  32. }
  33. /**
  34. * @Author:Qinii
  35. * @Date: 2020/5/18
  36. * @return mixed
  37. */
  38. public function lst()
  39. {
  40. [$page, $limit] = $this->getPage();
  41. $where = $this->request->params(['cate_id','keyword',['type',1],'mer_cate_id','is_gift_bag','status','us_status','product_id']);
  42. $where = array_merge($where,$this->repository->switchType($where['type'],$this->request->merId(),0));
  43. return app('json')->success($this->repository->getList($this->request->merId(),$where, $page, $limit));
  44. }
  45. /**
  46. * @Author:Qinii
  47. * @Date: 2020/5/18
  48. * @param $id
  49. * @return mixed
  50. */
  51. public function detail($id)
  52. {
  53. if(!$this->repository->merExists($this->request->merId(),$id))
  54. return app('json')->fail('数据不存在');
  55. return app('json')->success($this->repository->getAdminOneProduct($id,null));
  56. }
  57. /**
  58. * @Author:Qinii
  59. * @Date: 2020/5/18
  60. * @param validate $validate
  61. * @return mixed
  62. */
  63. public function create(validate $validate)
  64. {
  65. $merchant = $this->request->merchant();
  66. $data = $this->checkParams($validate);
  67. $data['mer_id'] = $this->request->merId();
  68. if ($data['is_gift_bag'] && !$this->repository->checkMerchantBagNumber($data['mer_id'])) return app('json')->fail('礼包数量超过数量限制');
  69. $this->repository->check($data,$this->request->merId());
  70. $data['status'] = $this->request->merchant()->is_audit ? 0 : 1;
  71. $data['mer_status'] = ($merchant['is_del'] || !$merchant['mer_state'] || !$merchant['status']) ? 0 : 1;
  72. $data['rate'] = 3;
  73. $this->repository->create($data,0);
  74. return app('json')->success('添加成功');
  75. }
  76. /**
  77. * @Author:Qinii
  78. * @Date: 2020/5/18
  79. * @param $id
  80. * @param validate $validate
  81. * @return mixed
  82. */
  83. public function update($id,validate $validate)
  84. {
  85. $merchant = $this->request->merchant();
  86. $data = $this->checkParams($validate);
  87. if (!$this->repository->merExists($this->request->merId(), $id))
  88. return app('json')->fail('数据不存在');
  89. $this->repository->check($data, $this->request->merId());
  90. $pro = $this->repository->getWhere(['product_id' => $id]);
  91. if ($pro->status == -2) {
  92. $data['status'] = 0;
  93. } else {
  94. $data['status'] = $this->request->merchant()->is_audit ? 0 : 1;
  95. }
  96. $data['mer_status'] = ($merchant['is_del'] || !$merchant['mer_state'] || !$merchant['status']) ? 0 : 1;
  97. unset($data['is_gift_bag']);
  98. $data['mer_id'] = $this->request->merId();
  99. $this->repository->edit($id, $data, $this->request->merId(), 0);
  100. return app('json')->success('编辑成功');
  101. }
  102. /**
  103. * @Author:Qinii
  104. * @Date: 2020/5/18
  105. * @param $id
  106. * @return mixed
  107. */
  108. public function delete($id)
  109. {
  110. if(!$this->repository->merExists($this->request->merId(),$id))
  111. return app('json')->fail('数据不存在');
  112. if($this->repository->getWhereCount(['product_id' => $id,'is_show' => 1,'status' => 1]))
  113. return app('json')->fail('商品上架中');
  114. $this->repository->delete($id);
  115. queue(ChangeSpuStatusJob::class,['product_type' => 0,'id' => $id]);
  116. return app('json')->success('转入回收站');
  117. }
  118. public function destory($id)
  119. {
  120. if(!$this->repository->merDeleteExists($this->request->merId(),$id))
  121. return app('json')->fail('只能删除回收站的商品');
  122. if(app()->make(StoreCartRepository::class)->getProductById($id))
  123. return app('json')->fail('商品有被加入购物车不可删除');
  124. $this->repository->destory($id);
  125. return app('json')->success('删除成功');
  126. }
  127. /**
  128. * @Author:Qinii
  129. * @Date: 2020/5/18
  130. * @param int $id
  131. * @return mixed
  132. */
  133. public function switchStatus($id)
  134. {
  135. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  136. if(!$this->repository->merExists($this->request->merId(),$id))
  137. return app('json')->fail('数据不存在');
  138. $this->repository->switchStatus([$id], ['is_show' => $status]);
  139. return app('json')->success('修改成功');
  140. }
  141. /**
  142. * @Author:Qinii
  143. * @Date: 2020/5/18
  144. * @return mixed
  145. */
  146. public function getStatusFilter()
  147. {
  148. return app('json')->success($this->repository->getFilter($this->request->merId(),'商品',0));
  149. }
  150. /**
  151. * @Author:Qinii
  152. * @Date: 2020/5/8
  153. * @Time: 14:39
  154. * @param validate $validate
  155. * @return array
  156. */
  157. public function checkParams(validate $validate)
  158. {
  159. $params = [
  160. "image", "slider_image", "store_name", "store_info", "keyword", "bar_code", "brand_id",
  161. "cate_id", "mer_cate_id", "unit_name", "sort" , "is_show", "is_good",'is_gift_bag',
  162. "video_link", "temp_id", "content", "spec_type","extension_type", "attr", "attrValue",['give_coupon_ids',[]]
  163. ];
  164. $data = $this->request->params($params);
  165. $validate->check($data);
  166. return $data;
  167. }
  168. /**
  169. * TODO
  170. * @return mixed
  171. * @author Qinii
  172. * @day 2020-06-24
  173. */
  174. public function config()
  175. {
  176. $data['extension_status'] = systemConfig('extension_status');
  177. return app('json')->success($data);
  178. }
  179. /**
  180. * TODO
  181. * @param $id
  182. * @return mixed
  183. * @author Qinii
  184. * @day 2020-07-03
  185. */
  186. public function restore($id)
  187. {
  188. if(!$this->repository->merDeleteExists($this->request->merId(),$id))
  189. return app('json')->fail('只能删除回收站的商品');
  190. $this->repository->restore($id);
  191. return app('json')->success('商品已恢复');
  192. }
  193. /**
  194. * @return \think\response\Json
  195. * @throws \think\Exception
  196. * @author xaboy
  197. * @day 2020/11/16
  198. */
  199. public function temp_key()
  200. {
  201. $upload = UploadService::create();
  202. $re = $upload->getTempKeys();
  203. return app('json')->success($re);
  204. }
  205. public function updateSort($id)
  206. {
  207. $sort = $this->request->param('sort');
  208. $this->repository->updateSort($id,$this->request->merId(),['sort' => $sort]);
  209. return app('json')->success('修改成功');
  210. }
  211. }