StoreCartController.php 6.5 KB

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