92、商城业务---购物车---添加购物车
因为我们添加成功后会跳转到下面这个页面,所以我们需要获取sku的信息和商品数量

所以当我们点击添加商品时就需要发送请求,请求如下:

1、controller层

2、serviceImpl层(将数据存储到redis中)
package com.gulimall.cart.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.gulimall.cart.feign.ProductFeignService;
import com.gulimall.cart.interceptor.CartInterceptor;
import com.gulimall.cart.service.CartService;
import com.gulimall.cart.to.UserInfoTo;
import com.gulimall.cart.vo.CartItem;
import com.gulimall.cart.vo.SkuInfoVo;
import com.gulimall.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadPoolExecutor;
@Slf4j
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    ProductFeignService productFeignService;
    @Autowired
    StringRedisTemplate redisTemplate;
    @Autowired
    ThreadPoolExecutor executor;
    private final String CART_PREFIX = "gulimall:cart";
    @Override
    public CartItem addToCart(Long skuId, Integer num) throws ExecutionException, InterruptedException {
        BoundHashOperations<String, Object, Object> cartOps = this.getCartOps();
        //2、商品添加到购物车
        CartItem cartItem = new CartItem();
        //异步
        CompletableFuture<Void> getSkuInfo = CompletableFuture.runAsync(() -> {
            //1、远程查询当前要添加的商品的信息
            R skuInfo = productFeignService.info(skuId);
            SkuInfoVo data = skuInfo.getData("skuInfo", new TypeReference<SkuInfoVo>() {
            });
            cartItem.setCheck(true);
            cartItem.setCount(num);
            cartItem.setImage(data.getSkuDefaultImg());
            cartItem.setTitle(data.getSkuTitle());
            cartItem.setSkuId(skuId);
            cartItem.setPrice(data.getPrice());
        }, executor);
        //3、远程查询sku的组合信息
        //异步
        CompletableFuture<Void> getSkuSaleAttrValues = CompletableFuture.runAsync(() -> {
            List<String> skuSaleAttrValue = productFeignService.getSkuSaleAttrValue(skuId);
            cartItem.setSkuAttr(skuSaleAttrValue);
        }, executor);
        CompletableFuture.allOf(getSkuInfo, getSkuSaleAttrValues).get();//等待异步请求都完成
        String s = JSON.toJSONString(cartItem);
        cartOps.put(skuId.toString(), s);
        return cartItem;
    }
    /**
     * 获取到我们要操作的购物车
     * @return
     */
    private BoundHashOperations<String, Object, Object> getCartOps() {
        //先得到当前用户信息
        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        String cartKey = "";
        if (userInfoTo.getUserId() != null) { //已登录
            //gulimall:cart:1
            cartKey = CART_PREFIX + userInfoTo.getUserId();
        } else {
            cartKey = CART_PREFIX + userInfoTo.getUserKey();
        }
        //绑定指定的key操作Redis
        BoundHashOperations<String, Object, Object> operations = redisTemplate.boundHashOps(cartKey);
        return operations;
    }
}
之所以选用redis的hash数据结构,是因为我们要存储成如下结构:

                    
                
                
            
        
浙公网安备 33010602011771号