Cart.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\api\service;
  13. use app\api\model\Cart as CartModel;
  14. use app\api\model\Goods as GoodsModel;
  15. use app\api\service\User as UserService;
  16. use app\common\library\helper;
  17. use app\common\service\BaseService;
  18. use cores\exception\BaseException;
  19. /**
  20. * 服务类: 购物车
  21. * Class Cart
  22. * @package app\api\service
  23. */
  24. class Cart extends BaseService
  25. {
  26. /**
  27. * 购物车商品列表(用于购物车页面)
  28. * @param array $cartIds 购物车记录ID集
  29. * @param bool $isGoodsGradeMoney 是否设置商品会员价
  30. * @return array|\think\Collection
  31. * @throws BaseException
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function getList(array $cartIds = [], bool $isGoodsGradeMoney = true)
  37. {
  38. // 购物车列表
  39. $cartList = $this->getCartList($cartIds);
  40. // 整理商品ID集
  41. $goodsIds = helper::getArrayColumn($cartList, 'goods_id');
  42. if (empty($goodsIds)) return [];
  43. // 获取商品列表
  44. $goodsList = $this->getGoodsListByIds($goodsIds, $isGoodsGradeMoney);
  45. // 整理购物车商品列表
  46. foreach ($cartList as $cartIdx => $item) {
  47. // 查找商品, 商品不存在则删除该购物车记录
  48. $result = $this->findGoods($goodsList, $item, $isGoodsGradeMoney);
  49. if ($result !== false) {
  50. $cartList[$cartIdx]['goods'] = $result;
  51. } else {
  52. $this->clear([$item['id']]);
  53. unset($cartList[$cartIdx]);
  54. }
  55. }
  56. return $cartList;
  57. }
  58. /**
  59. * 获取购物车商品列表(用于订单结算台)
  60. * @param array $cartIds 购物车记录ID集
  61. * @return array
  62. * @throws BaseException
  63. * @throws \think\db\exception\DataNotFoundException
  64. * @throws \think\db\exception\DbException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. */
  67. public function getOrderGoodsList(array $cartIds = []): array
  68. {
  69. // 购物车列表
  70. $cartList = $this->getList($cartIds, false);
  71. // 订单商品列表
  72. $goodsList = [];
  73. foreach ($cartList as $item) {
  74. // 商品记录
  75. $goods = $item['goods'];
  76. // 商品单价
  77. $goods['goods_price'] = $goods['skuInfo']['goods_price'];
  78. // 商品购买数量
  79. $goods['total_num'] = $item['goods_num'];
  80. // 商品SKU索引
  81. $goods['goods_sku_id'] = $item['goods_sku_id'];
  82. // 商品购买总金额
  83. $goods['total_price'] = helper::bcmul($goods['goods_price'], $item['goods_num']);
  84. $goodsList[] = $goods;
  85. }
  86. return $goodsList;
  87. }
  88. /**
  89. * 检索查询商品
  90. * @param mixed $goodsList 商品列表
  91. * @param CartModel $item 购物车记录
  92. * @param bool $isGoodsGradeMoney 是否设置商品会员价
  93. * @return false|mixed
  94. * @throws BaseException
  95. */
  96. private function findGoods($goodsList, CartModel $item, bool $isGoodsGradeMoney = true)
  97. {
  98. // 查找商品记录
  99. $goodsInfo = helper::getArrayItemByColumn($goodsList, 'goods_id', $item['goods_id']);
  100. if (empty($goodsInfo)) {
  101. return false;
  102. }
  103. // 获取当前选择的商品SKU信息
  104. $goodsInfo['skuInfo'] = GoodsModel::getSkuInfo($goodsInfo, $item['goods_sku_id'], $isGoodsGradeMoney);
  105. if (empty($goodsInfo['skuInfo'])) {
  106. return false;
  107. }
  108. // 商品封面 (优先sku封面)
  109. $goodsInfo['goods_image'] = $goodsInfo['skuInfo']['goods_image'] ?: $goodsInfo['goods_image'];
  110. // 这里需要用到clone, 因对象是引用传递 后面的值会覆盖前面的
  111. return clone $goodsInfo;
  112. }
  113. /**
  114. * 删除购物车中指定记录
  115. * @param array $cartIds
  116. * @return bool
  117. * @throws BaseException
  118. */
  119. public function clear(array $cartIds = []): bool
  120. {
  121. $model = new CartModel;
  122. return $model->clear($cartIds);
  123. }
  124. /**
  125. * 根据商品ID集获取商品列表
  126. * @param array $goodsIds
  127. * @param bool $isGoodsGradeMoney 是否设置会员折扣价
  128. * @return mixed
  129. */
  130. private function getGoodsListByIds(array $goodsIds, bool $isGoodsGradeMoney = true)
  131. {
  132. $model = new GoodsModel;
  133. return $model->isGoodsGradeMoney($isGoodsGradeMoney)
  134. ->getListByIdsFromApi($goodsIds)
  135. ->hidden(GoodsModel::getHidden(['goods_images']));
  136. }
  137. /**
  138. * 获取当前用户的购物车记录
  139. * @param array $cartIds 购物车记录ID集
  140. * @return \think\Collection
  141. * @throws BaseException
  142. * @throws \think\db\exception\DataNotFoundException
  143. * @throws \think\db\exception\DbException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. */
  146. private function getCartList(array $cartIds = []): \think\Collection
  147. {
  148. // 当前用户ID
  149. $userId = UserService::getCurrentLoginUserId();
  150. // 购物车记录模型
  151. $model = new CartModel;
  152. // 检索查询条件
  153. $filter = [];
  154. if (!empty($cartIds)) {
  155. $filter[] = ['id', 'in', $cartIds];
  156. }
  157. // 查询列表记录
  158. return $model->where($filter)
  159. ->where('user_id', '=', $userId)
  160. ->where('is_delete', '=', 0)
  161. ->select();
  162. }
  163. }