Product.php 8.4 KB

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