StoreCartController.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. $uid = $request->uid();
  97. @file_put_contents("quanju.txt", "礼包检查: uid=$uid, is_gift=$is_gift\r\n", 8);
  98. // 先查看符合条件的订单详情
  99. $hasOrder = $orderServices->count([
  100. ['uid'=> $uid],
  101. ['order_type'=> 1],
  102. ['paid'=> 1]
  103. ]);
  104. @file_put_contents("quanju.txt", "礼包检查: hasOrder=$hasOrder\r\n", 8);
  105. if ($hasOrder > 0) {
  106. return app('json')->fail('礼包商品只能购买一次');
  107. }
  108. }
  109. // $is_repeat = $storeServices->value(['id'=>$where['productId']],'is_repeat');
  110. // if ($is_repeat == 1){
  111. // return app('json')->fail('复购商品商品不支持加入购物车');
  112. // }
  113. $res = $cartService->setCart($request->uid(), $where['productId'], $where['cartNum'], $where['uniqueId'], $type, $new, $where['combinationId'], $where['secKillId'], $where['bargainId'], $where['advanceId']);
  114. if (!$res) return app('json')->fail(100022);
  115. else return app('json')->success(['cartId' => $res]);
  116. }
  117. /**
  118. * 购物车 删除商品
  119. * @param Request $request
  120. * @return mixed
  121. */
  122. public function del(Request $request)
  123. {
  124. $where = $request->postMore([
  125. ['ids', ''],//购物车编号
  126. ]);
  127. $where['ids'] = is_array($where['ids']) ? $where['ids'] : explode(',', $where['ids']);
  128. if (!count($where['ids']))
  129. return app('json')->fail(100100);
  130. if ($this->services->removeUserCart((int)$request->uid(), $where['ids']))
  131. return app('json')->success(100002);
  132. return app('json')->fail(100008);
  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 num(Request $request)
  143. {
  144. $where = $request->postMore([
  145. ['id', 0],//购物车编号
  146. ['number', 0],//购物车编号
  147. ]);
  148. if (!$where['id'] || !is_numeric($where['id'])) return app('json')->fail(100100);
  149. if (!$where['number'] || !is_numeric($where['number'])) return app('json')->fail(100007);
  150. $res = $this->services->changeUserCartNum($where['id'], $where['number'], $request->uid());
  151. if ($res) return app('json')->success(100001);
  152. else return app('json')->fail(100007);
  153. }
  154. /**
  155. * 购物车 统计 数量 价格
  156. * @param Request $request
  157. * @return mixed
  158. * @throws \think\db\exception\DataNotFoundException
  159. * @throws \think\db\exception\DbException
  160. * @throws \think\db\exception\ModelNotFoundException
  161. */
  162. public function count(Request $request)
  163. {
  164. [$numType] = $request->postMore([
  165. ['numType', true],//购物车编号
  166. ], true);
  167. $uid = (int)$request->uid();
  168. return app('json')->success($this->services->getUserCartCount($uid, $numType));
  169. }
  170. /**
  171. * 购物车重选
  172. * @param Request $request
  173. * @return mixed
  174. */
  175. public function reChange(Request $request)
  176. {
  177. [$cart_id, $product_id, $unique] = $request->postMore([
  178. ['cart_id', 0],
  179. ['product_id', 0],
  180. ['unique', '']
  181. ], true);
  182. $this->services->modifyCart($cart_id, $product_id, $unique);
  183. return app('json')->success(410225);
  184. }
  185. }