Product.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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\merchant\store\product;
  12. use app\common\repositories\store\order\StoreCartRepository;
  13. use app\common\repositories\store\product\ProductAttrValueRepository;
  14. use app\common\repositories\store\product\SpuRepository;
  15. use app\common\repositories\store\shipping\ShippingTemplateRepository;
  16. use app\common\repositories\store\StoreCategoryRepository;
  17. use crmeb\services\UploadService;
  18. use think\App;
  19. use crmeb\basic\BaseController;
  20. use app\validate\merchant\StoreProductValidate as validate;
  21. use app\common\repositories\store\product\ProductRepository as repository;
  22. use think\exception\ValidateException;
  23. class Product extends BaseController
  24. {
  25. protected $repository ;
  26. /**
  27. * Product constructor.
  28. * @param App $app
  29. * @param repository $repository
  30. */
  31. public function __construct(App $app ,repository $repository)
  32. {
  33. parent::__construct($app);
  34. $this->repository = $repository;
  35. }
  36. /**
  37. * @Author:Qinii
  38. * @Date: 2020/5/18
  39. * @return mixed
  40. */
  41. public function lst()
  42. {
  43. [$page, $limit] = $this->getPage();
  44. $where = $this->request->params(['temp_id','cate_id','keyword',['type',1],'mer_cate_id','is_gift_bag','status','us_status','product_id','mer_labels',['order','sort'],'is_ficti']);
  45. $where = array_merge($where,$this->repository->switchType($where['type'],$this->request->merId(),0));
  46. return app('json')->success($this->repository->getList($this->request->merId(),$where, $page, $limit));
  47. }
  48. /**
  49. * @Author:Qinii
  50. * @Date: 2020/5/18
  51. * @param $id
  52. * @return mixed
  53. */
  54. public function detail($id)
  55. {
  56. if(!$this->repository->merExists($this->request->merId(),$id))
  57. return app('json')->fail('数据不存在');
  58. return app('json')->success($this->repository->getAdminOneProduct($id,null));
  59. }
  60. /**
  61. * @Author:Qinii
  62. * @Date: 2020/5/18
  63. * @param validate $validate
  64. * @return mixed
  65. */
  66. public function create()
  67. {
  68. $params = $this->request->params($this->repository::CREATE_PARAMS);
  69. $data = $this->repository->checkParams($params,$this->request->merId());
  70. $data['mer_id'] = $this->request->merId();
  71. if ($data['is_gift_bag'] && !$this->repository->checkMerchantBagNumber($data['mer_id']))
  72. return app('json')->fail('礼包数量超过数量限制');
  73. $data['status'] = $this->request->merchant()->is_audit ? 0 : 1;
  74. $data['mer_status'] = ($this->request->merchant()->is_del || !$this->request->merchant()->mer_state || !$this->request->merchant()->status) ? 0 : 1;
  75. $data['rate'] = 3;
  76. $this->repository->create($data,0);
  77. return app('json')->success('添加成功');
  78. }
  79. /**
  80. * @Author:Qinii
  81. * @Date: 2020/5/18
  82. * @param $id
  83. * @param validate $validate
  84. * @return mixed
  85. */
  86. public function update($id)
  87. {
  88. $params = $this->request->params($this->repository::CREATE_PARAMS);
  89. $data = $this->repository->checkParams($params,$this->request->merId(), $id);
  90. if (!$this->repository->merExists($this->request->merId(), $id))
  91. return app('json')->fail('数据不存在');
  92. $pro = $this->repository->getWhere(['product_id' => $id]);
  93. if ($pro->status == -2) {
  94. $data['status'] = 0;
  95. } else {
  96. $data['status'] = $this->request->merchant()->is_audit ? 0 : 1;
  97. }
  98. $data['mer_status'] = ($this->request->merchant()->is_del || !$this->request->merchant()->mer_state || !$this->request->merchant()->status) ? 0 : 1;
  99. $data['mer_id'] = $this->request->merId();
  100. $this->repository->edit($id, $data, $this->request->merId(), 0);
  101. return app('json')->success('编辑成功');
  102. }
  103. /**
  104. * @Author:Qinii
  105. * @Date: 2020/5/18
  106. * @param $id
  107. * @return mixed
  108. */
  109. public function delete($id)
  110. {
  111. if(!$this->repository->merExists($this->request->merId(),$id))
  112. return app('json')->fail('数据不存在');
  113. if($this->repository->getWhereCount(['product_id' => $id,'is_show' => 1,'status' => 1]))
  114. return app('json')->fail('商品上架中');
  115. $this->repository->delete($id);
  116. //queue(ChangeSpuStatusJob::class,['product_type' => 0,'id' => $id]);
  117. return app('json')->success('转入回收站');
  118. }
  119. public function destory($id)
  120. {
  121. if(!$this->repository->merDeleteExists($this->request->merId(),$id))
  122. return app('json')->fail('只能删除回收站的商品');
  123. if(app()->make(StoreCartRepository::class)->getProductById($id))
  124. return app('json')->fail('商品有被加入购物车不可删除');
  125. $this->repository->destory($id);
  126. return app('json')->success('删除成功');
  127. }
  128. /**
  129. * @Author:Qinii
  130. * @Date: 2020/5/18
  131. * @return mixed
  132. */
  133. public function getStatusFilter()
  134. {
  135. return app('json')->success($this->repository->getFilter($this->request->merId(),'商品',0));
  136. }
  137. /**
  138. * TODO
  139. * @return mixed
  140. * @author Qinii
  141. * @day 2020-06-24
  142. */
  143. public function config()
  144. {
  145. $data = systemConfig(['extension_status','svip_switch_status','integral_status']);
  146. $merData= merchantConfig($this->request->merId(),['mer_integral_status','mer_integral_rate','mer_svip_status','svip_store_rate']);
  147. $svip_store_rate = $merData['svip_store_rate'] > 0 ? bcdiv($merData['svip_store_rate'],100,2) : 0;
  148. $data['mer_svip_status'] = ($data['svip_switch_status'] && $merData['mer_svip_status'] != 0 ) ? 1 : 0;
  149. $data['svip_store_rate'] = $svip_store_rate;
  150. $data['integral_status'] = $data['integral_status'] && $merData['mer_integral_status'] ? 1 : 0;
  151. $data['integral_rate'] = $merData['mer_integral_rate'] ?: 0;
  152. $data['delivery_way'] = $this->request->merchant()->delivery_way ? $this->request->merchant()->delivery_way : [2];
  153. $data['is_audit'] = $this->request->merchant()->is_audit;
  154. return app('json')->success($data);
  155. }
  156. /**
  157. * TODO
  158. * @param $id
  159. * @return mixed
  160. * @author Qinii
  161. * @day 2020-07-03
  162. */
  163. public function restore($id)
  164. {
  165. if(!$this->repository->merDeleteExists($this->request->merId(),$id))
  166. return app('json')->fail('只能恢复回收站的商品');
  167. $this->repository->restore($id);
  168. return app('json')->success('商品已恢复');
  169. }
  170. /**
  171. * @return \think\response\Json
  172. * @throws \think\Exception
  173. * @author xaboy
  174. * @day 2020/11/16
  175. */
  176. public function temp_key()
  177. {
  178. $upload = UploadService::create();
  179. $re = $upload->getTempKeys();
  180. return app('json')->success($re);
  181. }
  182. public function updateSort($id)
  183. {
  184. $sort = $this->request->param('sort');
  185. $this->repository->updateSort($id,$this->request->merId(),['sort' => $sort]);
  186. return app('json')->success('修改成功');
  187. }
  188. public function preview()
  189. {
  190. $data = $this->request->param();
  191. $data['merchant'] = [
  192. 'mer_name' => $this->request->merchant()->mer_name,
  193. 'is_trader' => $this->request->merchant()->is_trader,
  194. 'mer_avatar' => $this->request->merchant()->mer_avatar,
  195. 'product_score' => $this->request->merchant()->product_score,
  196. 'service_score' => $this->request->merchant()->service_score,
  197. 'postage_score' => $this->request->merchant()->postage_score,
  198. 'service_phone' => $this->request->merchant()->service_phone,
  199. 'care_count' => $this->request->merchant()->care_count,
  200. 'type_name' => $this->request->merchant()->type_name->type_name ?? '',
  201. 'care' => true,
  202. 'recommend' => $this->request->merchant()->recommend,
  203. ];
  204. $data['mer_id'] = $this->request->merId();
  205. $data['status'] = 1;
  206. $data['mer_status'] = 1;
  207. $data['rate'] = 3;
  208. return app('json')->success($this->repository->preview($data));
  209. }
  210. public function setLabels($id)
  211. {
  212. $data = $this->request->params(['mer_labels']);
  213. app()->make(SpuRepository::class)->setLabels($id,0,$data,$this->request->merId());
  214. return app('json')->success('修改成功');
  215. }
  216. public function getAttrValue($id)
  217. {
  218. $data = $this->repository->getAttrValue($id, $this->request->merId());
  219. return app('json')->success($data);
  220. }
  221. public function freeTrial($id)
  222. {
  223. $params = [
  224. "mer_cate_id",
  225. "sort" ,
  226. "is_show",
  227. "is_good",
  228. "attr",
  229. "attrValue",
  230. 'spec_type'
  231. ];
  232. $data = $this->request->params($params);
  233. $count = app()->make(StoreCategoryRepository::class)->getWhereCount(['store_category_id' => $data['mer_cate_id'],'is_show' => 1,'mer_id' => $this->request->merId()]);
  234. if (!$count) throw new ValidateException('商户分类不存在或不可用');
  235. $data['status'] = 1;
  236. $this->repository->freeTrial($id, $data,$this->request->merId());
  237. return app('json')->success('编辑成功');
  238. }
  239. /**
  240. * TODO 上下架
  241. * @Author:Qinii
  242. * @Date: 2020/5/18
  243. * @param int $id
  244. * @return mixed
  245. */
  246. public function switchStatus($id)
  247. {
  248. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  249. $this->repository->switchShow($id, $status,'is_show',$this->request->merId());
  250. return app('json')->success('修改成功');
  251. }
  252. /**
  253. * TODO 批量上下架
  254. * @return \think\response\Json
  255. * @author Qinii
  256. * @day 2022/9/6
  257. */
  258. public function batchShow()
  259. {
  260. $ids = $this->request->param('ids');
  261. if (empty($ids)) return app('json')->fail('请选择商品');
  262. $status = $this->request->param('status') == 1 ? 1 : 0;
  263. $this->repository->batchSwitchShow($ids,$status,'is_show',$this->request->merId());
  264. return app('json')->success('修改成功');
  265. }
  266. /**
  267. * TODO 批量设置模板
  268. * @return \think\response\Json
  269. * @author Qinii
  270. * @day 2022/9/6
  271. */
  272. public function batchTemplate()
  273. {
  274. $ids = $this->request->param('ids');
  275. $ids = is_array($ids) ? $ids : explode(',',$ids);
  276. $data = $this->request->params(['temp_id']);
  277. if (empty($ids)) return app('json')->fail('请选择商品');
  278. if (empty($data['temp_id'])) return app('json')->fail('请选择运费模板');
  279. if (!$this->repository->merInExists($this->request->merId(), $ids)) return app('json')->fail('请选择您自己商品');
  280. $make = app()->make(ShippingTemplateRepository::class);
  281. if (!$make->merInExists($this->request->merId(), [$data['temp_id']]))
  282. return app('json')->fail('请选择您自己的运费模板');
  283. $data['delivery_free'] = 0;
  284. $this->repository->updates($ids,$data);
  285. return app('json')->success('修改成功');
  286. }
  287. /**
  288. * TODO 批量标签
  289. * @return \think\response\Json
  290. * @author Qinii
  291. * @day 2022/9/6
  292. */
  293. public function batchLabels()
  294. {
  295. $ids = $this->request->param('ids');
  296. $data = $this->request->params(['mer_labels']);
  297. if (empty($ids)) return app('json')->fail('请选择商品');
  298. if (!$this->repository->merInExists($this->request->merId(), $ids))
  299. return app('json')->fail('请选择您自己商品');
  300. app()->make(SpuRepository::class)->batchLabels($ids, $data,$this->request->merId());
  301. return app('json')->success('修改成功');
  302. }
  303. /**
  304. * TODO 批量设置推荐类型
  305. * @return \think\response\Json
  306. * @author Qinii
  307. * @day 2022/9/6
  308. */
  309. public function batchHot()
  310. {
  311. $ids = $this->request->param('ids');
  312. $data['is_good'] = 1;
  313. if (empty($ids)) return app('json')->fail('请选择商品');
  314. if (!$this->repository->merInExists($this->request->merId(), $ids))
  315. return app('json')->fail('请选择您自己商品');
  316. $this->repository->updates($ids,$data);
  317. return app('json')->success('修改成功');
  318. }
  319. /**
  320. * TODO 批量设置佣金
  321. * @param ProductAttrValueRepository $repository
  322. * @return \think\response\Json
  323. * @author Qinii
  324. * @day 2022/12/26
  325. */
  326. public function batchExtension(ProductAttrValueRepository $repository)
  327. {
  328. $ids = $this->request->param('ids');
  329. $data = $this->request->params(['extension_one','extension_two']);
  330. if ($data['extension_one'] > 1 || $data['extension_one'] < 0 || $data['extension_two'] < 0 || $data['extension_two'] > 1) {
  331. return app('json')->fail('比例0~1之间');
  332. }
  333. if (empty($ids)) return app('json')->fail('请选择商品');
  334. if (!$this->repository->merInExists($this->request->merId(), $ids))
  335. return app('json')->fail('请选择您自己商品');
  336. $repository->updatesExtension($ids,$data);
  337. return app('json')->success('修改成功');
  338. }
  339. public function batchSvipType()
  340. {
  341. $ids = $this->request->param('ids');
  342. $data = $this->request->params([['svip_price_type',0]]);
  343. if (empty($ids)) return app('json')->fail('请选择商品');
  344. if (!$this->repository->merInExists($this->request->merId(), $ids))
  345. return app('json')->fail('请选择您自己商品');
  346. $this->repository->updates($ids,$data);
  347. return app('json')->success('修改成功');
  348. }
  349. }