// +---------------------------------------------------------------------- declare (strict_types=1); namespace app\api\service; use app\api\model\Cart as CartModel; use app\api\model\Goods as GoodsModel; use app\api\model\GoodsSku as GoodsSkuModel; use app\api\model\User as UserModel; use app\api\service\User as UserService; use app\common\enum\Setting as SettingEnum; use app\common\exception\BaseException; use app\common\library\helper; use app\common\service\BaseService; use app\api\model\Setting as SettingModel; /** * 服务类: 购物车 * Class Cart * @package app\api\service */ class Cart extends BaseService { /** * 购物车商品列表(用于购物车页面) * @param array $cartIds 购物车记录ID集 * @return array|\think\Collection * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getList(array $cartIds = []) { // 购物车列表 $cartList = $this->getCartList($cartIds); // 整理商品ID集 $goodsIds = helper::getArrayColumn($cartList, 'goods_id'); if (empty($goodsIds)) return []; // 获取商品列表 $goodsList = $this->getGoodsListByIds($goodsIds); // 隐藏冗余的属性 $goodsList->hidden(array_merge((new \app\api\model\Goods)->hidden, ['content', 'goods_images', 'images'])); // $goodsList->hidden(['content', 'goods_images', 'images']); // 整理购物车商品列表 foreach ($cartList as $cartIdx => $item) { // 查找商品, 商品不存在则删除该购物车记录 $result = $this->findGoods($goodsList, $item); if ($result !== false) { $cartList[$cartIdx]['goods'] = $result; } else { $this->clear([$item['id']]); unset($cartList[$cartIdx]); } } return $cartList; } /** * 获取购物车商品列表(用于订单结算台) * @param array $cartIds 购物车记录ID集 * @return array * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function getOrderGoodsList(array $cartIds = []) { // 购物车列表 $cartList = $this->getList($cartIds); if (empty($cartList)) { // throwError('未找到商品信息3'); } // 当前用户 $user = UserService::getCurrentLoginUser(true); // 订单商品列表 $goodsList = []; foreach ($cartList as $item) { // 商品记录 $goods = $item['goods']; // 商品单价 $goods['goods_price'] = $goods['skuInfo']['goods_price']; // 商品购买数量 $goods['total_num'] = $item['goods_num']; // 商品SKU索引 $goods['goods_sku_id'] = $item['goods_sku_id']; // 商品SKU名称 $goods['goods_sku_name'] = $goods['skuInfo']['goods_props'][0]['value']['name']??''; // 商品购买总金额 $goods['total_price'] = helper::bcmul($goods['goods_price'], $item['goods_num']); // 获取链接分享者的用户ID和店铺ID $goods['staffUserId'] = $item['staff_user_id']; //顾客购物优惠比例 $goods['distributorRadio'] = 0; if($user['role']==99){ $goods['distributorRadio'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR)['shopping_discount']; }else{ if($item['staff_user_id']){ $user_staff = UserModel::detail($item['staff_user_id']); if($user_staff && $user_staff['role'] == 99 && $item['is_staff']){ $goods['distributorRadio'] = SettingModel::getItem(SettingEnum::DISTRIBUTOR)['shopping_discount']; } } } $goods['shopId'] = $item['shop_id']; $goods['cart_id'] = $item['id']; $goodsList[] = $goods; } return $goodsList; } /** * 检索查询商品 * @param mixed $goodsList 商品列表 * @param CartModel $item 购物车记录 * @return false|mixed */ private function findGoods($goodsList, CartModel $item) { // 查找商品记录 $result = helper::getArrayItemByColumn($goodsList, 'goods_id', $item['goods_id']); if (empty($result)) { return false; } // 获取当前选择的商品SKU信息 $result['skuInfo'] = GoodsSkuModel::detail($result['goods_id'], $item['goods_sku_id']); // 这里需要用到clone, 因对象是引用传递 后面的值会覆盖前面的 return clone $result; } /** * 删除购物车中指定记录 * @param array $cartIds * @return bool * @throws BaseException */ public function clear(array $cartIds = []) { $model = new CartModel; return $model->clear($cartIds); } /** * 根据商品ID集获取商品列表 * @param array $goodsIds * @return mixed */ private function getGoodsListByIds(array $goodsIds) { $model = new GoodsModel; return $model->getListByIdsFromApi($goodsIds); } /** * 获取当前用户的购物车记录 * @param array $cartIds 购物车记录ID集 * @return \think\Collection * @throws BaseException * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ private function getCartList(array $cartIds = []) { // 当前用户ID $userId = UserService::getCurrentLoginUserId(); // 购物车记录模型 $model = new CartModel; // 检索查询条件 $filter = []; if (!empty($cartIds)) { $filter[] = ['id', 'in', $cartIds]; } // 查询列表记录 return $model->where($filter) ->where('user_id', '=', $userId) ->where('is_delete', '=', 0) ->order('create_time', 'desc') ->select(); } }