no message

parent e567922c
...@@ -21,7 +21,7 @@ class CartApiController extends Controller ...@@ -21,7 +21,7 @@ class CartApiController extends Controller
{ {
//添加购物车, items: {"goods_id":1166788996788323407,"goods_number":2} //添加购物车, items: {"goods_id":1166788996788323407,"goods_number":2}
public function addCart(Request $request) public function saveCart(Request $request)
{ {
$validator = Validator::make($request->all(), [ $validator = Validator::make($request->all(), [
'items' => 'required|string', 'items' => 'required|string',
...@@ -37,13 +37,13 @@ class CartApiController extends Controller ...@@ -37,13 +37,13 @@ class CartApiController extends Controller
'data', 'data',
]); ]);
$result = CartService::addCart($data, $request->user->id); $result = CartService::saveCart($data, $request->user->id);
return !$result ? $this->setError('Add cart failed , please contact administrator'):$this->setSuccess('Add inquiry success'); return !$result ? $this->setError('Add cart failed , please contact administrator'):$this->setSuccess('Add inquiry success');
} }
//购物车列表 //购物车列表
public function cartLists(){ public function cartLists(Request $request){
$result = CartService::cartLists($request->user->id);
} }
......
...@@ -14,8 +14,8 @@ use App\Models\CartModel; ...@@ -14,8 +14,8 @@ use App\Models\CartModel;
//购物车服务器层 //购物车服务器层
class CartService class CartService
{ {
//添加购物车 //添加或者更新购物车
public static function addCart($data, $user_id) public static function saveCart($data, $user_id)
{ {
try{ try{
...@@ -47,14 +47,14 @@ class CartService ...@@ -47,14 +47,14 @@ class CartService
$checkHas = CartModel::where(["user_id"=>$user_id,"goods_id"=>$goods_id,"status"=>1])->first(); $checkHas = CartModel::where(["user_id"=>$user_id,"goods_id"=>$goods_id,"status"=>1])->first();
if ($checkHas){ //存在累计库存 if ($checkHas){ //存在累计库存
$temp["goods_number"] = $skuInfo["stock"] > ($item['buy_number']+$checkHas["buy_number"]) ? $item['buy_number']+$checkHas["buy_number"] : $skuInfo["stock"]; $temp["buy_number"] = $skuInfo["stock"] > ($item['buy_number']+$checkHas["buy_number"]) ? $item['buy_number']+$checkHas["buy_number"] : $skuInfo["stock"];
$temp["update_time"] = time(); $temp["update_time"] = time();
$flag = CartModel::where(["cart_id"=>$checkHas["cart_id"]])->update($temp); $flag = CartModel::where(["cart_id"=>$checkHas["cart_id"]])->update($temp);
if (!$flag){ if (!$flag){
return false; return false;
} }
}else{ //不存在插入购物车 }else{ //不存在插入购物车
$temp["goods_number"] = $skuInfo["stock"] > $item['buy_number'] ? $item['buy_number'] : $skuInfo["stock"]; $temp["buy_number"] = $skuInfo["stock"] > $item['buy_number'] ? $item['buy_number'] : $skuInfo["stock"];
$temp["create_time"] = time(); $temp["create_time"] = time();
$temp["update_time"] = time(); $temp["update_time"] = time();
...@@ -75,7 +75,7 @@ class CartService ...@@ -75,7 +75,7 @@ class CartService
} }
//刷新购物车并且返回列表 //刷新购物车并且返回列表
public static function cartUpdateGetLists($user_id){ public static function cartLists($user_id){
//当前用户所有可用的购物车数据 //当前用户所有可用的购物车数据
$query = CartModel::where(['user_id'=>$user_id,"status"=>1]) $query = CartModel::where(['user_id'=>$user_id,"status"=>1])
...@@ -92,6 +92,7 @@ class CartService ...@@ -92,6 +92,7 @@ class CartService
$redis = Redis::connection(); $redis = Redis::connection();
$goodsInfoArr = ThirdService::getGoodsInfo(array_column($result,"goods_id")); $goodsInfoArr = ThirdService::getGoodsInfo(array_column($result,"goods_id"));
$cartList = [];
foreach ($result as $item) { foreach ($result as $item) {
$goods_id = $item['goods_id']; $goods_id = $item['goods_id'];
......
...@@ -43,7 +43,8 @@ Route::middleware(['api', 'api.check'])->namespace('Api')->group(function () { ...@@ -43,7 +43,8 @@ Route::middleware(['api', 'api.check'])->namespace('Api')->group(function () {
Route::get('country/list', 'CountryApiController@list'); Route::get('country/list', 'CountryApiController@list');
Route::POST('cart/addCart', 'CartApiController@addCart'); //添加购物车 Route::POST('cart/saveCart', 'CartApiController@saveCart'); //添加或者更新购物车
Route::POST('cart/cartLists', 'CartApiController@cartLists'); //购物车列表
}); });
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