StoreProduct.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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\api\server;
  12. use app\common\repositories\store\order\StoreCartRepository;
  13. use app\common\repositories\store\product\ProductLabelRepository;
  14. use app\common\repositories\store\product\ProductRepository;
  15. use app\common\repositories\store\service\StoreServiceRepository;
  16. use app\common\repositories\system\merchant\MerchantRepository;
  17. use app\validate\merchant\StoreProductValidate;
  18. use crmeb\basic\BaseController;
  19. use crmeb\services\UploadService;
  20. use think\App;
  21. use think\exception\HttpResponseException;
  22. use think\exception\ValidateException;
  23. class StoreProduct extends BaseController
  24. {
  25. protected $merId;
  26. protected $repository;
  27. public function __construct(App $app, ProductRepository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. $this->merId = $this->request->route('merId');
  32. }
  33. /**
  34. * TODO 头部统计
  35. * @param $merId
  36. * @return \think\response\Json
  37. * @author Qinii
  38. * @day 8/24/21
  39. */
  40. public function title($merId)
  41. {
  42. return app('json')->success($this->repository->getFilter($merId, '', 0));
  43. }
  44. /**
  45. * TODO 列表
  46. * @param $merId
  47. * @return \think\response\Json
  48. * @author Qinii
  49. * @day 8/24/21
  50. */
  51. public function lst($merId)
  52. {
  53. [$page, $limit] = $this->getPage();
  54. $where = $this->request->params(['cate_id', 'keyword', ['type',20], 'mer_cate_id', 'is_gift_bag', 'status', 'us_status', 'product_id', 'mer_labels',['order','sort']]);
  55. $where = array_merge($where, $this->repository->switchType($where['type'], $merId, 0));
  56. return app('json')->success($this->repository->getList($merId, $where, $page, $limit));
  57. }
  58. /**
  59. * TODO 添加
  60. * @param $merId
  61. * @param StoreProductValidate $validate
  62. * @return \think\response\Json
  63. * @author Qinii
  64. * @day 8/24/21
  65. */
  66. public function create($merId, StoreProductValidate $validate)
  67. {
  68. $res = $this->request->params($this->repository::CREATE_PARAMS);
  69. $data = $this->repository->checkParams($res,$merId);
  70. $data['mer_id'] = $merId;
  71. $data['is_gift_bag'] = 0;
  72. $merchant = app()->make(MerchantRepository::class)->get($merId);
  73. $data['status'] = $merchant->is_audit ? 0 : 1;
  74. $data['mer_status'] = ($merchant['is_del'] || !$merchant['mer_state'] || !$merchant['status']) ? 0 : 1;
  75. $data['rate'] = 3;
  76. $this->repository->create($data, 0, 1);
  77. return app('json')->success('添加成功');
  78. }
  79. /**
  80. * TODO 编辑
  81. * @param $merId
  82. * @param $id
  83. * @param StoreProductValidate $validate
  84. * @return \think\response\Json
  85. * @author Qinii
  86. * @day 8/24/21
  87. */
  88. public function update($merId, $id, StoreProductValidate $validate)
  89. {
  90. $res = $this->request->params($this->repository::CREATE_PARAMS);
  91. $data = $this->repository->checkParams($res,$merId,$id);
  92. $merchant = app()->make(MerchantRepository::class)->get($merId);
  93. if (!$this->repository->merExists($merId, $id))
  94. return app('json')->fail('数据不存在');
  95. $pro = $this->repository->getWhere(['product_id' => $id]);
  96. if ($pro->status == -2) {
  97. $data['status'] = 0;
  98. } else {
  99. $data['status'] = $merchant->is_audit ? 0 : 1;
  100. }
  101. $data['mer_status'] = ($merchant['is_del'] || !$merchant['mer_state'] || !$merchant['status']) ? 0 : 1;
  102. $data['mer_id'] = $merId;
  103. $this->repository->edit($id, $data, $merId, 0, 1);
  104. return app('json')->success('编辑成功');
  105. }
  106. /**
  107. * TODO 详情
  108. * @param $merId
  109. * @param $id
  110. * @return \think\response\Json
  111. * @author Qinii
  112. * @day 8/24/21
  113. */
  114. public function detail($merId, $id)
  115. {
  116. if (!$this->repository->merExists($merId, $id))
  117. return app('json')->fail('数据不存在');
  118. return app('json')->success($this->repository->getAdminOneProduct($id, 0, 1));
  119. }
  120. /**
  121. * TODO 修改状态
  122. * @param $merId
  123. * @param $id
  124. * @return \think\response\Json
  125. * @author Qinii
  126. * @day 8/24/21
  127. */
  128. public function switchStatus($merId, $id)
  129. {
  130. $status = $this->request->param('status', 0) == 1 ? 1 : 0;
  131. if (!$this->repository->merExists($merId, $id))
  132. return app('json')->fail('数据不存在');
  133. $this->repository->switchShow($id,$status, 'is_show',$merId);
  134. return app('json')->success('修改成功');
  135. }
  136. /**
  137. * TODO 加入回收站
  138. * @param $merId
  139. * @param $id
  140. * @return \think\response\Json
  141. * @author Qinii
  142. * @day 8/24/21
  143. */
  144. public function delete($merId, $id)
  145. {
  146. if (!$this->repository->merExists($merId, $id))
  147. return app('json')->fail('数据不存在');
  148. if ($this->repository->getWhereCount(['product_id' => $id, 'is_show' => 1, 'status' => 1]))
  149. return app('json')->fail('商品上架中');
  150. $this->repository->delete($id);
  151. return app('json')->success('转入回收站');
  152. }
  153. public function config($merId)
  154. {
  155. $data['extension_status'] = systemConfig('extension_status');
  156. $data['integral_status'] = 0;
  157. $data['integral_rate'] = 0;
  158. if(systemConfig('integral_status') && merchantConfig($merId,'mer_integral_status')) {
  159. $data['integral_status'] = 1;
  160. $data['integral_rate'] = merchantConfig($merId,'mer_integral_rate');
  161. }
  162. $merchant = app()->make(MerchantRepository::class)->get($merId);
  163. $data['delivery_way'] = $merchant->delivery_way;
  164. return app('json')->success($data);
  165. }
  166. public function restore($id)
  167. {
  168. if (!$this->repository->merDeleteExists($this->merId, $id))
  169. return app('json')->fail('只能删除回收站的商品');
  170. $this->repository->restore($id);
  171. return app('json')->success('商品已恢复');
  172. }
  173. public function destory($id)
  174. {
  175. if (!$this->repository->merDeleteExists($this->merId, $id))
  176. return app('json')->fail('只能删除回收站的商品');
  177. if (app()->make(StoreCartRepository::class)->getProductById($id))
  178. return app('json')->fail('商品有被加入购物车不可删除');
  179. $this->repository->destory($id);
  180. return app('json')->success('删除成功');
  181. }
  182. public function updateGood($id)
  183. {
  184. $is_good = $this->request->param('is_good', 0) == 1 ? 1 : 0;
  185. if (!$this->repository->merExists($this->merId, $id))
  186. return app('json')->fail('数据不存在');
  187. $this->repository->update($id, ['is_good' => $is_good]);
  188. return app('json')->success('修改成功');
  189. }
  190. }