A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘波啵 中级黑马   /  2019-9-26 15:31  /  826 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

​     使用redis(hash类型数据)实现购物车的存储。大key为  CacheKey.CART_LIST ,当 前登录名作为小key。购物车存储结构如下图:
每个用户的购物车就是一个list集合,集合中是map类型的数据,有两个属性,一个是 item,表示购物车明细数据,另一个是checked,用于存储购物车是否被选中。
每个用户的购物车类型是 List> 。 购物车列表采用前端渲染(vue.js)的方式。 1.3 后端代码 1.3.1 购物车列表
(1)创建服务接口
 
(2)CacheKey枚举增加CART_LIST用于存储购物车列表 , 创建服务实现类
/**  * 购物车服务  */ public interface CartService {
    /**      * 从redis中提取购物车      * @param username      * @return      */     public List<Map<String,Object>> findCartList(String username); }
 
(3)在qingcheng_portal工程添加CartController
 
@Service public class CartServiceImpl implements CartService {
    @Autowired     private RedisTemplate redisTemplate;
    @Override     public List<Map<String, Object>> findCartList(String username) {         System.out.println("从redis中提取购物车"+username);         List<Map<String,Object>> cartList = (List<Map<String,Object>>)  redisTemplate.boundHashOps(CacheKey.CART_LIST).get(username);         if(cartList==null){             return new ArrayList<>();         }         return cartList;     } }
@RestController @RequestMapping("/cart") public class CartController {
    @Reference     private CartService cartService;
    /**      * 从redis中提取购物车      * @return      */     @GetMapping("/findCartList")     public List<Map<String, Object>> findCartList(){         String username =  SecurityContextHolder.getContext().getAuthentication().getName();         List<Map<String, Object>> cartList =  cartService.findCartList(username);         return cartList;     }
}
1.3.2 添加商品到购物车
实现思路:遍历购物车,如果购物车列表中不存在该商品则添加,存在则累加数量。
(1)CartService接口新增方法定义
 
(2)CartServiceImpl实现此方法
    /**      * 添加商品到购物车      * @param username      * @param skuId      * @param num      */     public void addItem(String username, String  skuId, Integer num);
     @Reference     private SkuService skuService;
    @Reference     private CategoryService categoryService;
    @Override     public void addItem(String username, String skuId, Integer num) {         //实现思路: 遍历购物车,如果购物车中存在该商品则累加数量,如果不存在则 添加购物车项         //获取购物车         List<Map<String, Object>> cartList = findCartList(username);
        boolean flag=false;//是否在购物车中存在         for( Map map:cartList  ){             OrderItem orderItem= (OrderItem)map.get("item");             if(orderItem.getSkuId().equals(skuId)){  //购物车存在该商品                 if(orderItem.getNum()<=0){ //如果数量小于等于0                     cartList.remove(map);//购物车项删除                     break;                 }                 int weight = orderItem.getWeight() / orderItem.getNum();   //单个商品重量
                orderItem.setNum( orderItem.getNum()+num ); //数量累加                 orderItem.setMoney(  orderItem.getNum()*orderItem.getPrice() );//金额计算                 orderItem.setWeight( weight*orderItem.getNum() );//重量计 算
                if(orderItem.getNum()<=0){ //如果数量小于等于0                     cartList.remove(map);//购物车项删除                 }
                flag=true;                 break;             }         }         //如果购物车中没有该商品,则添加
        if(flag==false){
            Sku sku = skuService.findById(skuId);             if(sku==null){                 throw new RuntimeException("商品不存在");             }             if(!"1".equals(sku.getStatus())){                 throw new RuntimeException("商品状态不合法");             }             if(num<=0){  //数量不能为0或负数                 throw new RuntimeException("商品数量不合法");             }
            OrderItem orderItem=new OrderItem();
            orderItem.setSkuId(skuId);             orderItem.setSpuId(sku.getSpuId());             orderItem.setNum(num);             orderItem.setImage(sku.getImage());             orderItem.setPrice(sku.getPrice());             orderItem.setName(sku.getName());             orderItem.setMoney( orderItem.getPrice()*num );//金额计算             if(sku.getWeight()==null){                 sku.setWeight(0);             }             orderItem.setWeight(sku.getWeight()*num); //重量计算
            //商品分类             orderItem.setCategoryId3(sku.getCategoryId());             Category category3 =  (Category)redisTemplate.boundHashOps(CacheKey.CATEGORY).get(sku.getCatego ryId());             if(category3==null){                 category3 =  categoryService.findById(sku.getCategoryId()); //根据3级分类id查2级                  redisTemplate.boundHashOps(CacheKey.CATEGORY).put(sku.getCategoryId(),  category3 );             }             orderItem.setCategoryId2(category3.getParentId());
(3)qingcheng_web_portal的CartController新增方法
 
1.4 前端代码
            Category category2 =  (Category)redisTemplate.boundHashOps(CacheKey.CATEGORY).get(category3.get ParentId());             if(category2==null ){                 category2 =  categoryService.findById(category3.getParentId()); //根据2级分类id查询1级                  redisTemplate.boundHashOps(CacheKey.CATEGORY).put(category3.getParentId() , category2 );             }             orderItem.setCategoryId1(category2.getParentId());
            Map map=new HashMap();             map.put("item",orderItem);             map.put("checked",true);//默认选中
            cartList.add(map);         }          redisTemplate.boundHashOps(CacheKey.CART_LIST).put(username,cartList);     }  
    /**      * 添加商品到购物车      * @param skuId 商品id      * @param num 数量      * @return      */     @GetMapping("/addItem")     public Result addItem(String skuId,Integer num){         String  username=SecurityContextHolder.getContext().getAuthentication().getName() ;         cartService.addItem(username,skuId,num);         return new Result();     }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马