StoreCartController.php 7.2 KB

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