Cart.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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\model\GoodsSku as GoodsSkuModel;
  16. use app\api\model\User as UserModel;
  17. use app\api\service\User as UserService;
  18. use app\common\enum\Setting as SettingEnum;
  19. use app\common\exception\BaseException;
  20. use app\common\library\helper;
  21. use app\common\service\BaseService;
  22. use app\api\model\Setting as SettingModel;
  23. /**
  24. * 服务类: 购物车
  25. * Class Cart
  26. * @package app\api\service
  27. */
  28. class Cart extends BaseService
  29. {
  30. /**
  31. * 购物车商品列表(用于购物车页面)
  32. * @param array $cartIds 购物车记录ID集
  33. * @return array|\think\Collection
  34. * @throws BaseException
  35. * @throws \think\db\exception\DataNotFoundException
  36. * @throws \think\db\exception\DbException
  37. * @throws \think\db\exception\ModelNotFoundException
  38. */
  39. public function getList(array $cartIds = [])
  40. {
  41. // 购物车列表
  42. $cartList = $this->getCartList($cartIds);
  43. // 整理商品ID集
  44. $goodsIds = helper::getArrayColumn($cartList, 'goods_id');
  45. if (empty($goodsIds)) return [];
  46. // 获取商品列表
  47. $goodsList = $this->getGoodsListByIds($goodsIds);
  48. // 隐藏冗余的属性
  49. $goodsList->hidden(array_merge((new \app\api\model\Goods)->hidden, ['content', 'goods_images', 'images']));
  50. // $goodsList->hidden(['content', 'goods_images', 'images']);
  51. // 整理购物车商品列表
  52. foreach ($cartList as $cartIdx => $item) {
  53. // 查找商品, 商品不存在则删除该购物车记录
  54. $result = $this->findGoods($goodsList, $item);
  55. if ($result !== false) {
  56. $cartList[$cartIdx]['goods'] = $result;
  57. } else {
  58. $this->clear([$item['id']]);
  59. unset($cartList[$cartIdx]);
  60. }
  61. }
  62. return $cartList;
  63. }
  64. /**
  65. * 获取购物车商品列表(用于订单结算台)
  66. * @param array $cartIds 购物车记录ID集
  67. * @return array
  68. * @throws BaseException
  69. * @throws \think\db\exception\DataNotFoundException
  70. * @throws \think\db\exception\DbException
  71. * @throws \think\db\exception\ModelNotFoundException
  72. */
  73. public function getOrderGoodsList(array $cartIds = [])
  74. {
  75. // 购物车列表
  76. $cartList = $this->getList($cartIds);
  77. if (empty($cartList)) {
  78. // throwError('未找到商品信息3');
  79. }
  80. // 当前用户
  81. $user = UserService::getCurrentLoginUser(true);
  82. // 订单商品列表
  83. $goodsList = [];
  84. foreach ($cartList as $item) {
  85. // 商品记录
  86. $goods = $item['goods'];
  87. // 商品单价
  88. $goods['goods_price'] = $goods['skuInfo']['goods_price'];
  89. // 商品购买数量
  90. $goods['total_num'] = $item['goods_num'];
  91. // 商品SKU索引
  92. $goods['goods_sku_id'] = $item['goods_sku_id'];
  93. // 商品SKU名称
  94. $goods['goods_sku_name'] = $goods['skuInfo']['goods_props'][0]['value']['name']??'';
  95. // 商品购买总金额
  96. $goods['total_price'] = helper::bcmul($goods['goods_price'], $item['goods_num']);
  97. // 获取链接分享者的用户ID和店铺ID
  98. $goods['staffUserId'] = $item['staff_user_id'];
  99. //顾客购物优惠比例
  100. $goods['distributorRadio'] = 0;
  101. if($user['role']==99){
  102. $goods['distributorRadio'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR)['shopping_discount'];
  103. }else{
  104. if($item['staff_user_id']){
  105. $user_staff = UserModel::detail($item['staff_user_id']);
  106. if($user_staff && $user_staff['role'] == 99 && $item['is_staff']){
  107. $goods['distributorRadio'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR)['shopping_discount'];
  108. }
  109. }
  110. }
  111. $goods['shopId'] = $item['shop_id'];
  112. $goods['cart_id'] = $item['id'];
  113. $goodsList[] = $goods;
  114. }
  115. return $goodsList;
  116. }
  117. /**
  118. * 检索查询商品
  119. * @param mixed $goodsList 商品列表
  120. * @param CartModel $item 购物车记录
  121. * @return false|mixed
  122. */
  123. private function findGoods($goodsList, CartModel $item)
  124. {
  125. // 查找商品记录
  126. $result = helper::getArrayItemByColumn($goodsList, 'goods_id', $item['goods_id']);
  127. if (empty($result)) {
  128. return false;
  129. }
  130. // 获取当前选择的商品SKU信息
  131. $result['skuInfo'] = GoodsSkuModel::detail($result['goods_id'], $item['goods_sku_id']);
  132. // 这里需要用到clone, 因对象是引用传递 后面的值会覆盖前面的
  133. return clone $result;
  134. }
  135. /**
  136. * 删除购物车中指定记录
  137. * @param array $cartIds
  138. * @return bool
  139. * @throws BaseException
  140. */
  141. public function clear(array $cartIds = [])
  142. {
  143. $model = new CartModel;
  144. return $model->clear($cartIds);
  145. }
  146. /**
  147. * 根据商品ID集获取商品列表
  148. * @param array $goodsIds
  149. * @return mixed
  150. */
  151. private function getGoodsListByIds(array $goodsIds)
  152. {
  153. $model = new GoodsModel;
  154. return $model->getListByIdsFromApi($goodsIds);
  155. }
  156. /**
  157. * 获取当前用户的购物车记录
  158. * @param array $cartIds 购物车记录ID集
  159. * @return \think\Collection
  160. * @throws BaseException
  161. * @throws \think\db\exception\DataNotFoundException
  162. * @throws \think\db\exception\DbException
  163. * @throws \think\db\exception\ModelNotFoundException
  164. */
  165. private function getCartList(array $cartIds = [])
  166. {
  167. // 当前用户ID
  168. $userId = UserService::getCurrentLoginUserId();
  169. // 购物车记录模型
  170. $model = new CartModel;
  171. // 检索查询条件
  172. $filter = [];
  173. if (!empty($cartIds)) {
  174. $filter[] = ['id', 'in', $cartIds];
  175. }
  176. // 查询列表记录
  177. return $model->where($filter)
  178. ->where('user_id', '=', $userId)
  179. ->where('is_delete', '=', 0)
  180. ->order('create_time', 'desc')
  181. ->select();
  182. }
  183. }