StoreCartController.php 6.2 KB

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