$type, 'uid' => $uid, 'product_id' => $product_id, 'product_attr_unique' => $product_attr_unique, 'is_new' => $is_new, 'is_pay' => 0, 'is_del' => 0, 'combination_id' => $combination_id, 'seckill_id' => $seckill_id, 'integral_id' => $integral_id ]; if ($cart = self::where($where)->find()) { $cart->cart_num = $cart_num; $cart->add_time = time(); $cart->save(); return $cart; } else { return self::set(compact('uid', 'product_id', 'cart_num', 'product_attr_unique', 'is_new', 'type', 'combination_id', 'seckill_id', 'integral_id')); } } /**删除购物车 * @param $uid * @param $ids * @return StoreCart */ public static function removeUserCart($uid, $ids) { return self::where('uid', $uid)->where('id', 'IN', explode(",", $ids))->update(['is_del' => 1]); } /**获取购物车数量 * @param $uid * @param $type * @return int|string * @throws \think\Exception */ public static function getUserCartNum($uid, $type) { return self::where('uid', $uid)->where('type', $type)->where('is_pay', 0)->where('is_del', 0)->where('is_new', 0)->count(); } /**修改购物车商品数量 * @param $cartId * @param $cartNum * @param $uid * @return StoreCart|bool */ public static function changeUserCartNum($cartId, $cartNum, $uid) { if (!self::be(['uid' => $uid, 'id' => $cartId, 'cart_num' => $cartNum])) { return self::where('uid', $uid)->where('id', $cartId)->update(['cart_num' => $cartNum]); } else { return true; } } /**获取购物车数据 * @param $uid * @param string $cartIds * @param int $status * @param $is_vip * @return array * @throws \think\Exception * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getUserProductCartList($uid, $cartIds = '', $status = 0, $is_vip = 0) { $productInfoField = 'id,image,slider_image,price,cost,ot_price,vip_price,postage,mer_id,give_gold_num,free_shipping,cate_id,sales,stock,store_name,unit_name,is_show,is_del,is_postage,is_virtual'; $model = new self(); $valid = $invalid = []; if ($cartIds) $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0) ->where('is_del', 0); else $model = $model->where('uid', $uid)->where('type', 'product')->where('is_pay', 0)->where('is_new', 0) ->where('is_del', 0); if ($cartIds) $model->where('id', 'IN', $cartIds); $list = $model->select()->toArray(); if (!count($list)) return compact('valid', 'invalid'); foreach ($list as $k => $cart) { $product = StoreProduct::field($productInfoField) ->find($cart['product_id'])->toArray(); $cart['productInfo'] = $product; //商品不存在 if (!$product) { $model->where('id', $cart['id'])->update(['is_del' => 1]); //商品删除或无库存 } else if (!$product['is_show'] || $product['is_del'] || !$product['stock']) { $invalid[] = $cart; } else { $cart['truePrice'] = $is_vip ? (isset($cart['productInfo']['vip_price']) ? (float)$cart['productInfo']['vip_price'] : (float)$cart['productInfo']['price']) : (float)$cart['productInfo']['price']; $cart['costPrice'] = (float)$cart['productInfo']['cost']; $cart['trueStock'] = $cart['productInfo']['stock']; $valid[] = $cart; } } foreach ($valid as $k => $cart) { if ($cart['trueStock'] < $cart['cart_num']) { $cart['cart_num'] = $cart['trueStock']; $model->where('id', $cart['id'])->update(['cart_num' => $cart['cart_num']]); $valid[$k] = $cart; } } return compact('valid', 'invalid'); } }