StoreCartController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /** @var StoreOrderServices $orderServices */
  93. $orderServices = app()->make(StoreOrderServices::class);
  94. if ($is_gift == 1) {
  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. if (!$orderServices->checkGift($uid)) {
  105. return app('json')->fail('礼包商品只能购买一次');
  106. }
  107. } else {
  108. // 非礼包商品,检查用户是否购买过礼包商品
  109. $uid = $request->uid();
  110. if ($uid!=1){
  111. if ($orderServices->checkGift($uid)) {
  112. return app('json')->fail('必须先买礼包商品');
  113. }
  114. }
  115. }
  116. // $is_repeat = $storeServices->value(['id'=>$where['productId']],'is_repeat');
  117. // if ($is_repeat == 1){
  118. // return app('json')->fail('复购商品商品不支持加入购物车');
  119. // }
  120. $res = $cartService->setCart($request->uid(), $where['productId'], $where['cartNum'], $where['uniqueId'], $type, $new, $where['combinationId'], $where['secKillId'], $where['bargainId'], $where['advanceId']);
  121. if (!$res) return app('json')->fail(100022);
  122. else return app('json')->success(['cartId' => $res]);
  123. }
  124. /**
  125. * 购物车 删除商品
  126. * @param Request $request
  127. * @return mixed
  128. */
  129. public function del(Request $request)
  130. {
  131. $where = $request->postMore([
  132. ['ids', ''],//购物车编号
  133. ]);
  134. $where['ids'] = is_array($where['ids']) ? $where['ids'] : explode(',', $where['ids']);
  135. if (!count($where['ids']))
  136. return app('json')->fail(100100);
  137. if ($this->services->removeUserCart((int)$request->uid(), $where['ids']))
  138. return app('json')->success(100002);
  139. return app('json')->fail(100008);
  140. }
  141. /**
  142. * 购物车 修改商品数量
  143. * @param Request $request
  144. * @return mixed
  145. * @throws \think\db\exception\DataNotFoundException
  146. * @throws \think\db\exception\DbException
  147. * @throws \think\db\exception\ModelNotFoundException
  148. */
  149. public function num(Request $request)
  150. {
  151. $where = $request->postMore([
  152. ['id', 0],//购物车编号
  153. ['number', 0],//购物车编号
  154. ]);
  155. if (!$where['id'] || !is_numeric($where['id'])) return app('json')->fail(100100);
  156. if (!$where['number'] || !is_numeric($where['number'])) return app('json')->fail(100007);
  157. $res = $this->services->changeUserCartNum($where['id'], $where['number'], $request->uid());
  158. if ($res) return app('json')->success(100001);
  159. else return app('json')->fail(100007);
  160. }
  161. /**
  162. * 购物车 统计 数量 价格
  163. * @param Request $request
  164. * @return mixed
  165. * @throws \think\db\exception\DataNotFoundException
  166. * @throws \think\db\exception\DbException
  167. * @throws \think\db\exception\ModelNotFoundException
  168. */
  169. public function count(Request $request)
  170. {
  171. [$numType] = $request->postMore([
  172. ['numType', true],//购物车编号
  173. ], true);
  174. $uid = (int)$request->uid();
  175. return app('json')->success($this->services->getUserCartCount($uid, $numType));
  176. }
  177. /**
  178. * 购物车重选
  179. * @param Request $request
  180. * @return mixed
  181. */
  182. public function reChange(Request $request)
  183. {
  184. [$cart_id, $product_id, $unique] = $request->postMore([
  185. ['cart_id', 0],
  186. ['product_id', 0],
  187. ['unique', '']
  188. ], true);
  189. $this->services->modifyCart($cart_id, $product_id, $unique);
  190. return app('json')->success(410225);
  191. }
  192. }