StoreCartServices.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. declare (strict_types=1);
  12. namespace app\services\order;
  13. use app\services\activity\advance\StoreAdvanceServices;
  14. use app\services\BaseServices;
  15. use app\dao\order\StoreCartDao;
  16. use app\services\activity\coupon\StoreCouponIssueServices;
  17. use app\services\activity\coupon\StoreCouponIssueUserServices;
  18. use app\services\shipping\ShippingTemplatesNoDeliveryServices;
  19. use app\services\system\SystemUserLevelServices;
  20. use app\services\user\member\MemberCardServices;
  21. use app\services\user\UserServices;
  22. use app\jobs\ProductLogJob;
  23. use crmeb\services\CacheService;
  24. use think\exception\ValidateException;
  25. use app\services\activity\seckill\StoreSeckillServices;
  26. use app\services\activity\bargain\StoreBargainServices;
  27. use app\services\activity\combination\StoreCombinationServices;
  28. use app\services\product\product\StoreProductServices;
  29. use app\services\product\sku\StoreProductAttrValueServices;
  30. /**
  31. *
  32. * Class StoreCartServices
  33. * @package app\services\order
  34. * @method updateCartStatus($cartIds) 修改购物车状态
  35. * @method getUserCartNum(int $uid, string $type, int $numType) 购物车数量
  36. * @method deleteCartStatus(array $cartIds) 修改购物车状态
  37. * @method array productIdByCartNum(array $ids, int $uid) 根据商品id获取购物车数量
  38. * @method getCartList(array $where, ?int $page = 0, ?int $limit = 0, ?array $with = []) 获取用户购物车
  39. * @method getSum($where, $field) 求和
  40. * @method getProductTrend($time, $timeType, $str) 购物车趋势
  41. */
  42. class StoreCartServices extends BaseServices
  43. {
  44. /**
  45. * StoreCartServices constructor.
  46. * @param StoreCartDao $dao
  47. */
  48. public function __construct(StoreCartDao $dao)
  49. {
  50. $this->dao = $dao;
  51. }
  52. /**
  53. * 获取某个用户下的购物车数量
  54. * @param array $unique
  55. * @param int $productId
  56. * @param int $uid
  57. * @return array
  58. */
  59. public function getUserCartNums(array $unique, int $productId, int $uid)
  60. {
  61. $where['is_pay'] = 0;
  62. $where['is_del'] = 0;
  63. $where['is_new'] = 0;
  64. $where['product_id'] = $productId;
  65. $where['uid'] = $uid;
  66. return $this->dao->getUserCartNums($where, $unique);
  67. }
  68. /**
  69. * 获取用户下的购物车列表
  70. * @param $uid
  71. * @param string $cartIds
  72. * @param bool $new
  73. * @param array $addr
  74. * @return array
  75. * @throws \Psr\SimpleCache\InvalidArgumentException
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getUserProductCartListV1($uid, $cartIds = '', bool $new, $addr = [], int $shipping_type = 1)
  81. {
  82. if ($new) {
  83. $cartIds = explode(',', $cartIds);
  84. $cartInfo = [];
  85. $redis = CacheService::redisHandler();
  86. foreach ($cartIds as $key) {
  87. $info = $redis->get($key);
  88. if ($info) {
  89. $cartInfo[] = $info;
  90. }
  91. }
  92. } else {
  93. $cartInfo = $this->dao->getCartList(['uid' => $uid, 'status' => 1, 'id' => $cartIds], 0, 0, ['productInfo', 'attrInfo']);
  94. }
  95. if (!$cartInfo) {
  96. throw new ValidateException('获取购物车信息失败');
  97. }
  98. [$cartInfo, $valid, $invalid] = $this->handleCartList($uid, $cartInfo, $addr, $shipping_type);
  99. $seckillIds = array_unique(array_column($cartInfo, 'seckill_id'));
  100. $bargainIds = array_unique(array_column($cartInfo, 'bargain_id'));
  101. $combinationId = array_unique(array_column($cartInfo, 'combination_id'));
  102. $advanceId = array_unique(array_column($cartInfo, 'advance_id'));
  103. $deduction = ['seckill_id' => $seckillIds[0] ?? 0, 'bargain_id' => $bargainIds[0] ?? 0, 'combination_id' => $combinationId[0] ?? 0, 'advance_id' => $advanceId[0] ?? 0];
  104. return ['cartInfo' => $cartInfo, 'valid' => $valid, 'invalid' => $invalid, 'deduction' => $deduction];
  105. }
  106. /**
  107. * 使用雪花算法生成订单ID
  108. * @return string
  109. * @throws \Exception
  110. */
  111. public function getCartId($prefix)
  112. {
  113. $snowflake = new \Godruoyi\Snowflake\Snowflake();
  114. //32位
  115. if (PHP_INT_SIZE == 4) {
  116. $id = abs((int)$snowflake->id());
  117. } else {
  118. $id = $snowflake->setStartTimeStamp(strtotime('2020-06-05') * 1000)->id();
  119. }
  120. return $prefix . $id;
  121. }
  122. /**
  123. * 验证库存
  124. * @param int $uid
  125. * @param int $cartNum
  126. * @param string $unique
  127. * @param int $type
  128. * @param $productId
  129. * @param int $seckillId
  130. * @param int $bargainId
  131. * @param int $combinationId
  132. * @return array
  133. * @throws \Psr\SimpleCache\InvalidArgumentException
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function checkProductStock(int $uid, int $cartNum, string $unique, int $type = 0, $productId, int $seckillId, int $bargainId, int $combinationId, int $advanceId)
  139. {
  140. /** @var StoreProductAttrValueServices $attrValueServices */
  141. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  142. switch ($type) {
  143. case 0://普通
  144. if ($unique == '') {
  145. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  146. }
  147. /** @var StoreProductServices $productServices */
  148. $productServices = app()->make(StoreProductServices::class);
  149. $productInfo = $productServices->isValidProduct($productId);
  150. if (!$productInfo) {
  151. throw new ValidateException('该商品已下架或删除');
  152. }
  153. $attrInfo = $attrValueServices->getOne(['unique' => $unique, 'type' => 0]);
  154. if (!$unique || !$attrInfo || $attrInfo['product_id'] != $productId) {
  155. throw new ValidateException('请选择有效的商品属性');
  156. }
  157. $nowStock = $attrInfo['stock'];//现有库存
  158. if ($cartNum > $nowStock) {
  159. throw new ValidateException('该商品库存不足' . $cartNum);
  160. }
  161. if ($productInfo['is_virtual'] == 1 && $productInfo['virtual_type'] == 2 && $attrInfo['coupon_id']) {
  162. /** @var StoreCouponIssueServices $issueCoupon */
  163. $issueCoupon = app()->make(StoreCouponIssueServices::class);
  164. if (!$issueCoupon->getCount(['id' => $attrInfo['coupon_id'], 'status' => 1, 'is_del' => 0])) {
  165. throw new ValidateException('您要购买的优惠券已失效,无法购买');
  166. }
  167. /** @var StoreCouponIssueUserServices $issueUserCoupon */
  168. $issueUserCoupon = app()->make(StoreCouponIssueUserServices::class);
  169. if ($issueUserCoupon->getCount(['uid' => $uid, 'issue_coupon_id' => $attrInfo['coupon_id']])) {
  170. throw new ValidateException('您已获取过该优惠券,无法再次购买');
  171. }
  172. }
  173. $stockNum = $this->dao->value(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid, 'status' => 1], 'cart_num') ?: 0;
  174. if ($nowStock < ($cartNum + $stockNum)) {
  175. $surplusStock = $nowStock - $cartNum;//剩余库存
  176. if ($surplusStock < $stockNum) {
  177. $this->dao->update(['product_id' => $productId, 'product_attr_unique' => $unique, 'uid' => $uid, 'status' => 1], ['cart_num' => $surplusStock]);
  178. }
  179. }
  180. break;
  181. case 1://秒杀
  182. /** @var StoreSeckillServices $seckillService */
  183. $seckillService = app()->make(StoreSeckillServices::class);
  184. [$attrInfo, $unique, $productInfo] = $seckillService->checkSeckillStock($uid, $seckillId, $cartNum, $unique);
  185. break;
  186. case 2://砍价
  187. /** @var StoreBargainServices $bargainService */
  188. $bargainService = app()->make(StoreBargainServices::class);
  189. [$attrInfo, $unique, $productInfo, $bargainUserInfo] = $bargainService->checkBargainStock($uid, $bargainId, $cartNum, $unique);
  190. break;
  191. case 3://拼团
  192. /** @var StoreCombinationServices $combinationService */
  193. $combinationService = app()->make(StoreCombinationServices::class);
  194. [$attrInfo, $unique, $productInfo] = $combinationService->checkCombinationStock($uid, $combinationId, $cartNum, $unique);
  195. break;
  196. case 6://预售
  197. /** @var StoreAdvanceServices $advanceService */
  198. $advanceService = app()->make(StoreAdvanceServices::class);
  199. [$attrInfo, $unique, $productInfo] = $advanceService->checkAdvanceStock($uid, $advanceId, $cartNum, $unique);
  200. break;
  201. default:
  202. throw new ValidateException('请刷新后重试');
  203. }
  204. if ($type && $type != 6) {
  205. //根商品规格库存
  206. $product_stock = $attrValueServices->value(['product_id' => $productInfo['product_id'], 'suk' => $attrInfo['suk'], 'type' => 0], 'stock');
  207. if ($product_stock < $cartNum) {
  208. throw new ValidateException('商品库存不足' . $cartNum);
  209. }
  210. if ($type != 5 && !CacheService::checkStock($unique, (int)$cartNum, $type)) {
  211. throw new ValidateException('商品库存不足' . $cartNum . ',无法购买请选择其他商品!');
  212. }
  213. }
  214. return [$attrInfo, $unique, $bargainUserInfo['bargain_price_min'] ?? 0, $cartNum, $productInfo];
  215. }
  216. /**
  217. * 添加购物车
  218. * @param int $uid 用户UID
  219. * @param int $product_id 商品ID
  220. * @param int $cart_num 商品数量
  221. * @param string $product_attr_unique 商品SKU
  222. * @param string $type 添加购物车类型
  223. * @param bool $new true = 立即购买,false = 加入购物车
  224. * @param int $combination_id 拼团商品ID
  225. * @param int $seckill_id 秒杀商品ID
  226. * @param int $bargain_id 砍价商品ID
  227. * @return mixed|string
  228. * @throws \Psr\SimpleCache\InvalidArgumentException
  229. * @throws \think\db\exception\DataNotFoundException
  230. * @throws \think\db\exception\DbException
  231. * @throws \think\db\exception\ModelNotFoundException
  232. */
  233. public function setCart(int $uid, int $product_id, int $cart_num = 1, string $product_attr_unique = '', int $type = 0, bool $new = true, int $combination_id = 0, int $seckill_id = 0, int $bargain_id = 0, int $advance_id = 0)
  234. {
  235. if ($cart_num < 1) $cart_num = 1;
  236. //检测库存限量
  237. [$attrInfo, $product_attr_unique, $bargainPriceMin, $cart_num, $productInfo] = $this->checkProductStock($uid, $cart_num, $product_attr_unique, $type, $product_id, $seckill_id, $bargain_id, $combination_id, $advance_id);
  238. if ($new) {
  239. /** @var StoreOrderCreateServices $storeOrderCreateService */
  240. $storeOrderCreateService = app()->make(StoreOrderCreateServices::class);
  241. $key = $storeOrderCreateService->getNewOrderId((string)$uid);
  242. $info['id'] = $key;
  243. $info['type'] = $type;
  244. $info['seckill_id'] = $seckill_id;
  245. $info['bargain_id'] = $bargain_id;
  246. $info['combination_id'] = $combination_id;
  247. $info['advance_id'] = $advance_id;
  248. $info['product_id'] = $product_id;
  249. $info['product_attr_unique'] = $product_attr_unique;
  250. $info['cart_num'] = $cart_num;
  251. $info['productInfo'] = $productInfo ? $productInfo->toArray() : [];
  252. $info['productInfo']['attrInfo'] = $attrInfo->toArray();
  253. $info['sum_price'] = $info['productInfo']['attrInfo']['price'];
  254. //砍价
  255. if ($bargain_id) {
  256. $info['truePrice'] = $bargainPriceMin;
  257. $info['productInfo']['attrInfo']['price'] = $bargainPriceMin;
  258. } else {
  259. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? $info['productInfo']['price'] ?? 0;
  260. }
  261. //拼团砍价秒杀不参与会员价
  262. if ($bargain_id || $combination_id || $seckill_id || $advance_id) {
  263. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? 0;
  264. $info['vip_truePrice'] = 0;
  265. }
  266. $info['trueStock'] = $info['productInfo']['attrInfo']['stock'];
  267. $info['costPrice'] = $info['productInfo']['attrInfo']['cost'];
  268. try {
  269. CacheService::redisHandler()->set($key, $info, 3600);
  270. } catch (\Throwable $e) {
  271. throw new ValidateException($e->getMessage());
  272. }
  273. return $key;
  274. } else {//加入购物车记录
  275. ProductLogJob::dispatch(['cart', ['uid' => $uid, 'product_id' => $product_id, 'cart_num' => $cart_num]]);
  276. $cart = $this->dao->getOne(['type' => $type, 'uid' => $uid, 'product_id' => $product_id, 'product_attr_unique' => $product_attr_unique, 'is_del' => 0, 'is_new' => 0, 'is_pay' => 0, 'status' => 1]);
  277. if ($cart) {
  278. $cart->cart_num = $cart_num + $cart->cart_num;
  279. $cart->add_time = time();
  280. $cart->save();
  281. return $cart->id;
  282. } else {
  283. $add_time = time();
  284. return $this->dao->save(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'type', 'add_time'))->id;
  285. }
  286. }
  287. }
  288. /**移除购物车商品
  289. * @param int $uid
  290. * @param array $ids
  291. * @return StoreCartDao|bool
  292. */
  293. public function removeUserCart(int $uid, array $ids)
  294. {
  295. if (!$uid || !$ids) return false;
  296. return $this->dao->removeUserCart($uid, $ids);
  297. }
  298. /**购物车 修改商品数量
  299. * @param $id
  300. * @param $number
  301. * @param $uid
  302. * @return bool|\crmeb\basic\BaseModel
  303. * @throws \think\db\exception\DataNotFoundException
  304. * @throws \think\db\exception\DbException
  305. * @throws \think\db\exception\ModelNotFoundException
  306. */
  307. public function changeUserCartNum($id, $number, $uid)
  308. {
  309. if (!$id || !$number || !$uid) return false;
  310. $where = ['uid' => $uid, 'id' => $id];
  311. $carInfo = $this->dao->getOne($where, 'product_id,combination_id,seckill_id,bargain_id,product_attr_unique,cart_num');
  312. /** @var StoreProductServices $StoreProduct */
  313. $StoreProduct = app()->make(StoreProductServices::class);
  314. $stock = $StoreProduct->getProductStock($carInfo->product_id, $carInfo->product_attr_unique);
  315. if (!$stock) throw new ValidateException('暂无库存');
  316. if (!$number) throw new ValidateException('库存错误');
  317. if ($stock < $number) throw new ValidateException('库存不足' . $number);
  318. if ($carInfo->cart_num == $number) return true;
  319. return $this->dao->changeUserCartNum(['uid' => $uid, 'id' => $id], (int)$number);
  320. }
  321. /**
  322. * 修改购物车状态
  323. * @param int $productId
  324. * @param int $status 0 商品下架
  325. */
  326. public function changeStatus(int $productId, $status = 0)
  327. {
  328. $this->dao->update($productId, ['status' => $status], 'product_id');
  329. }
  330. /**
  331. * 获取购物车列表
  332. * @param int $uid
  333. * @param int $status
  334. * @return array
  335. * @throws \think\db\exception\DataNotFoundException
  336. * @throws \think\db\exception\DbException
  337. * @throws \think\db\exception\ModelNotFoundException
  338. */
  339. public function getUserCartList(int $uid, int $status, string $cartIds = '')
  340. {
  341. [$page, $limit] = $this->getPageValue();
  342. $list = $this->dao->getCartList(['uid' => $uid, 'status' => $status, 'id' => $cartIds], $page, $limit, ['productInfo', 'attrInfo']);
  343. [$list, $valid, $invalid] = $this->handleCartList($uid, $list);
  344. $seckillIds = array_unique(array_column($list, 'seckill_id'));
  345. $bargainIds = array_unique(array_column($list, 'bargain_id'));
  346. $combinationId = array_unique(array_column($list, 'combination_id'));
  347. $discountId = array_unique(array_column($list, 'discount_id'));
  348. $deduction = ['seckill_id' => $seckillIds[0] ?? 0, 'bargain_id' => $bargainIds[0] ?? 0, 'combination_id' => $combinationId[0] ?? 0, 'discount_id' => $discountId[0] ?? 0];
  349. if ($status == 1) {
  350. return ['valid' => $list, 'invalid' => [], 'deduction' => $deduction];
  351. } else {
  352. return ['valid' => [], 'invalid' => $list, 'deduction' => $deduction];
  353. }
  354. }
  355. /**
  356. * 购物车重选
  357. * @param int $cart_id
  358. * @param int $product_id
  359. * @param string $unique
  360. */
  361. public function modifyCart(int $cart_id, int $product_id, string $unique)
  362. {
  363. /** @var StoreProductAttrValueServices $attrService */
  364. $attrService = app()->make(StoreProductAttrValueServices::class);
  365. $stock = $attrService->value(['product_id' => $product_id, 'unique' => $unique, 'type' => 0], 'stock');
  366. if ($stock > 0) {
  367. $this->dao->update($cart_id, ['product_attr_unique' => $unique, 'cart_num' => 1]);
  368. } else {
  369. throw new ValidateException('选择的规格库存不足');
  370. }
  371. }
  372. /**
  373. * 重选购物车
  374. * @param $id
  375. * @param $uid
  376. * @param $productId
  377. * @param $unique
  378. * @param $num
  379. * @throws \think\db\exception\DataNotFoundException
  380. * @throws \think\db\exception\DbException
  381. * @throws \think\db\exception\ModelNotFoundException
  382. */
  383. public function resetCart($id, $uid, $productId, $unique, $num)
  384. {
  385. $res = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  386. if ($res) {
  387. $res->cart_num = $res->cart_num + $num;
  388. $res->save();
  389. $this->dao->delete($id);
  390. } else {
  391. $this->dao->update($id, ['product_attr_unique' => $unique, 'cart_num' => $num]);
  392. }
  393. }
  394. /**
  395. * 首页加入购物车
  396. * @param $uid
  397. * @param $productId
  398. * @param $num
  399. * @param $unique
  400. * @param $type
  401. * @return mixed
  402. * @throws \think\db\exception\DataNotFoundException
  403. * @throws \think\db\exception\DbException
  404. * @throws \think\db\exception\ModelNotFoundException
  405. */
  406. public function setCartNum($uid, $productId, $num, $unique, $type)
  407. {
  408. /** @var StoreProductAttrValueServices $attrValueServices */
  409. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  410. if ($unique == '') {
  411. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  412. }
  413. /** @var StoreProductServices $productServices */
  414. $productServices = app()->make(StoreProductServices::class);
  415. if (!$productServices->isValidProduct((int)$productId)) {
  416. throw new ValidateException('该商品已下架或删除');
  417. }
  418. if (!($unique && $attrValueServices->getAttrvalueCount($productId, $unique, 0))) {
  419. throw new ValidateException('请选择有效的商品属性');
  420. }
  421. if ($productServices->getProductStock((int)$productId, $unique) < $num) {
  422. throw new ValidateException('该商品库存不足' . $num);
  423. }
  424. $cart = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  425. if ($cart) {
  426. if ($type == -1) {
  427. $cart->cart_num = $num;
  428. } elseif ($type == 0) {
  429. $cart->cart_num = $cart->cart_num - $num;
  430. } elseif ($type == 1) {
  431. $cart->cart_num = $cart->cart_num + $num;
  432. }
  433. if ($cart->cart_num === 0) {
  434. return $this->dao->delete($cart->id);
  435. } else {
  436. $cart->add_time = time();
  437. $cart->save();
  438. return $cart->id;
  439. }
  440. } else {
  441. $data = [
  442. 'uid' => $uid,
  443. 'product_id' => $productId,
  444. 'cart_num' => $num,
  445. 'product_attr_unique' => $unique,
  446. 'type' => 0,
  447. 'add_time' => time()
  448. ];
  449. return $this->dao->save($data)->id;
  450. }
  451. }
  452. /**
  453. * 获取用户购物车数量 ids 统计金额
  454. * @param int $uid
  455. * @param string $numType
  456. * @throws \think\db\exception\DataNotFoundException
  457. * @throws \think\db\exception\DbException
  458. * @throws \think\db\exception\ModelNotFoundException
  459. */
  460. public function getUserCartCount(int $uid, string $numType)
  461. {
  462. $count = 0;
  463. $ids = [];
  464. $sum_price = 0;
  465. $cartList = $this->dao->getUserCartList($uid, '*', ['productInfo', 'attrInfo']);
  466. if ($cartList) {
  467. /** @var StoreProductServices $productServices */
  468. $productServices = app()->make(StoreProductServices::class);
  469. /** @var MemberCardServices $memberCardService */
  470. $memberCardService = app()->make(MemberCardServices::class);
  471. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  472. /** @var UserServices $user */
  473. $user = app()->make(UserServices::class);
  474. $userInfo = $user->getUserInfo($uid);
  475. $discount = 100;
  476. if (sys_config('member_func_status', 1)) {
  477. /** @var SystemUserLevelServices $systemLevel */
  478. $systemLevel = app()->make(SystemUserLevelServices::class);
  479. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  480. }
  481. foreach ($cartList as &$item) {
  482. $productInfo = $item['productInfo'];
  483. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  484. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  485. $item['truePrice'] = $truePrice;
  486. $item['price_type'] = $type;
  487. } else {
  488. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($item['productInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $item['productInfo']['vip_price'] ?? 0, $item['productInfo']['is_vip'] ?? 0, true);
  489. $item['truePrice'] = $truePrice;
  490. $item['price_type'] = $type;
  491. }
  492. $sum_price = bcadd((string)$sum_price, (string)bcmul((string)$item['cart_num'], (string)$item['truePrice'], 4), 2);
  493. }
  494. $ids = array_column($cartList, 'id');
  495. if ($numType) {
  496. $count = count($cartList);
  497. } else {
  498. $count = array_sum(array_column($cartList, 'cart_num'));
  499. }
  500. }
  501. return compact('count', 'ids', 'sum_price');
  502. }
  503. /**
  504. * 处理购物车数据
  505. * @param int $uid
  506. * @param array $cartList
  507. * @param array $addr
  508. * @return array
  509. */
  510. public function handleCartList(int $uid, array $cartList, array $addr = [], int $shipping_type = 1)
  511. {
  512. if (!$cartList) {
  513. return [$cartList, [], []];
  514. }
  515. /** @var StoreProductServices $productServices */
  516. $productServices = app()->make(StoreProductServices::class);
  517. /** @var MemberCardServices $memberCardService */
  518. $memberCardService = app()->make(MemberCardServices::class);
  519. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  520. $tempIds = [];
  521. $userInfo = [];
  522. $discount = 100;
  523. if ($uid) {
  524. /** @var UserServices $user */
  525. $user = app()->make(UserServices::class);
  526. $userInfo = $user->getUserInfo($uid);
  527. //用户等级是否开启
  528. if (sys_config('member_func_status', 1)) {
  529. /** @var SystemUserLevelServices $systemLevel */
  530. $systemLevel = app()->make(SystemUserLevelServices::class);
  531. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  532. }
  533. }
  534. //不送达运费模板
  535. if ($shipping_type == 1 && $addr) {
  536. $cityId = (int)($addr['city_id'] ?? 0);
  537. if ($cityId) {
  538. foreach ($cartList as $item) {
  539. $tempIds[] = $item['productInfo']['temp_id'];
  540. }
  541. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  542. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  543. $tempIds = $noDeliveryServices->isNoDelivery(array_unique($tempIds), $cityId);
  544. }
  545. }
  546. $valid = $invalid = [];
  547. foreach ($cartList as &$item) {
  548. $item['productInfo']['express_delivery'] = false;
  549. $item['productInfo']['store_mention'] = false;
  550. if (isset($item['productInfo']['logistics'])) {
  551. if (in_array(1, explode(',', $item['productInfo']['logistics']))) {
  552. $item['productInfo']['express_delivery'] = true;
  553. }
  554. if (in_array(2, explode(',', $item['productInfo']['logistics']))) {
  555. $item['productInfo']['store_mention'] = true;
  556. }
  557. }
  558. if (isset($item['attrInfo']) && $item['attrInfo'] && (!isset($item['productInfo']['attrInfo']) || !$item['productInfo']['attrInfo'])) {
  559. $item['productInfo']['attrInfo'] = $item['attrInfo'] ?? [];
  560. }
  561. $item['attrStatus'] = isset($item['productInfo']['attrInfo']['stock']) && $item['productInfo']['attrInfo']['stock'];
  562. $item['productInfo']['attrInfo']['image'] = $item['productInfo']['attrInfo']['image'] ?? $item['productInfo']['image'] ?? '';
  563. $item['productInfo']['attrInfo']['suk'] = $item['productInfo']['attrInfo']['suk'] ?? '已失效';
  564. if (isset($item['productInfo']['attrInfo'])) {
  565. $item['productInfo']['attrInfo'] = get_thumb_water($item['productInfo']['attrInfo']);
  566. }
  567. $item['productInfo'] = get_thumb_water($item['productInfo']);
  568. $productInfo = $item['productInfo'];
  569. $item['vip_truePrice'] = 0;
  570. $is_activity = $item['seckill_id'] || $item['bargain_id'] || $item['combination_id'] || $item['advance_id'];
  571. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  572. $item['costPrice'] = $productInfo['attrInfo']['cost'] ?? 0;
  573. $item['trueStock'] = $productInfo['attrInfo']['stock'] ?? 0;
  574. $item['truePrice'] = $productInfo['attrInfo']['price'] ?? 0;
  575. $item['sum_price'] = $productInfo['attrInfo']['price'] ?? 0;
  576. if (!$is_activity) {
  577. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  578. $item['truePrice'] = $truePrice;
  579. $item['vip_truePrice'] = $vip_truePrice;
  580. $item['price_type'] = $type;
  581. }
  582. } else {
  583. $item['costPrice'] = $item['productInfo']['cost'] ?? 0;
  584. $item['trueStock'] = $item['productInfo']['stock'] ?? 0;
  585. $item['truePrice'] = $item['productInfo']['price'] ?? 0;
  586. $item['sum_price'] = $item['productInfo']['price'] ?? 0;
  587. if (!$is_activity) {
  588. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($item['productInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $item['productInfo']['vip_price'] ?? 0, $item['productInfo']['is_vip'] ?? 0, true);
  589. $item['truePrice'] = $truePrice;
  590. $item['vip_truePrice'] = $vip_truePrice;
  591. $item['price_type'] = $type;
  592. }
  593. }
  594. if (isset($item['status']) && $item['status'] == 0) {
  595. $item['is_valid'] = 0;
  596. $invalid[] = $item;
  597. } else {
  598. switch ($shipping_type) {
  599. case 1:
  600. //不送达
  601. if (in_array($item['productInfo']['temp_id'], $tempIds)) {
  602. $item['is_valid'] = 0;
  603. $invalid[] = $item;
  604. } else {
  605. $item['is_valid'] = 1;
  606. $valid[] = $item;
  607. }
  608. break;
  609. case 2:
  610. //不支持到店自提
  611. if (isset($item['productInfo']['logistics']) && $item['productInfo']['logistics'] && !in_array(2, explode(',', $item['productInfo']['logistics']))) {
  612. $item['is_valid'] = 0;
  613. $invalid[] = $item;
  614. } else {
  615. $item['is_valid'] = 1;
  616. $valid[] = $item;
  617. }
  618. break;
  619. }
  620. }
  621. unset($item['attrInfo']);
  622. }
  623. return [$cartList, $valid, $invalid];
  624. }
  625. }