StoreCartServices.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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['attrInfo'] = $attrInfo->toArray();
  254. $info['sum_price'] = $info['productInfo']['attrInfo']['price'];
  255. //砍价
  256. if ($bargain_id) {
  257. $info['truePrice'] = $bargainPriceMin;
  258. $info['productInfo']['attrInfo']['price'] = $bargainPriceMin;
  259. } else {
  260. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? $info['productInfo']['price'] ?? 0;
  261. }
  262. //拼团砍价秒杀不参与会员价
  263. if ($bargain_id || $combination_id || $seckill_id || $advance_id) {
  264. $info['truePrice'] = $info['productInfo']['attrInfo']['price'] ?? 0;
  265. $info['vip_truePrice'] = 0;
  266. }
  267. $info['trueStock'] = $info['productInfo']['attrInfo']['stock'];
  268. $info['costPrice'] = $info['productInfo']['attrInfo']['cost'];
  269. try {
  270. CacheService::redisHandler()->set($key, $info, 3600);
  271. } catch (\Throwable $e) {
  272. throw new ValidateException($e->getMessage());
  273. }
  274. return $key;
  275. } else {//加入购物车记录
  276. ProductLogJob::dispatch(['cart', ['uid' => $uid, 'product_id' => $product_id, 'cart_num' => $cart_num]]);
  277. $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]);
  278. if ($cart) {
  279. $cart->cart_num = $cart_num + $cart->cart_num;
  280. $cart->add_time = time();
  281. $cart->save();
  282. return $cart->id;
  283. } else {
  284. $add_time = time();
  285. return $this->dao->save(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'type', 'add_time'))->id;
  286. }
  287. }
  288. }
  289. /**移除购物车商品
  290. * @param int $uid
  291. * @param array $ids
  292. * @return StoreCartDao|bool
  293. */
  294. public function removeUserCart(int $uid, array $ids)
  295. {
  296. if (!$uid || !$ids) return false;
  297. return $this->dao->removeUserCart($uid, $ids);
  298. }
  299. /**购物车 修改商品数量
  300. * @param $id
  301. * @param $number
  302. * @param $uid
  303. * @return bool|\crmeb\basic\BaseModel
  304. * @throws \think\db\exception\DataNotFoundException
  305. * @throws \think\db\exception\DbException
  306. * @throws \think\db\exception\ModelNotFoundException
  307. */
  308. public function changeUserCartNum($id, $number, $uid)
  309. {
  310. if (!$id || !$number || !$uid) return false;
  311. $where = ['uid' => $uid, 'id' => $id];
  312. $carInfo = $this->dao->getOne($where, 'product_id,combination_id,seckill_id,bargain_id,product_attr_unique,cart_num');
  313. /** @var StoreProductServices $StoreProduct */
  314. $StoreProduct = app()->make(StoreProductServices::class);
  315. $stock = $StoreProduct->getProductStock($carInfo->product_id, $carInfo->product_attr_unique);
  316. if (!$stock) throw new ValidateException('暂无库存');
  317. if (!$number) throw new ValidateException('库存错误');
  318. if ($stock < $number) throw new ValidateException('库存不足' . $number);
  319. if ($carInfo->cart_num == $number) return true;
  320. return $this->dao->changeUserCartNum(['uid' => $uid, 'id' => $id], (int)$number);
  321. }
  322. /**
  323. * 修改购物车状态
  324. * @param int $productId
  325. * @param int $status 0 商品下架
  326. */
  327. public function changeStatus(int $productId, $status = 0)
  328. {
  329. $this->dao->update($productId, ['status' => $status], 'product_id');
  330. }
  331. /**
  332. * 获取购物车列表
  333. * @param int $uid
  334. * @param int $status
  335. * @return array
  336. * @throws \think\db\exception\DataNotFoundException
  337. * @throws \think\db\exception\DbException
  338. * @throws \think\db\exception\ModelNotFoundException
  339. */
  340. public function getUserCartList(int $uid, int $status, string $cartIds = '')
  341. {
  342. [$page, $limit] = $this->getPageValue();
  343. $list = $this->dao->getCartList(['uid' => $uid, 'status' => $status, 'id' => $cartIds], $page, $limit, ['productInfo', 'attrInfo']);
  344. [$list, $valid, $invalid] = $this->handleCartList($uid, $list);
  345. $seckillIds = array_unique(array_column($list, 'seckill_id'));
  346. $bargainIds = array_unique(array_column($list, 'bargain_id'));
  347. $combinationId = array_unique(array_column($list, 'combination_id'));
  348. $discountId = array_unique(array_column($list, 'discount_id'));
  349. $deduction = ['seckill_id' => $seckillIds[0] ?? 0, 'bargain_id' => $bargainIds[0] ?? 0, 'combination_id' => $combinationId[0] ?? 0, 'discount_id' => $discountId[0] ?? 0];
  350. if ($status == 1) {
  351. return ['valid' => $list, 'invalid' => [], 'deduction' => $deduction];
  352. } else {
  353. return ['valid' => [], 'invalid' => $list, 'deduction' => $deduction];
  354. }
  355. }
  356. /**
  357. * 购物车重选
  358. * @param int $cart_id
  359. * @param int $product_id
  360. * @param string $unique
  361. */
  362. public function modifyCart(int $cart_id, int $product_id, string $unique)
  363. {
  364. /** @var StoreProductAttrValueServices $attrService */
  365. $attrService = app()->make(StoreProductAttrValueServices::class);
  366. $stock = $attrService->value(['product_id' => $product_id, 'unique' => $unique, 'type' => 0], 'stock');
  367. if ($stock > 0) {
  368. $this->dao->update($cart_id, ['product_attr_unique' => $unique, 'cart_num' => 1]);
  369. } else {
  370. throw new ValidateException('选择的规格库存不足');
  371. }
  372. }
  373. /**
  374. * 重选购物车
  375. * @param $id
  376. * @param $uid
  377. * @param $productId
  378. * @param $unique
  379. * @param $num
  380. * @throws \think\db\exception\DataNotFoundException
  381. * @throws \think\db\exception\DbException
  382. * @throws \think\db\exception\ModelNotFoundException
  383. */
  384. public function resetCart($id, $uid, $productId, $unique, $num)
  385. {
  386. $res = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  387. if ($res) {
  388. $res->cart_num = $res->cart_num + $num;
  389. $res->save();
  390. $this->dao->delete($id);
  391. } else {
  392. $this->dao->update($id, ['product_attr_unique' => $unique, 'cart_num' => $num]);
  393. }
  394. }
  395. /**
  396. * 首页加入购物车
  397. * @param $uid
  398. * @param $productId
  399. * @param $num
  400. * @param $unique
  401. * @param $type
  402. * @return mixed
  403. * @throws \think\db\exception\DataNotFoundException
  404. * @throws \think\db\exception\DbException
  405. * @throws \think\db\exception\ModelNotFoundException
  406. */
  407. public function setCartNum($uid, $productId, $num, $unique, $type)
  408. {
  409. /** @var StoreProductAttrValueServices $attrValueServices */
  410. $attrValueServices = app()->make(StoreProductAttrValueServices::class);
  411. if ($unique == '') {
  412. $unique = $attrValueServices->value(['product_id' => $productId, 'type' => 0], 'unique');
  413. }
  414. /** @var StoreProductServices $productServices */
  415. $productServices = app()->make(StoreProductServices::class);
  416. if (!$productServices->isValidProduct((int)$productId)) {
  417. throw new ValidateException('该商品已下架或删除');
  418. }
  419. if (!($unique && $attrValueServices->getAttrvalueCount($productId, $unique, 0))) {
  420. throw new ValidateException('请选择有效的商品属性');
  421. }
  422. if ($productServices->getProductStock((int)$productId, $unique) < $num) {
  423. throw new ValidateException('该商品库存不足' . $num);
  424. }
  425. $cart = $this->dao->getOne(['uid' => $uid, 'product_id' => $productId, 'product_attr_unique' => $unique]);
  426. if ($cart) {
  427. if ($type == -1) {
  428. $cart->cart_num = $num;
  429. } elseif ($type == 0) {
  430. $cart->cart_num = $cart->cart_num - $num;
  431. } elseif ($type == 1) {
  432. $cart->cart_num = $cart->cart_num + $num;
  433. }
  434. if ($cart->cart_num === 0) {
  435. return $this->dao->delete($cart->id);
  436. } else {
  437. $cart->add_time = time();
  438. $cart->save();
  439. return $cart->id;
  440. }
  441. } else {
  442. $data = [
  443. 'uid' => $uid,
  444. 'product_id' => $productId,
  445. 'cart_num' => $num,
  446. 'product_attr_unique' => $unique,
  447. 'type' => 0,
  448. 'add_time' => time()
  449. ];
  450. return $this->dao->save($data)->id;
  451. }
  452. }
  453. /**
  454. * 获取用户购物车数量 ids 统计金额
  455. * @param int $uid
  456. * @param string $numType
  457. * @throws \think\db\exception\DataNotFoundException
  458. * @throws \think\db\exception\DbException
  459. * @throws \think\db\exception\ModelNotFoundException
  460. */
  461. public function getUserCartCount(int $uid, string $numType)
  462. {
  463. $count = 0;
  464. $ids = [];
  465. $sum_price = 0;
  466. $cartList = $this->dao->getUserCartList($uid, '*', ['productInfo', 'attrInfo']);
  467. if ($cartList) {
  468. /** @var StoreProductServices $productServices */
  469. $productServices = app()->make(StoreProductServices::class);
  470. /** @var MemberCardServices $memberCardService */
  471. $memberCardService = app()->make(MemberCardServices::class);
  472. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  473. /** @var UserServices $user */
  474. $user = app()->make(UserServices::class);
  475. $userInfo = $user->getUserInfo($uid);
  476. $discount = 100;
  477. if (sys_config('member_func_status', 1)) {
  478. /** @var SystemUserLevelServices $systemLevel */
  479. $systemLevel = app()->make(SystemUserLevelServices::class);
  480. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  481. }
  482. foreach ($cartList as &$item) {
  483. $productInfo = $item['productInfo'];
  484. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  485. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  486. $item['truePrice'] = $truePrice;
  487. $item['price_type'] = $type;
  488. } else {
  489. [$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);
  490. $item['truePrice'] = $truePrice;
  491. $item['price_type'] = $type;
  492. }
  493. $sum_price = bcadd((string)$sum_price, (string)bcmul((string)$item['cart_num'], (string)$item['truePrice'], 4), 2);
  494. }
  495. $ids = array_column($cartList, 'id');
  496. if ($numType) {
  497. $count = count($cartList);
  498. } else {
  499. $count = array_sum(array_column($cartList, 'cart_num'));
  500. }
  501. }
  502. return compact('count', 'ids', 'sum_price');
  503. }
  504. /**
  505. * 处理购物车数据
  506. * @param int $uid
  507. * @param array $cartList
  508. * @param array $addr
  509. * @return array
  510. */
  511. public function handleCartList(int $uid, array $cartList, array $addr = [], int $shipping_type = 1)
  512. {
  513. if (!$cartList) {
  514. return [$cartList, [], []];
  515. }
  516. /** @var StoreProductServices $productServices */
  517. $productServices = app()->make(StoreProductServices::class);
  518. /** @var MemberCardServices $memberCardService */
  519. $memberCardService = app()->make(MemberCardServices::class);
  520. $vipStatus = $memberCardService->isOpenMemberCard('vip_price', false);
  521. $tempIds = [];
  522. $userInfo = [];
  523. $discount = 100;
  524. if ($uid) {
  525. /** @var UserServices $user */
  526. $user = app()->make(UserServices::class);
  527. $userInfo = $user->getUserInfo($uid);
  528. //用户等级是否开启
  529. if (sys_config('member_func_status', 1)) {
  530. /** @var SystemUserLevelServices $systemLevel */
  531. $systemLevel = app()->make(SystemUserLevelServices::class);
  532. $discount = $systemLevel->value(['id' => $userInfo['level'], 'is_del' => 0, 'is_show' => 1], 'discount') ?: 100;
  533. }
  534. }
  535. //不送达运费模板
  536. if ($shipping_type == 1 && $addr) {
  537. $cityId = (int)($addr['city_id'] ?? 0);
  538. if ($cityId) {
  539. foreach ($cartList as $item) {
  540. $tempIds[] = $item['productInfo']['temp_id'];
  541. }
  542. /** @var ShippingTemplatesNoDeliveryServices $noDeliveryServices */
  543. $noDeliveryServices = app()->make(ShippingTemplatesNoDeliveryServices::class);
  544. $tempIds = $noDeliveryServices->isNoDelivery(array_unique($tempIds), $cityId);
  545. }
  546. }
  547. $valid = $invalid = [];
  548. foreach ($cartList as &$item) {
  549. $item['productInfo']['express_delivery'] = false;
  550. $item['productInfo']['store_mention'] = false;
  551. if (isset($item['productInfo']['logistics'])) {
  552. if (in_array(1, explode(',', $item['productInfo']['logistics']))) {
  553. $item['productInfo']['express_delivery'] = true;
  554. }
  555. if (in_array(2, explode(',', $item['productInfo']['logistics']))) {
  556. $item['productInfo']['store_mention'] = true;
  557. }
  558. }
  559. if (isset($item['attrInfo']) && $item['attrInfo'] && (!isset($item['productInfo']['attrInfo']) || !$item['productInfo']['attrInfo'])) {
  560. $item['productInfo']['attrInfo'] = $item['attrInfo'] ?? [];
  561. }
  562. $item['attrStatus'] = isset($item['productInfo']['attrInfo']['stock']) && $item['productInfo']['attrInfo']['stock'];
  563. $item['productInfo']['attrInfo']['image'] = $item['productInfo']['attrInfo']['image'] ?? $item['productInfo']['image'] ?? '';
  564. $item['productInfo']['attrInfo']['suk'] = $item['productInfo']['attrInfo']['suk'] ?? '已失效';
  565. if (isset($item['productInfo']['attrInfo'])) {
  566. $item['productInfo']['attrInfo'] = get_thumb_water($item['productInfo']['attrInfo']);
  567. }
  568. $item['productInfo'] = get_thumb_water($item['productInfo']);
  569. $productInfo = $item['productInfo'];
  570. $item['vip_truePrice'] = 0;
  571. $is_activity = $item['seckill_id'] || $item['bargain_id'] || $item['combination_id'] || $item['advance_id'];
  572. if (isset($productInfo['attrInfo']['product_id']) && $item['product_attr_unique']) {
  573. $item['costPrice'] = $productInfo['attrInfo']['cost'] ?? 0;
  574. $item['trueStock'] = $productInfo['attrInfo']['stock'] ?? 0;
  575. $item['truePrice'] = $productInfo['attrInfo']['price'] ?? 0;
  576. $item['sum_price'] = $productInfo['attrInfo']['price'] ?? 0;
  577. if (!$is_activity) {
  578. [$truePrice, $vip_truePrice, $type] = $productServices->setLevelPrice($productInfo['attrInfo']['price'] ?? 0, $uid, $userInfo, $vipStatus, $discount, $productInfo['attrInfo']['vip_price'] ?? 0, $productInfo['is_vip'] ?? 0, true);
  579. $item['truePrice'] = $truePrice;
  580. $item['vip_truePrice'] = $vip_truePrice;
  581. $item['price_type'] = $type;
  582. }
  583. } else {
  584. $item['costPrice'] = $item['productInfo']['cost'] ?? 0;
  585. $item['trueStock'] = $item['productInfo']['stock'] ?? 0;
  586. $item['truePrice'] = $item['productInfo']['price'] ?? 0;
  587. $item['sum_price'] = $item['productInfo']['price'] ?? 0;
  588. if (!$is_activity) {
  589. [$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);
  590. $item['truePrice'] = $truePrice;
  591. $item['vip_truePrice'] = $vip_truePrice;
  592. $item['price_type'] = $type;
  593. }
  594. }
  595. if (isset($item['status']) && $item['status'] == 0) {
  596. $item['is_valid'] = 0;
  597. $invalid[] = $item;
  598. } else {
  599. switch ($shipping_type) {
  600. case 1:
  601. //不送达
  602. if (in_array($item['productInfo']['temp_id'], $tempIds)) {
  603. $item['is_valid'] = 0;
  604. $invalid[] = $item;
  605. } else {
  606. $item['is_valid'] = 1;
  607. $valid[] = $item;
  608. }
  609. break;
  610. case 2:
  611. //不支持到店自提
  612. if (isset($item['productInfo']['logistics']) && $item['productInfo']['logistics'] && !in_array(2, explode(',', $item['productInfo']['logistics']))) {
  613. $item['is_valid'] = 0;
  614. $invalid[] = $item;
  615. } else {
  616. $item['is_valid'] = 1;
  617. $valid[] = $item;
  618. }
  619. break;
  620. }
  621. }
  622. unset($item['attrInfo']);
  623. }
  624. return [$cartList, $valid, $invalid];
  625. }
  626. }