购物车

parent 309264ef
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Http\ApiHelper\ApiCode;
use App\Http\Requests\UserRegister; use App\Http\Requests\UserRegister;
use App\Http\Services\CartService; use App\Http\Services\CartService;
use App\Http\Services\InquiryService; use App\Http\Services\InquiryService;
...@@ -19,11 +20,27 @@ use Illuminate\Support\Facades\Validator; ...@@ -19,11 +20,27 @@ use Illuminate\Support\Facades\Validator;
//购物车控制器 //购物车控制器
class CartApiController extends Controller class CartApiController extends Controller
{ {
//单条添加或者更新购物车
//添加购物车, items: {"goods_id":1166788996788323407,"goods_number":2}
public function saveCart(Request $request) public function saveCart(Request $request)
{ {
$validator = Validator::make($request->all(), [ $validator = Validator::make($request->all(), [
'goods_id' => 'required|string',
'buy_number' => 'required|string',
'cart_id' => 'string',
]);
if ($validator->fails()) {
return $this->setError($validator->errors()->first());
}
$result = CartService::saveCart($request->user->id,$request->user->gid,$request->input("goods_id",0),$request->input("buy_number",0));
return !$result ? $this->setError('Add cart failed , please contact administrator'):$this->setSuccess('success',ApiCode::API_CODE_SUCCESS,$result);
}
//批量添加购物车, items: {"goods_id":1166788996788323407,"goods_number":2}
public function saveCartBatch(Request $request)
{
$validator = Validator::make($request->all(), [
'items' => 'required|string', 'items' => 'required|string',
], [ ], [
'items.min' => 'items must be at least 1 characters long.' 'items.min' => 'items must be at least 1 characters long.'
......
...@@ -17,8 +17,93 @@ class CartService ...@@ -17,8 +17,93 @@ class CartService
static $day_filter = ["工作日","个工作日","日"]; static $day_filter = ["工作日","个工作日","日"];
static $week_filter = ["周"]; static $week_filter = ["周"];
//添加或者更新购物车 //添加或者更新购物车(单条)
public static function saveCart($data,$user_id="",$gid="") public static function saveCart($user_id="",$gid="",$goods_id=0,$buy_number=0)
{
try{
$con = DB::connection();
$con->beginTransaction();
$redis = Redis::connection();
$goodsInfoArr = ThirdService::getGoodsInfo([$goods_id]);
$temp = [
'goods_id' => $goods_id,
'update_time' => time(),
];
if ($user_id){
$temp["user_id"] = $user_id;
}else{
$temp["gid"] = $gid;
}
$skuInfo = $goodsInfoArr[$goods_id]; //sku库存
$digikeyInfo = $redis->hget("sku_raw_map",$goods_id); //digikey 编码
if ($digikeyInfo){
$digikeyArr = json_decode($digikeyInfo,true);
$temp["raw_goods_sn"]= $digikeyArr["raw_goods_id"];
$temp["raw_goods_packing"]= $digikeyArr["pack"];
$temp["raw_brand_name"]= $digikeyArr["raw_brand_name"];
}
//购买时价格
$buy_price = 0;
$ladder_price = data_get($skuInfo,"ladder_price");
if ($ladder_price){ //购买价格
foreach ($ladder_price as $a=>$b){
if ($buy_number >= $b["purchases"]){
$buy_price = $b["price_us"];
}else{
break;
}
}
}
$temp["buy_price"] = $buy_price;
$checkHas = CartModel::where(["user_id"=>$user_id,"goods_id"=>$goods_id,"status"=>1])->first();
$temp["buy_number"] = $skuInfo["stock"] > $buy_number ? $buy_number : $skuInfo["stock"];
if ($checkHas){
$cart_id = $checkHas["cart_id"];
$temp["update_time"] = time();
$temp["status"] = $buy_number > 0 ? CartModel::status_yes:CartModel::status_no;
$flag = CartModel::where(["cart_id"=>$checkHas["cart_id"]])->update($temp);
if (!$flag){
return false;
}
}else{ //不存在插入购物车
$temp["status"] = $buy_number > 0 ? CartModel::status_yes:CartModel::status_no;
$temp["create_time"] = time();
$temp["update_time"] = time();
$cart_id = CartModel::insertGetId($temp);
if (!$cart_id){
return false;
}
}
#返回更新详情
$con->commit();
$te = [
"cart_id"=>$cart_id,
"goods_id"=>$goods_id,
"buy_price"=>$buy_price,
"buy_number"=>$buy_number,
"ext_price"=>round($buy_price*$buy_number,2),
"stock"=>$skuInfo["stock"],
];
return $te;
}catch (\Exception $e){
$con->rollback();
throw new InvalidRequestException($e->getMessage().$e->getLine());
}
}
//添加或者更新购物车(批量)
public static function saveCartBatch($data,$user_id="",$gid="")
{ {
try{ try{
...@@ -180,6 +265,7 @@ class CartService ...@@ -180,6 +265,7 @@ class CartService
//格式化数据 //格式化数据
$skuInfo = self::dullDataInfo($skuInfo); $skuInfo = self::dullDataInfo($skuInfo);
$cartList[] = [ $cartList[] = [
"cart_id"=>$v["cart_id"], //cart_id
"goods_id"=>$skuInfo["goods_id"], //sku_id "goods_id"=>$skuInfo["goods_id"], //sku_id
"goods_name"=>$skuInfo["goods_name"], //型号名称 "goods_name"=>$skuInfo["goods_name"], //型号名称
"status_cn"=>\Arr::get(CartModel::$status,$v["status"]), //型号名称 "status_cn"=>\Arr::get(CartModel::$status,$v["status"]), //型号名称
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment