1
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Model\Coupon;
|
||||
use App\Model\OrderInquiry;
|
||||
use App\Model\PatientHistoryInquiry;
|
||||
use App\Model\Product;
|
||||
use App\Model\ProductPlatformAmount;
|
||||
use App\Model\User;
|
||||
use App\Model\UserCoupon;
|
||||
use App\Model\UserPatient;
|
||||
@@ -222,32 +223,118 @@ class UserPatientService extends BaseService
|
||||
|
||||
$shopping_cart = json_decode($shopping_cart,true);
|
||||
|
||||
return success($shopping_cart);
|
||||
if (empty($shopping_cart)){
|
||||
return success();
|
||||
}
|
||||
|
||||
$data = array();
|
||||
foreach ($shopping_cart as $item){
|
||||
// 获取商品数据
|
||||
$params =array();
|
||||
$params['product_id'] = $item['product_id'];
|
||||
$product = Product::getOne($params);
|
||||
if (empty($product)){
|
||||
continue;
|
||||
}
|
||||
|
||||
$item['product_cover_img'] = addAliyunOssWebsite($product['product_cover_img']);
|
||||
$item['product_price'] = $product['product_price'];
|
||||
|
||||
$data[] = $item;
|
||||
}
|
||||
|
||||
return success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加购物车药品数据
|
||||
* 修改购物车药品数据
|
||||
* @return array
|
||||
*/
|
||||
public function addShoppingCart(): array
|
||||
public function putShoppingCart(): array
|
||||
{
|
||||
$user_info = $this->request->getAttribute("userInfo") ?? [];
|
||||
$product_id = $this->request->input("product_id");
|
||||
$shopping_cart_num = $this->request->input("shopping_cart_num");
|
||||
|
||||
$product_id = $this->request->route('product_id');
|
||||
// if ($shopping_cart_num > 0){
|
||||
// // 验证商品数据
|
||||
// $params =array();
|
||||
// $params['product_id'] = $product_id;
|
||||
// $product = Product::getOne($params);
|
||||
// if (empty($product)){
|
||||
// return fail();
|
||||
// }
|
||||
// // 获取商品库存
|
||||
// $params =array();
|
||||
// $params['product_platform_id'] = $product['product_platform_id'];
|
||||
// $product_platform_amount = ProductPlatformAmount::getOne($params);
|
||||
// if ($product_platform_amount['stock'] <= 0){
|
||||
// return fail(HttpEnumCode::HTTP_ERROR,"商品库存不足");
|
||||
// }
|
||||
// }
|
||||
|
||||
// $redis = $this->container->get(Redis::class);
|
||||
// 读取缓存数据
|
||||
$redis = $this->container->get(Redis::class);
|
||||
$redis_key = "shopping_cart" . $user_info['client_user_id'];
|
||||
|
||||
// 验证商品数据
|
||||
$params =array();
|
||||
$params['product_id'] = $product_id;
|
||||
$product = Product::getOne($params);
|
||||
if (empty($product)){
|
||||
return fail();
|
||||
$shopping_cart = $redis->get($redis_key);
|
||||
if (!empty($shopping_cart)){
|
||||
$shopping_cart = json_decode($shopping_cart,true);
|
||||
}
|
||||
|
||||
// 获取商品库存
|
||||
// 处理缓存数据为空的情况
|
||||
if (empty($shopping_cart)){
|
||||
if ($shopping_cart_num > 0){
|
||||
// 处理存储数据
|
||||
$data = array();
|
||||
$data['product_id'] = $product_id;
|
||||
$data['shopping_cart_num'] = $shopping_cart_num;
|
||||
$res = $redis->set($redis_key,json_encode([$data],JSON_UNESCAPED_UNICODE));
|
||||
if (!$res){
|
||||
return fail(HttpEnumCode::SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
return success();
|
||||
}
|
||||
|
||||
$product_ids = array_column($shopping_cart,'product_id');
|
||||
if (in_array($product_id,$product_ids)){
|
||||
// 商品存在于购物车缓存中
|
||||
foreach ($shopping_cart as $key => $value){
|
||||
if ($value['product_id'] == $product_id){
|
||||
if ($shopping_cart_num <= 0){
|
||||
// 商品存在于购物车缓存中,此数据表示删除
|
||||
unset($shopping_cart[$key]);
|
||||
}else{
|
||||
$shopping_cart[$key]['shopping_cart_num'] = $shopping_cart_num;
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
// 商品未存在于购物车缓存中。
|
||||
$data = array();
|
||||
$data['product_id'] = $product_id;
|
||||
$data['shopping_cart_num'] = $shopping_cart_num;
|
||||
|
||||
array_unshift($shopping_cart,$data);
|
||||
}
|
||||
|
||||
if (!empty($shopping_cart)){
|
||||
// 重置数组排列
|
||||
array_values($shopping_cart);
|
||||
|
||||
// 添加到缓存中
|
||||
$res = $redis->set($redis_key,json_encode($shopping_cart,JSON_UNESCAPED_UNICODE));
|
||||
if (!$res){
|
||||
return fail(HttpEnumCode::SERVER_ERROR);
|
||||
}
|
||||
}else{
|
||||
$res = $redis->del($redis_key);
|
||||
if (!$res){
|
||||
return fail(HttpEnumCode::SERVER_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
return success();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user