StoreCartController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\api\controller\v1\store;
  12. use app\Request;
  13. use app\services\activity\combination\StorePinkServices;
  14. use app\services\order\StoreCartServices;
  15. use app\services\order\StoreOrderCartInfoServices;
  16. use app\services\product\product\StoreProductServices;
  17. /**
  18. * 购物车类
  19. * Class StoreCartController
  20. * @package app\api\controller\store
  21. */
  22. class StoreCartController
  23. {
  24. protected $services;
  25. public function __construct(StoreCartServices $services)
  26. {
  27. $this->services = $services;
  28. }
  29. /**
  30. * 购物车 列表
  31. * @param Request $request
  32. * @return mixed
  33. * @throws \think\db\exception\DataNotFoundException
  34. * @throws \think\db\exception\DbException
  35. * @throws \think\db\exception\ModelNotFoundException
  36. */
  37. public function lst(Request $request)
  38. {
  39. [$status] = $request->postMore([
  40. ['status', 1],//购物车商品状态
  41. ], true);
  42. return app('json')->success($this->services->getUserCartList($request->uid(), $status));
  43. }
  44. /**
  45. * 购物车 添加
  46. * @param Request $request
  47. * @return mixed
  48. * @throws \Psr\SimpleCache\InvalidArgumentException
  49. * @throws \think\db\exception\DataNotFoundException
  50. * @throws \think\db\exception\DbException
  51. * @throws \think\db\exception\ModelNotFoundException
  52. */
  53. public function add(Request $request)
  54. {
  55. $where = $request->postMore([
  56. [['productId', 'd'], 0],//普通商品编号
  57. [['cartNum', 'd'], 1], //购物车数量
  58. ['uniqueId', ''],//属性唯一值
  59. [['new', 'd'], 0],// 1 加入购物车直接购买 0 加入购物车
  60. [['is_new', 'd'], 0],// 1 加入购物车直接购买 0 加入购物车
  61. [['combinationId', 'd'], 0],//拼团商品编号
  62. [['secKillId', 'd'], 0],//秒杀商品编号
  63. [['bargainId', 'd'], 0],//砍价商品编号
  64. [['advanceId', 'd'], 0],//预售商品编号
  65. [['pinkId', 'd'], 0],//拼团团队ID
  66. ]);
  67. if ($where['is_new'] || $where['new']) $new = true;
  68. else $new = false;
  69. /** @var StoreCartServices $cartService */
  70. $cartService = app()->make(StoreCartServices::class);
  71. if (!$where['productId'] || !is_numeric($where['productId'])) return app('json')->fail(100100);
  72. $type = 0;
  73. if ($where['secKillId']) {
  74. $type = 1;
  75. } elseif ($where['bargainId']) {
  76. $type = 2;
  77. } elseif ($where['combinationId']) {
  78. $type = 3;
  79. if ($where['pinkId']) {
  80. /** @var StorePinkServices $pinkServices */
  81. $pinkServices = app()->make(StorePinkServices::class);
  82. if ($pinkServices->isPinkStatus($where['pinkId'])) return app('json')->fail(410315);
  83. }
  84. } elseif ($where['advanceId']) {
  85. $type = 6;
  86. }
  87. // 检测是否是礼包商品
  88. $storeServices = app()->make(StoreProductServices::class);
  89. $is_gift = $storeServices->value(['id'=>$where['productId']],'is_gift');
  90. // 礼包商品只能购买一次检查
  91. if ($is_gift == 1) {
  92. /** @var StoreOrderCartInfoServices $cartInfoServices */
  93. $cartInfoServices = app()->make(StoreOrderCartInfoServices::class);
  94. // 直接查询用户是否购买过该礼包商品的已支付订单
  95. if ($cartInfoServices->hasUserPurchasedProduct($request->uid(), $where['productId'])) {
  96. return app('json')->fail('礼包商品只能购买一次');
  97. }
  98. }
  99. $is_repeat = $storeServices->value(['id'=>$where['productId']],'is_repeat');
  100. if ($is_repeat == 1){
  101. return app('json')->fail('复购商品商品不支持加入购物车');
  102. }
  103. $res = $cartService->setCart($request->uid(), $where['productId'], $where['cartNum'], $where['uniqueId'], $type, $new, $where['combinationId'], $where['secKillId'], $where['bargainId'], $where['advanceId']);
  104. if (!$res) return app('json')->fail(100022);
  105. else return app('json')->success(['cartId' => $res]);
  106. }
  107. /**
  108. * 购物车 删除商品
  109. * @param Request $request
  110. * @return mixed
  111. */
  112. public function del(Request $request)
  113. {
  114. $where = $request->postMore([
  115. ['ids', ''],//购物车编号
  116. ]);
  117. $where['ids'] = is_array($where['ids']) ? $where['ids'] : explode(',', $where['ids']);
  118. if (!count($where['ids']))
  119. return app('json')->fail(100100);
  120. if ($this->services->removeUserCart((int)$request->uid(), $where['ids']))
  121. return app('json')->success(100002);
  122. return app('json')->fail(100008);
  123. }
  124. /**
  125. * 购物车 修改商品数量
  126. * @param Request $request
  127. * @return mixed
  128. * @throws \think\db\exception\DataNotFoundException
  129. * @throws \think\db\exception\DbException
  130. * @throws \think\db\exception\ModelNotFoundException
  131. */
  132. public function num(Request $request)
  133. {
  134. $where = $request->postMore([
  135. ['id', 0],//购物车编号
  136. ['number', 0],//购物车编号
  137. ]);
  138. if (!$where['id'] || !is_numeric($where['id'])) return app('json')->fail(100100);
  139. if (!$where['number'] || !is_numeric($where['number'])) return app('json')->fail(100007);
  140. $res = $this->services->changeUserCartNum($where['id'], $where['number'], $request->uid());
  141. if ($res) return app('json')->success(100001);
  142. else return app('json')->fail(100007);
  143. }
  144. /**
  145. * 购物车 统计 数量 价格
  146. * @param Request $request
  147. * @return mixed
  148. * @throws \think\db\exception\DataNotFoundException
  149. * @throws \think\db\exception\DbException
  150. * @throws \think\db\exception\ModelNotFoundException
  151. */
  152. public function count(Request $request)
  153. {
  154. [$numType] = $request->postMore([
  155. ['numType', true],//购物车编号
  156. ], true);
  157. $uid = (int)$request->uid();
  158. return app('json')->success($this->services->getUserCartCount($uid, $numType));
  159. }
  160. /**
  161. * 购物车重选
  162. * @param Request $request
  163. * @return mixed
  164. */
  165. public function reChange(Request $request)
  166. {
  167. [$cart_id, $product_id, $unique] = $request->postMore([
  168. ['cart_id', 0],
  169. ['product_id', 0],
  170. ['unique', '']
  171. ], true);
  172. $this->services->modifyCart($cart_id, $product_id, $unique);
  173. return app('json')->success(410225);
  174. }
  175. }