这里写图片描述

1商品详情页面点击购买到购物车页面

点击购物,刷新购物车页面 流程图
这里写图片描述

代码实现

    // 购买按钮,通过页面cookie获取数据
    @RequestMapping(value = "/shopping/buyCart.shtml")
    public String buyCart(HttpServletRequest request, ModelMap modelMap, Integer skuId, Integer amount,
            Integer buyLimit, Integer productId, HttpServletResponse response) {

        BuyCart buyCart = null;
        // 对象转json,json 转对象
        ObjectMapper om = new ObjectMapper();

        // 去掉json中value=null的key 和value
        om.setSerializationInclusion(Inclusion.NON_NULL);
        // 获取页面cookie信息
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {// 页面有cookie
            for (Cookie cookie : cookies) {
                // cookies 存在购买者cookie
                if (Constans.BUYCART_COOKIE.equals(cookie.getName())) {
                    String value = cookie.getValue();
                    // json 转 对象
                    try {
                        buyCart = om.readValue(value, BuyCart.class);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
        if (buyCart == null) {// 最佳
            buyCart = new BuyCart();
        }

        // skuId  != null:   页面点击购买的请求
        //skuId  == null: 购物车页面刷新情况,不改动cookie,页面展示以前购物信息.
        if (skuId != null) {
            Sku sku = new Sku();
            sku.setId(skuId);

            //buyLimit != null  点击购买的情况
            //buyLimit == null   购物车页面+-    对原有购物车的商品数量进行+-
            //购物车页面+  window.location.href='/shopping/buyCart.shtml?skuId='+skuId+"&amount=1";
            if (buyLimit != null) {
                sku.setSkuUpperLimit(buyLimit);
            }
            // 购物项
            BuyItem item = new BuyItem();
            item.setSku(sku);
            item.setAmount(amount);
            // 购物车
            buyCart.addItem(item);
            //productId != null  点击购买的情况
            //productId == null  购物车页面+-    对原有购物车的商品数量进行+-
            if (productId != null) {
                // 最后一款商品的ID
                buyCart.setProductId(productId);
            }

            // 对象转json,写的过程,json 是只字符串流
            StringWriter out = new StringWriter();

            try {
                om.writeValue(out, buyCart);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // 购物车持久化
            Cookie cookie = new Cookie(Constans.BUYCART_COOKIE, out.toString());

            // 关闭浏览器也要存在
            // 默认-1,代表关闭浏览器就销毁cookie
            // 0 代表马上销毁cookie
            // expiry 设置时间,秒,代表关闭浏览器后还要多少时间才销毁cookie
            cookie.setMaxAge(60 * 60 * 24);// 设置cookie保存一天

            // 路径设置setPath 设置cookie存放目录
            // 请求路径/shopping/buyCart.shtml
            // 默认路径/shopping
            // 如果不设置"/"的后果:
            // 所有页面都能访问根目录下面的cookie。如果cookie的路径为/shopping,那么只有/shopping下的目录或者子目录下(或者/shopping/*.jsp请求页面)的页面或者代码才能访问cookie
            // 如果默认/shopping,用户在前台页面想要查看购物车,因为没有cookie,所以购物车信息为空
            cookie.setPath("/");

            // 发送到页面去
            response.addCookie(cookie);// 节省性能,cookie只装skuID
        }
        // 装满购物车
        for (BuyItem buyItem : buyCart.getItems()) {
            Sku s = skuService.getSkuByKey(buyItem.getSku().getId());
            buyItem.setSku(s);
        }
        modelMap.addAttribute("buyCart", buyCart);

        return "product/cart";
    }

    // 清空购物车
    @RequestMapping(value = "/shopping/clearCart.shtml")
    public String clearCart(HttpServletResponse response) {
        Cookie cookie = new Cookie(Constans.BUYCART_COOKIE, null);
        cookie.setPath("/");
        cookie.setMaxAge(0);
        response.addCookie(cookie);
        return "product/cart";
    }

    // deleteItem删除购物项
    @RequestMapping(value="/shopping/deleteItem.shtml")
    public String deleteItem(HttpServletRequest request,HttpServletResponse response,Integer skuId){
        BuyCart buyCart = null;
        // 对象转json,json 转对象
        ObjectMapper om = new ObjectMapper();
        // 去掉json中value=null的key 和value
        om.setSerializationInclusion(Inclusion.NON_NULL);
        // 获取页面cookie信息
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {// 页面有cookie
            for (Cookie cookie : cookies) {
                // cookies 存在购买者cookie
                if (Constans.BUYCART_COOKIE.equals(cookie.getName())) {
                    String value = cookie.getValue();
                    // json 转 对象
                    try {
                        buyCart = om.readValue(value, BuyCart.class);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
        // sku信息
        if (buyCart != null) {// 购物车页面刷新情况,没有skuid则不添加购物车,如果只是添加购物车,则重新把信息发到cookie,否则购物车页面刷新,不添加cookie
            Sku sku = new Sku();
            sku.setId(skuId);
            // 购物项
            BuyItem item = new BuyItem();
            item.setSku(sku);
            // 购物车
            buyCart.deleteItem(item);

            // 对象转json,写的过程,json 是只字符串流
            StringWriter out = new StringWriter();

            try {
                om.writeValue(out, buyCart);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            // 购物车持久化
            Cookie cookie = new Cookie(Constans.BUYCART_COOKIE, out.toString());

            // 关闭浏览器也要存在
            // 默认-1,代表关闭浏览器就销毁cookie
            // 0 代表马上销毁cookie
            // expiry 设置时间,秒,代表关闭浏览器后还要多少时间才销毁cookie
            cookie.setMaxAge(60 * 60 * 24);// 设置cookie保存一天

            // 路径设置setPath 设置cookie存放目录
            // 请求路径/shopping/buyCart.shtml
            // 默认路径/shopping
            // 如果不设置"/"的后果:
            // 所有页面都能访问根目录下面的cookie。如果cookie的路径为/shopping,那么只有/shopping下的目录或者子目录下(或者/shopping/*.jsp请求页面)的页面或者代码才能访问cookie
            // 如果默认/shopping,用户在前台页面想要查看购物车,因为没有cookie,所以购物车信息为空
            cookie.setPath("/");
            // 发送到页面去
            response.addCookie(cookie);// 节省性能,cookie只装skuID
    }
        return "redirect:/shopping/buyCart.shtml";
}

    //结算
    @RequestMapping(value = "/buyer/trueBuy.shtml")
    public String trueBuy(HttpServletRequest request,HttpServletResponse response,ModelMap modelMap){
        //先从cookie获取购物车信息
        BuyCart buyCart = null;
        // 对象转json,json 转对象
        ObjectMapper om = new ObjectMapper();
        // 去掉json中value=null的key 和value
        om.setSerializationInclusion(Inclusion.NON_NULL);
        // 获取页面cookie信息
        Cookie[] cookies = request.getCookies();
        if (cookies != null && cookies.length > 0) {// 页面有cookie
            for (Cookie cookie : cookies) {
                // cookies 存在购买者cookie
                if (Constans.BUYCART_COOKIE.equals(cookie.getName())) {
                    String value = cookie.getValue();
                    // json 转 对象
                    try {
                        buyCart = om.readValue(value, BuyCart.class);
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }

        //如果购物车时间过长,cookie不存在,cookie存在,获取存在购物车,但不存在购物项,这些情况都去刷新购物车页面
        if(buyCart!=null){
            List<BuyItem> items = buyCart.getItems();//初始购物车长度

            if(items!=null&&items.size()>0){
                //由于时间太久,购物项商品的库存有的可能为0,或者小于购物数量
                int L = items.size();//初始购物车长度
            for (BuyItem buyItem : items) {
                //当前sku的库存
                Sku s = skuService.getSkuByKey(buyItem.getSku().getId());
                //当前sku的库存,小于cookie中的购买数量,把该商品移除
                if(s.getStockInventory()<buyItem.getAmount()){
                    buyCart.deleteItem(buyItem);
                }
            }
            int O = buyCart.getItems().size();//和数据库对比后的购物车长度
            if(L>O){//说明购物车数据有删除
                // 对象转json,写的过程,json 是只字符串流
                StringWriter out = new StringWriter();

                try {
                    om.writeValue(out, buyCart);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                // 购物车持久化
                Cookie cookie = new Cookie(Constans.BUYCART_COOKIE, out.toString());

                // 关闭浏览器也要存在
                // 默认-1,代表关闭浏览器就销毁cookie
                // 0 代表马上销毁cookie
                // expiry 设置时间,秒,代表关闭浏览器后还要多少时间才销毁cookie
                cookie.setMaxAge(60 * 60 * 24);// 设置cookie保存一天

                // 路径设置setPath 设置cookie存放目录
                // 请求路径/shopping/buyCart.shtml
                // 默认路径/shopping
                // 如果不设置"/"的后果:
                // 所有页面都能访问根目录下面的cookie。如果cookie的路径为/shopping,那么只有/shopping下的目录或者子目录下(或者/shopping/*.jsp请求页面)的页面或者代码才能访问cookie
                // 如果默认/shopping,用户在前台页面想要查看购物车,因为没有cookie,所以购物车信息为空
                cookie.setPath("/");

                // 发送到页面去
                response.addCookie(cookie);// 节省性能,cookie只装skuID
                return "/shopping/buyCart.shtml";//刷新购物车页面

            }else{//cookie购物车的数据与数据库的数据一致,跳转结算功能
                //收获地址
                Buyer buyer =(Buyer) sessionProvider.getAttribute(request, Constans.BUYER_SESSION,response);
                AddrQuery addrQuery=new AddrQuery();
                addrQuery.setBuyerId(buyer.getUsername());
                //默认地址
                addrQuery.setIsDef(1);
                List<Addr> addrList = addrService.getAddrList(addrQuery); 
                if(addrList.size()>0){
                    modelMap.addAttribute("addr",addrList.get(0));
                }
                for (BuyItem buyItem : buyCart.getItems()) {
                    Sku s = skuService.getSkuByKey(buyItem.getSku().getId());
                    buyItem.setSku(s);
                }
                modelMap.addAttribute("buyCart", buyCart);
                return "product/productOrder";
            }

            }else{
                return "/shopping/buyCart.shtml";//刷新购物车页面
            }
        }else{
            return "/shopping/buyCart.shtml";//刷新购物车页面
        }
    }
posted on 2017-04-18 22:22  2637282556  阅读(258)  评论(0编辑  收藏  举报