Cart.php 5.9 KB

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