Cart.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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 $enableGradeMoney 是否设置商品会员价
  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. * @throws BaseException
  36. */
  37. public function getList(array $cartIds = [], bool $enableGradeMoney = true)
  38. {
  39. // 购物车列表
  40. $cartList = $this->getCartList($cartIds);
  41. // 整理商品ID集
  42. $goodsIds = helper::getArrayColumn($cartList, 'goods_id');
  43. if (empty($goodsIds)) return [];
  44. // 获取商品列表
  45. $goodsList = $this->getGoodsListByIds($goodsIds, $enableGradeMoney);
  46. // 整理购物车商品列表
  47. foreach ($cartList as $cartIdx => $item) {
  48. // 查找商品, 商品不存在则删除该购物车记录
  49. $result = $this->findGoods($goodsList, $item, $enableGradeMoney);
  50. if ($result !== false) {
  51. $cartList[$cartIdx]['goods'] = $result;
  52. } else {
  53. $this->clear([$item['id']]);
  54. unset($cartList[$cartIdx]);
  55. }
  56. }
  57. return $cartList;
  58. }
  59. /**
  60. * 获取购物车商品列表(用于订单结算台)
  61. * @param array $cartIds 购物车记录ID集
  62. * @return array
  63. * @throws BaseException
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\DbException
  66. * @throws \think\db\exception\ModelNotFoundException
  67. * @throws BaseException
  68. */
  69. public function getOrderGoodsList(array $cartIds = []): array
  70. {
  71. // 购物车列表
  72. $cartList = $this->getList($cartIds, false);
  73. // 订单商品列表
  74. $goodsList = [];
  75. foreach ($cartList as $item) {
  76. // 商品记录
  77. $goods = $item['goods'];
  78. // 商品单价
  79. $goods['goods_price'] = $goods['skuInfo']['goods_price'];
  80. // 商品购买数量
  81. $goods['total_num'] = $item['goods_num'];
  82. // 商品SKU索引
  83. $goods['goods_sku_id'] = $item['goods_sku_id'];
  84. // 商品购买总金额
  85. $goods['total_price'] = helper::bcmul($goods['goods_price'], $item['goods_num']);
  86. $goodsList[] = $goods;
  87. }
  88. return $goodsList;
  89. }
  90. /**
  91. * 检索查询商品
  92. * @param mixed $goodsList 商品列表
  93. * @param CartModel $item 购物车记录
  94. * @param bool $enableGradeMoney 是否设置商品会员价
  95. * @return false|mixed
  96. * @throws BaseException
  97. */
  98. private function findGoods($goodsList, CartModel $item, bool $enableGradeMoney = true)
  99. {
  100. // 查找商品记录
  101. $goodsInfo = helper::getArrayItemByColumn($goodsList, 'goods_id', $item['goods_id']);
  102. if (empty($goodsInfo)) {
  103. return false;
  104. }
  105. // 获取当前选择的商品SKU信息
  106. $goodsInfo['skuInfo'] = GoodsModel::getSkuInfo($goodsInfo, $item['goods_sku_id'], $enableGradeMoney);
  107. if (empty($goodsInfo['skuInfo'])) {
  108. return false;
  109. }
  110. // 商品封面 (优先sku封面)
  111. $goodsInfo['goods_image'] = $goodsInfo['skuInfo']['goods_image'] ?: $goodsInfo['goods_image'];
  112. // 这里需要用到clone, 因对象是引用传递 后面的值会覆盖前面的
  113. return clone $goodsInfo;
  114. }
  115. /**
  116. * 删除购物车中指定记录
  117. * @param array $cartIds
  118. * @return bool
  119. * @throws BaseException
  120. */
  121. public function clear(array $cartIds = []): bool
  122. {
  123. $model = new CartModel;
  124. return $model->clear($cartIds);
  125. }
  126. /**
  127. * 根据商品ID集获取商品列表
  128. * @param array $goodsIds
  129. * @param bool $enableGradeMoney 是否设置会员折扣价
  130. * @return mixed
  131. * @throws \think\db\exception\DataNotFoundException
  132. * @throws \think\db\exception\DbException
  133. * @throws \think\db\exception\ModelNotFoundException
  134. */
  135. private function getGoodsListByIds(array $goodsIds, bool $enableGradeMoney = true)
  136. {
  137. return (new GoodsModel)->setEnableGradeMoney($enableGradeMoney)
  138. ->getListByIdsFromApi($goodsIds)
  139. ->hidden(GoodsModel::getHidden(['goods_images']));
  140. }
  141. /**
  142. * 获取当前用户的购物车记录
  143. * @param array $cartIds 购物车记录ID集
  144. * @return \think\Collection
  145. * @throws BaseException
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\DbException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. */
  150. private function getCartList(array $cartIds = []): \think\Collection
  151. {
  152. // 当前用户ID
  153. $userId = UserService::getCurrentLoginUserId();
  154. // 购物车记录模型
  155. $model = new CartModel;
  156. // 检索查询条件
  157. $filter = [];
  158. if (!empty($cartIds)) {
  159. $filter[] = ['id', 'in', $cartIds];
  160. }
  161. // 查询列表记录
  162. return $model->where($filter)
  163. ->where('user_id', '=', $userId)
  164. ->where('is_delete', '=', 0)
  165. ->select();
  166. }
  167. }