StoreCartController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\v1\order;
  12. use app\Request;
  13. use app\services\order\StoreCartServices;
  14. use app\services\activity\discounts\StoreDiscountsServices;
  15. use crmeb\services\CacheService;
  16. /**
  17. * 购物车类
  18. * Class StoreCartController
  19. * @package app\controller\api\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. */
  33. public function lst(Request $request)
  34. {
  35. [$status, $latitude, $longitude,$store_id] = $request->postMore([
  36. ['status', 1],//购物车商品状态
  37. ['latitude', ''],
  38. ['longitude', ''],
  39. ['store_id',0]
  40. ], true);
  41. $this->services->setItem('latitude', $latitude)->setItem('longitude', $longitude)->setItem('store_id', (int)$store_id)->setItem('status', $status);
  42. $result = $this->services->getUserCartList($request->uid(), $status);
  43. $this->services->reset();
  44. $result['valid'] = $this->services->getReturnCartList($result['valid'], $result['promotions']);
  45. unset($result['promotions']);
  46. return app('json')->successful($result);
  47. }
  48. /**
  49. * 购物车 添加
  50. * @param Request $request
  51. * @return mixed
  52. * @throws \think\db\exception\DataNotFoundException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. * @throws \think\exception\DbException
  55. */
  56. public function add(Request $request)
  57. {
  58. $where = $request->postMore([
  59. ['productId', 0],//普通商品编号
  60. [['cartNum', 'd'], 1], //购物车数量
  61. ['uniqueId', ''],//属性唯一值
  62. [['new', 'd'], 0],// 1 加入购物车直接购买 0 加入购物车
  63. [['is_new', 'd'], 0],// 1 加入购物车直接购买 0 加入购物车
  64. [['secKillId', 'd'], 0],//秒杀商品编号
  65. [['bargainId', 'd'], 0],//砍价商品编号
  66. [['combinationId', 'd'], 0],//拼团商品编号
  67. [['integralId', 'd'], 0],//积分商品编号
  68. [['discountId', 'd'], 0],//优惠套餐编号
  69. ['discountInfos', []],//优惠套餐商品信息
  70. [['newcomerId', 'd'], 0],//新人专享商品编号
  71. [['luckRecordId', 'd'], 0],//抽奖记录编号
  72. ]);
  73. if ($where['is_new'] || $where['new']) $new = true;
  74. else $new = false;
  75. if (!$where['productId'] && !$where['discountId']) {
  76. return app('json')->fail('参数错误');
  77. }
  78. $type = 0;
  79. $uid = (int)$request->uid();
  80. $activityId = 0;
  81. if ($where['discountId']) {
  82. /** @var StoreDiscountsServices $discountService */
  83. $discountService = app()->make(StoreDiscountsServices::class);
  84. $discounts = $discountService->get((int)$where['discountId'], ['id', 'is_limit', 'limit_num']);
  85. if (!$discounts) {
  86. return app('json')->fail('套餐商品未找到!');
  87. }
  88. //套餐限量
  89. if ($discounts['is_limit']) {
  90. if ($discounts['limit_num'] <= 0) {
  91. return app('json')->fail('套餐限量不足');
  92. }
  93. if (!CacheService::checkStock(md5($discounts['id']), 1, 5)) {
  94. return app('json')->fail('套餐限量不足');
  95. }
  96. }
  97. $cartIds = [];
  98. $cartNum = 0;
  99. $activityId = (int)$where['discountId'];
  100. foreach ($where['discountInfos'] as $info) {
  101. [$cartId, $cartNum] = $this->services->setCart($uid, (int)$info['product_id'], 1, $info['unique'], 5, $new, $activityId, (int)$info['id']);
  102. $cartIds[] = $cartId;
  103. }
  104. } else {
  105. if ($where['secKillId']) {
  106. $type = 1;
  107. $activityId = $where['secKillId'];
  108. } elseif ($where['bargainId']) {
  109. $type = 2;
  110. $activityId = $where['bargainId'];
  111. } elseif ($where['combinationId']) {
  112. $type = 3;
  113. $activityId = $where['combinationId'];
  114. } elseif ($where['integralId']) {
  115. $type = 4;
  116. $activityId = $where['integralId'];
  117. } elseif ($where['newcomerId']) {
  118. $type = 7;
  119. $activityId = $where['newcomerId'];
  120. } elseif ($where['luckRecordId']) {
  121. $type = 8;
  122. $activityId = $where['luckRecordId'];
  123. }
  124. [$cartIds, $cartNum] = $this->services->setCart($uid, (int)$where['productId'], (int)$where['cartNum'], $where['uniqueId'], $type, $new, (int)$activityId);
  125. }
  126. if (!$cartIds) {
  127. return app('json')->fail('添加失败');
  128. } else {
  129. //更新秒杀详情缓存
  130. $this->services->cacheTag('Cart_Nums_' . $uid)->clear();
  131. return app('json')->successful('ok', ['cartId' => $cartIds, 'cartNum' => $cartNum]);
  132. }
  133. }
  134. /**
  135. * 购物车 删除商品
  136. * @param Request $request
  137. * @return mixed
  138. */
  139. public function del(Request $request)
  140. {
  141. [$ids, $store_id] = $request->postMore([
  142. ['ids', ''],//购物车编号
  143. ['store_id',0]
  144. ], true);
  145. $ids = is_array($ids) ? $ids : explode(',', $ids);
  146. if (!count($ids))
  147. return app('json')->fail('参数错误!');
  148. $uid = (int)$request->uid();
  149. $res = $this->services->removeUserCart($uid, $ids);
  150. if ($res) {
  151. $invalid_key = 'invalid_' . $store_id . '_'. $uid;
  152. CacheService::redisHandler()->delete($invalid_key);
  153. return app('json')->successful();
  154. }
  155. return app('json')->fail('清除失败!');
  156. }
  157. /**
  158. * 购物车 修改商品数量
  159. * @param Request $request
  160. * @return mixed
  161. * @throws \think\Exception
  162. * @throws \think\db\exception\DataNotFoundException
  163. * @throws \think\db\exception\ModelNotFoundException
  164. * @throws \think\exception\DbException
  165. */
  166. public function num(Request $request)
  167. {
  168. $where = $request->postMore([
  169. ['id', 0],//购物车编号
  170. ['number', 0],//购物车编号
  171. ]);
  172. if (!$where['id'] || !$where['number'] || !is_numeric($where['id']) || !is_numeric($where['number'])) return app('json')->fail('参数错误!');
  173. $res = $this->services->changeUserCartNum($where['id'], $where['number'], $request->uid());
  174. if ($res) return app('json')->successful();
  175. else return app('json')->fail('修改失败');
  176. }
  177. /**
  178. * 购物车 统计 数量 价格
  179. * @param Request $request
  180. * @return mixed
  181. */
  182. public function count(Request $request)
  183. {
  184. [$numType, $store_id] = $request->postMore([
  185. ['numType', true],//购物车编号
  186. ['store_id', 0]
  187. ], true);
  188. $uid = (int)$request->uid();
  189. return app('json')->success('ok', $this->services->getUserCartCount($uid, $numType, (int)$store_id));
  190. }
  191. /**
  192. * 购物车重选
  193. * @param Request $request
  194. * @return mixed
  195. */
  196. public function reChange(Request $request)
  197. {
  198. [$cart_id, $product_id, $unique] = $request->postMore([
  199. ['cart_id', 0],
  200. ['product_id', 0],
  201. ['unique', '']
  202. ], true);
  203. $this->services->modifyCart($cart_id, $product_id, $unique);
  204. return app('json')->success('重选成功');
  205. }
  206. /**
  207. * 计算用户购物车商品(优惠活动、最优优惠券)
  208. * @param Request $request
  209. * @return mixed
  210. */
  211. public function computeCart(Request $request)
  212. {
  213. [$cartId, $new, $addressId, $shipping_type, $storeId] = $request->postMore([
  214. 'cartId',
  215. 'new',
  216. ['addressId', 0],
  217. ['shipping_type', -1],
  218. ['store_id', 0],
  219. ['delivery_type', 1],
  220. ], true);
  221. if (!is_string($cartId) || !$cartId) {
  222. $result = ['promotions' => [], 'coupon' => [], 'deduction' => []];
  223. } else {
  224. $user = $request->user()->toArray();
  225. $result = $this->services->computeUserCart($user, $cartId, !!$new, (int)$addressId, (int)$shipping_type, (int)$storeId);
  226. }
  227. return app('json')->success($result);
  228. }
  229. }