订单添加

parent f500d123
...@@ -37,13 +37,19 @@ class CartApiController extends Controller ...@@ -37,13 +37,19 @@ class CartApiController extends Controller
'data', 'data',
]); ]);
$result = CartService::saveCart($data, $request->user->id); $result = CartService::saveCart($data, $request->user->id,$request->user->gid);
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(Request $request){ public function cartLists(Request $request){
$result = CartService::cartLists($request->user->id); $result = CartService::cartLists($request->user->id,$request->user->gid);
return $this->setSuccessData($result);
}
//查询当前购物车数量
public function cartMyCount(Request $request){
$result = CartService::cartLists($request->user->id,$request->user->gid);
return $this->setSuccessData($result); return $this->setSuccessData($result);
} }
......
...@@ -16,7 +16,7 @@ class CheckApiLogin ...@@ -16,7 +16,7 @@ class CheckApiLogin
*/ */
public function handle($request, Closure $next) public function handle($request, Closure $next)
{ {
if (!\Auth::check()) { if (!\Auth::check() && !$request->is('api/cart/*')) { //一些接口可以登录或者不登录都可以通行
$response = [ $response = [
'code' => 1, 'code' => 1,
'msg' => 'need login...', 'msg' => 'need login...',
...@@ -24,6 +24,13 @@ class CheckApiLogin ...@@ -24,6 +24,13 @@ class CheckApiLogin
return response()->json($response); return response()->json($response);
} else { } else {
$request->user = \Auth::user(); $request->user = \Auth::user();
if (!$request->user){
$request->user->id = 0;
}
$gid = $request->cookie('sem_gid');
if ($gid){
$request->user->gid = $gid;
}
return $next($request); return $next($request);
} }
} }
......
...@@ -18,7 +18,7 @@ class CartService ...@@ -18,7 +18,7 @@ class CartService
static $week_filter = ["周"]; static $week_filter = ["周"];
//添加或者更新购物车 //添加或者更新购物车
public static function saveCart($data, $user_id) public static function saveCart($data,$user_id="",$gid="")
{ {
try{ try{
...@@ -35,11 +35,16 @@ class CartService ...@@ -35,11 +35,16 @@ class CartService
$goods_id = $v['goods_id']; $goods_id = $v['goods_id'];
$temp = [ $temp = [
'user_id' => $user_id,
'goods_id' => $v['goods_id'], 'goods_id' => $v['goods_id'],
'update_time' => time(),
]; ];
$skuInfo = $goodsInfoArr[$goods_id]; //sku库存 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 编码 $digikeyInfo = $redis->hget("sku_raw_map",$goods_id); //digikey 编码
if ($digikeyInfo){ if ($digikeyInfo){
$digikeyArr = json_decode($digikeyInfo,true); $digikeyArr = json_decode($digikeyInfo,true);
...@@ -94,8 +99,10 @@ class CartService ...@@ -94,8 +99,10 @@ class CartService
} }
} }
//刷新购物车并且返回列表 /*
public static function cartLists($user_id){ * 刷新购物车并且返回列表
*/
public static function cartLists($user_id,$gid=""){
//当前用户所有可用的购物车数据 //当前用户所有可用的购物车数据
$query = CartModel::where(['user_id'=>$user_id,"status"=>1]) $query = CartModel::where(['user_id'=>$user_id,"status"=>1])
...@@ -119,7 +126,13 @@ class CartService ...@@ -119,7 +126,13 @@ class CartService
$temp = [ $temp = [
'user_id' => $user_id, 'user_id' => $user_id,
'goods_id' => $v['goods_id'], 'goods_id' => $v['goods_id'],
'update_time' => time()
]; ];
if ($user_id){
$temp["user_id"] = $user_id;
}else{
$temp["gid"] = $gid;
}
$skuInfo = $goodsInfoArr[$goods_id]; //sku库存 $skuInfo = $goodsInfoArr[$goods_id]; //sku库存
$digikeyInfo = $redis->hget("sku_raw_map",$goods_id); //digikey 编码 $digikeyInfo = $redis->hget("sku_raw_map",$goods_id); //digikey 编码
...@@ -144,10 +157,9 @@ class CartService ...@@ -144,10 +157,9 @@ class CartService
} }
} }
$temp["status"] = $temp["buy_number"] > 0 ? CartModel::STATUS_YES:CartModel::STATUS_NO; $temp["status"] = $temp["buy_number"] > 0 ? CartModel::status_yes:CartModel::status_no;
$temp["buy_price"] = $buy_price; $temp["buy_price"] = $buy_price;
$temp["update_time"] = time(); $temp["update_time"] = time();
$temp["update_time"] = time();
$flag = CartModel::where(["cart_id"=>$v["cart_id"]])->update($temp); $flag = CartModel::where(["cart_id"=>$v["cart_id"]])->update($temp);
if (!$flag){ if (!$flag){
continue; continue;
...@@ -267,6 +279,13 @@ class CartService ...@@ -267,6 +279,13 @@ class CartService
return $info; return $info;
} }
/*
* 购物车当前数量
*/
public static function cartMyCount($user_id,$gid=""){
$where = $user_id ? ["user_id"=>$user_id]:["gid"=>$gid];
return CartModel::where($where)->count();
}
} }
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