Cart.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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\index\service;
  13. use app\index\model\Cart as CartModel;
  14. use app\index\model\Goods as GoodsModel;
  15. use app\index\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. public function getCartIds()
  62. {
  63. $userId = UserService::getCurrentLoginUserId();
  64. if (empty($userId))return [];
  65. $model = new CartModel;
  66. $carts = $model->where('user_id', '=', $userId)
  67. ->where('is_delete', '=', 0)
  68. ->select()->toArray();
  69. return array_column($carts,'id');
  70. }
  71. /**
  72. * 获取购物车商品列表(用于订单结算台)
  73. * @param array $cartIds 购物车记录ID集
  74. * @return array
  75. * @throws BaseException
  76. * @throws \think\db\exception\DataNotFoundException
  77. * @throws \think\db\exception\DbException
  78. * @throws \think\db\exception\ModelNotFoundException
  79. */
  80. public function getOrderGoodsList(array $cartIds = []): array
  81. {
  82. // 购物车列表
  83. $cartList = $this->getList($cartIds, false);
  84. // 订单商品列表
  85. $goodsList = [];
  86. foreach ($cartList as $item) {
  87. // 商品记录
  88. $goods = $item['goods'];
  89. // 商品单价
  90. $goods['goods_price'] = $goods['skuInfo']['goods_price'];
  91. // 商品购买数量
  92. $goods['total_num'] = $item['goods_num'];
  93. //每一套的口味备注
  94. $goods['remark_for_one'] = $item['remark_for_one'];
  95. // 商品SKU索引
  96. $goods['goods_sku_id'] = $item['goods_sku_id'];
  97. // 商品购买总金额
  98. $goods['total_price'] = helper::bcmul($goods['goods_price'], $item['goods_num']);
  99. $goodsList[] = $goods;
  100. }
  101. return $goodsList;
  102. }
  103. /**
  104. * 检索查询商品
  105. * @param mixed $goodsList 商品列表
  106. * @param CartModel $item 购物车记录
  107. * @param bool $isGoodsGradeMoney 是否设置商品会员价
  108. * @return false|mixed
  109. * @throws BaseException
  110. */
  111. private function findGoods($goodsList, CartModel $item, bool $isGoodsGradeMoney = true)
  112. {
  113. // 查找商品记录
  114. $goodsInfo = helper::getArrayItemByColumn($goodsList, 'goods_id', $item['goods_id']);
  115. if (empty($goodsInfo)) {
  116. return false;
  117. }
  118. // 获取当前选择的商品SKU信息
  119. $goodsInfo['skuInfo'] = GoodsModel::getSkuInfo($goodsInfo, $item['goods_sku_id'], $isGoodsGradeMoney);
  120. // 这里需要用到clone, 因对象是引用传递 后面的值会覆盖前面的
  121. return clone $goodsInfo;
  122. }
  123. /**
  124. * 删除购物车中指定记录
  125. * @param array $cartIds
  126. * @return bool
  127. * @throws BaseException
  128. */
  129. public function clear(array $cartIds = []): bool
  130. {
  131. $model = new CartModel;
  132. return $model->clear($cartIds);
  133. }
  134. /**
  135. * 根据商品ID集获取商品列表
  136. * @param array $goodsIds
  137. * @param bool $isGoodsGradeMoney 是否设置会员折扣价
  138. * @return mixed
  139. */
  140. private function getGoodsListByIds(array $goodsIds, bool $isGoodsGradeMoney = true)
  141. {
  142. $model = new GoodsModel;
  143. return $model->isGoodsGradeMoney($isGoodsGradeMoney)->getListByIdsFromApi($goodsIds);
  144. }
  145. /**
  146. * 获取当前用户的购物车记录
  147. * @param array $cartIds 购物车记录ID集
  148. * @return \think\Collection
  149. * @throws BaseException
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. private function getCartList(array $cartIds = []): \think\Collection
  155. {
  156. // 当前用户ID
  157. $userId = UserService::getCurrentLoginUserId();
  158. // 购物车记录模型
  159. $model = new CartModel;
  160. // 检索查询条件
  161. $filter = [];
  162. if (!empty($cartIds)) {
  163. $filter[] = ['id', 'in', $cartIds];
  164. }
  165. // 查询列表记录
  166. return $model->where($filter)
  167. ->where('user_id', '=', $userId)
  168. ->where('is_delete', '=', 0)
  169. ->select();
  170. }
  171. }