StoreCart.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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\store\order;
  12. use app\common\repositories\store\order\StoreOrderRepository;
  13. use app\common\repositories\store\pionts\PointsProductRepository;
  14. use app\common\repositories\store\product\ProductAssistRepository;
  15. use app\common\repositories\store\product\ProductAssistSetRepository;
  16. use app\common\repositories\store\product\ProductAttrValueRepository;
  17. use app\common\repositories\store\product\ProductGroupRepository;
  18. use app\common\repositories\store\product\ProductPresellRepository;
  19. use app\common\repositories\store\product\ProductRepository;
  20. use app\common\repositories\store\product\StoreDiscountProductRepository;
  21. use app\common\repositories\store\product\StoreDiscountRepository;
  22. use app\common\repositories\store\StoreSeckillActiveRepository;
  23. use app\common\repositories\user\UserRepository;
  24. use MongoDB\BSON\MaxKey;
  25. use think\App;
  26. use crmeb\basic\BaseController;
  27. use app\validate\api\StoreCartValidate as validate;
  28. use app\common\repositories\store\order\StoreCartRepository as repository;
  29. use think\exception\ValidateException;
  30. class StoreCart extends BaseController
  31. {
  32. /**
  33. * @var repository
  34. */
  35. protected $repository;
  36. /**
  37. * StoreBrand constructor.
  38. * @param App $app
  39. * @param repository $repository
  40. */
  41. public function __construct(App $app, repository $repository)
  42. {
  43. parent::__construct($app);
  44. $this->repository = $repository;
  45. }
  46. /**
  47. * 获取列表信息
  48. *
  49. * 本函数用于从仓库中获取列表数据,并以成功响应的形式返回给调用方。
  50. * 它首先通过请求对象获取用户信息,然后利用这些信息从仓库中获取相应的列表数据。
  51. * 最后,使用应用的JSON工具类将数据包装在一个成功的响应中返回。
  52. *
  53. * @return \Illuminate\Http\JsonResponse 成功获取数据后的JSON响应
  54. */
  55. public function lst()
  56. {
  57. // 从仓库中根据当前用户信息获取列表数据
  58. $data = $this->repository->getList($this->request->userInfo());
  59. // 构建并返回一个表示成功的JSON响应,包含获取的数据
  60. return app('json')->success($data);
  61. }
  62. /**
  63. * 添加商品到购物车
  64. *
  65. * @param validate $validate 输入验证器,用于验证请求参数的合法性
  66. * @return json 返回操作结果,如果失败则包含错误信息
  67. */
  68. public function create(validate $validate)
  69. {
  70. // 根据验证器验证参数,并获取验证后的数据
  71. $data = $this->checkParams($validate);
  72. // 检查商品类型是否在允许的范围内
  73. if(!in_array($data['product_type'],[0,1,2,3,4,20]))
  74. return app('json')->fail('商品类型错误');
  75. // 检查购买数量是否大于0
  76. if ($data['cart_num'] <= 0)
  77. return app('json')->fail('购买数量有误');
  78. // 获取当前用户信息
  79. $user = $this->request->userInfo();
  80. // 触发购物车添加前的事件,允许其他功能插件在此事件中进行干预
  81. event('user.cart.before',compact('user','data'));
  82. // 初始化商品来源和来源ID
  83. $data['source'] = $data['product_type'];
  84. $data['source_id'] = $data['product_id'];
  85. // 根据商品类型执行不同的检查逻辑
  86. switch ($data['product_type'])
  87. {
  88. case 0: // 普通商品
  89. $result = app()->make(ProductRepository::class)->cartCheck($data,$this->request->userInfo());
  90. // 处理商品来源,如果请求参数中包含有效的来源信息,则更新商品来源和来源ID
  91. [$source, $sourceId, $pid] = explode(':', $this->request->param('source', '0'), 3) + ['', '', ''];
  92. $data['source'] = (in_array($source, [0, 1]) && $pid == $data['product_id']) ? $source : 0;
  93. if ($data['source'] > 0) $data['source_id'] = intval($sourceId);
  94. break;
  95. case 1: // 秒杀商品
  96. $result = app()->make(ProductRepository::class)->cartSeckillCheck($data,$this->request->userInfo());
  97. $data['source_id'] = $result['active_id'];
  98. break;
  99. case 2: // 预售商品
  100. $result = app()->make(ProductPresellRepository::class)->cartCheck($data,$this->request->userInfo());
  101. $data['product_id'] = $result['product']['product_id'];
  102. break;
  103. case 3: // 助力商品
  104. $result = app()->make(ProductAssistSetRepository::class)->cartCheck($data,$this->request->userInfo());
  105. $data['product_id'] = $result['product']['product_id'];
  106. break;
  107. case 4: // 拼团商品
  108. $result = app()->make(ProductGroupRepository::class)->cartCheck($data,$this->request->userInfo());
  109. $data['product_id'] = $result['product']['product_id'];
  110. $data['source_id'] = $data['group_buying_id'];
  111. break;
  112. case 20:// 积分商品
  113. $result = app()->make(PointsProductRepository::class)->cartCheck($data,$this->request->userInfo());
  114. $data['product_id'] = $result['product']['product_id'];
  115. break;
  116. }
  117. // 移除不需要保存到购物车的字段
  118. unset($data['group_buying_id']);
  119. // 如果购物车中已存在该商品,则更新数量;否则,添加新商品到购物车
  120. if ($cart = $result['cart']) {
  121. // 更新购物车
  122. $cart_id = $cart['cart_id'];
  123. $cart_num = ['cart_num' => ($cart['cart_num'] + $data['cart_num'])];
  124. $storeCart = $this->repository->update($cart_id,$cart_num);
  125. } else {
  126. // 添加购物车
  127. $data['uid'] = $this->request->uid();
  128. $data['mer_id'] = $result['product']['mer_id'];
  129. $cart = $storeCart = $this->repository->create($data);
  130. }
  131. // 触发购物车添加后的事件
  132. event('user.cart', compact('user','storeCart'));
  133. // 返回添加成功的购物车ID
  134. return app('json')->success(['cart_id' => (int)$cart['cart_id']]);
  135. }
  136. /**
  137. * 修改购物车中商品的数量。
  138. *
  139. * 此方法用于处理用户在购物车中更改商品数量的请求。它首先验证请求的数量是否有效,
  140. * 然后检查商品的库存和购买限制,最后更新购物车中的商品数量。
  141. *
  142. * @param int $id 购物车项的ID。
  143. * @return \think\Response 返回一个JSON响应,包含操作的结果信息。
  144. */
  145. public function change($id)
  146. {
  147. // 从请求中获取修改后的商品数量和唯一属性ID
  148. $where = $this->request->params(['cart_num']);
  149. $product_attr_unique = $this->request->param('product_attr_unique');
  150. // 检查修改后的数量是否小于0,如果是,则返回错误信息
  151. if (intval($where['cart_num']) < 0)
  152. return app('json')->fail('数量必须大于0');
  153. // 尝试根据ID和用户ID获取购物车项,如果不存在,则返回错误信息
  154. if (!$cart = $this->repository->getOne($id, $this->request->uid()))
  155. return app('json')->fail('购物车信息不存在');
  156. // 如果商品有单次购买限制,检查此次购买数量是否超过限制
  157. if ($cart->product->once_count) {
  158. $cart_num = app()->make(ProductRepository::class)->productOnceCountCart($cart['product_id'], $this->request->uid());
  159. if (($cart_num - $cart['cart_num'] + $where['cart_num']) > $cart->product->once_count)
  160. return app('json')->fail('单次购买限制 ' . $cart->product->once_count . ' 件');
  161. }
  162. // 根据唯一属性ID检查SKU是否存在,如果不存在,则返回错误信息
  163. if (!$res = app()->make(ProductAttrValueRepository::class)->getOptionByUnique($product_attr_unique ?? $cart['product_attr_unique']))
  164. return app('json')->fail('SKU不存在');
  165. // 检查库存是否足够,如果不足,则返回错误信息
  166. if ($res['stock'] < $where['cart_num'])
  167. return app('json')->fail('库存不足');
  168. // 如果提供了唯一属性ID,则更新购物车项的唯一属性ID
  169. if($product_attr_unique){
  170. $where['product_attr_unique'] = $product_attr_unique;
  171. }
  172. // 检测购物车是否存在和该商品相同sku的购物车记录,如果有则合并加数量,并删除多余的
  173. if(!empty($product_attr_unique)) {
  174. $isExist = $this->repository->getCartByProductSku($product_attr_unique, $this->request->uid(), '', $id);
  175. if($isExist){
  176. $where['cart_num'] = bcadd($cart['cart_num'], $isExist['cart_num']);
  177. $this->repository->delete($isExist['cart_id']);
  178. }
  179. }
  180. // 更新购物车项的数量
  181. $this->repository->update($id, $where);
  182. // 返回成功信息
  183. return app('json')->success('修改成功');
  184. }
  185. /**
  186. * 批量删除购物车中的商品
  187. *
  188. * 本函数用于处理用户请求,批量删除用户购物车中的指定商品项。
  189. * 它首先从请求中获取待删除的购物车项的ID列表,然后调用仓库接口进行批量删除操作。
  190. * 如果ID列表为空,或者删除操作失败,函数将返回相应的错误信息。
  191. * 成功删除所有指定购物车项后,函数会返回一个成功提示。
  192. *
  193. * @return \think\response\Json 删除操作的结果,成功时包含成功消息,失败时包含错误消息。
  194. */
  195. public function batchDelete()
  196. {
  197. // 从请求中获取待删除的购物车项的ID列表
  198. $ids = $this->request->param('cart_id');
  199. // 检查ID列表是否为空,如果为空则返回参数错误的响应
  200. if(!count($ids)) return app('json')->fail('参数错误');
  201. // 调用仓库接口,批量删除指定的购物车项
  202. $this->repository->batchDelete($ids, $this->request->uid());
  203. // 返回删除成功的响应
  204. return app('json')->success('删除成功');
  205. }
  206. /**
  207. * 获取购物车商品数量
  208. *
  209. * 本函数用于获取当前用户购物车中的商品总数。它通过调用仓库层的相应方法来实现,
  210. * 并使用JSON工具类对结果进行包装,以便于前端获取和显示。
  211. *
  212. * @return \Illuminate\Http\JsonResponse 返回一个包含购物车商品数量的JSON响应
  213. */
  214. public function cartCount()
  215. {
  216. // 调用应用程序中的JSON工具类,以成功状态返回用户购物车商品数量
  217. return app('json')->success($this->repository->getCartCount($this->request->uid()));
  218. }
  219. /**
  220. * 检查购物车中商品的合法性
  221. * 该方法用于在添加或更新购物车商品前,验证商品及相关属性的有效性,包括商品存在性、库存、属性唯一性等。
  222. * @param array $data 包含商品ID、属性唯一标识和购买数量的数据数组
  223. * @return array 返回经过验证和处理后的数据数组,包含是否为新商品、用户ID、商家ID等信息
  224. * @throws ValidateException 如果验证过程中发现任何问题,将抛出此异常
  225. */
  226. public function check($data)
  227. {
  228. // 根据商品ID获取商品信息
  229. $product = app()->make(ProductRepository::class)->get($data['product_id']);
  230. // 如果商品不存在,则抛出异常
  231. if (!$product) {
  232. throw new ValidateException('商品不存在');
  233. }
  234. // 如果购买数量小于0,则抛出异常
  235. if ($data['cart_num'] < 0) {
  236. throw new ValidateException('数量必须大于0');
  237. }
  238. // 根据商品属性唯一标识获取属性值信息
  239. if (!$res = app()->make(ProductAttrValueRepository::class)->getOptionByUnique($data['product_attr_unique'])) {
  240. throw new ValidateException('SKU不存在');
  241. }
  242. // 如果获取的属性值所属的商品ID与输入的商品ID不一致,则抛出异常
  243. if ($res['product_id'] != $data['product_id']) {
  244. throw new ValidateException('数据不一致');
  245. }
  246. // 如果购买数量超过属性值的库存,则抛出异常
  247. if ($res['stock'] < $data['cart_num']) {
  248. throw new ValidateException('库存不足');
  249. }
  250. // 设置数据数组中的字段,标记为新商品,设置用户ID和商家ID
  251. $data['is_new'] = 1;
  252. $data['uid'] = $this->request->uid();
  253. $data['mer_id'] = $product['mer_id'];
  254. // 返回处理后的数据数组
  255. return $data;
  256. }
  257. /**
  258. * 再次提交数据进行验证和创建
  259. * 本函数的目的是接收并验证一组数据,然后将这些数据提交给仓库创建购物车项。
  260. * 它首先通过验证器对每个数据项进行验证,然后将验证通过的数据项创建为购物车项。
  261. *
  262. * @param validate $validate 验证器对象,用于数据验证
  263. * @return json 返回一个包含创建的购物车项ID的JSON对象
  264. */
  265. public function again(validate $validate)
  266. {
  267. // 从请求中获取名为data的参数,如果不存在则默认为空数组
  268. $param = $this->request->param('data',[]);
  269. // 遍历参数数组中的每个数据项,进行验证和处理
  270. foreach ($param as $data){
  271. // 使用验证器对当前数据项进行验证
  272. $validate->check($data);
  273. // 调用本对象的check方法进行进一步处理,将处理结果存入$item数组
  274. $item[] = $this->check($data);
  275. }
  276. // 遍历$item数组,将每个数据项提交给仓库创建购物车项,并将购物车ID存入$ids数组
  277. foreach ($item as $it){
  278. // 在仓库中创建购物车项,并获取创建的购物车ID
  279. $it__id = $this->repository->create($it);
  280. // 将购物车ID转换为整数类型,并存入$ids数组
  281. $ids[] = (int)$it__id['cart_id'];
  282. }
  283. // 返回一个成功的JSON响应,包含所有创建的购物车项的ID
  284. return app('json')->success(['cart_id' => $ids]);
  285. }
  286. /**
  287. * 检查请求参数的合法性,并处理传播者ID的相关逻辑。
  288. *
  289. * 此方法主要用于在添加或更新购物车项时,验证请求参数是否符合预期规则,
  290. * 并对传播者ID进行特殊处理,确保其有效性。
  291. *
  292. * @param validate $validate 验证器对象,用于参数验证。
  293. * @return array 返回验证后的参数数据。
  294. */
  295. public function checkParams(validate $validate)
  296. {
  297. // 从请求中提取指定参数,包括产品ID、产品属性唯一标识、购物车数量、是否为新品、产品类型、团购ID和传播者ID。
  298. $data = $this->request->params(['product_id','product_attr_unique','cart_num','is_new',['product_type',0],['group_buying_id',0],['spread_id',0],['reservation_id',0],['reservation_date','']]);
  299. // 使用验证器对提取的参数数据进行验证。
  300. $validate->check($data);
  301. // 如果传播者ID存在
  302. if ($data['spread_id']) {
  303. // 如果传播者ID不等于当前用户ID,则检查该传播者ID对应的用户是否存在。
  304. if ($data['spread_id'] !== $this->request->userInfo()->uid){
  305. // 如果传播者用户不存在,则将传播者ID设为0。
  306. $user = app()->make(UserRepository::class)->get($data['spread_id']);
  307. if (!$user) $data['spread_id'] = 0;
  308. } else {
  309. // 如果传播者ID等于当前用户ID,为了防止自传播,将传播者ID设为0。
  310. $data['spread_id'] = 0;
  311. }
  312. }
  313. // 返回处理后的参数数据。
  314. return $data;
  315. }
  316. /**
  317. * 套餐购买
  318. * @return \think\response\Json
  319. * @author Qinii
  320. * @day 1/7/22
  321. */
  322. public function batchCreate()
  323. {
  324. $data = $this->request->params(['data','discount_id','is_new']);
  325. $productRepostory = app()->make(ProductRepository::class);
  326. if (!$data['discount_id'])
  327. return app('json')->fail('优惠套餐ID不能为空');
  328. if (!$data['is_new'])
  329. return app('json')->fail('套餐不能加入购物车');
  330. $cartData = app()->make(StoreDiscountRepository::class)->check($data['discount_id'], $data['data'], $this->request->userInfo());
  331. $cart_id = [];
  332. if ($cartData){
  333. foreach ($cartData as $datum) {
  334. $datum['is_new'] = $data['is_new'];
  335. $cart = $this->repository->create($datum);
  336. $cart_id[] = (int)$cart['cart_id'];
  337. }
  338. }
  339. return app('json')->success(compact('cart_id'));
  340. }
  341. }