egg(95下)--egg之购物车之异步改变数量

router.js

    router.get('/incCart', initMiddleware, controller.default.cart.incCart);

    router.get('/decCart', initMiddleware, controller.default.cart.decCart);

controller

app/controller/default/cart.js

增加

    async incCart() {

        var goods_id = this.ctx.request.query.goods_id;
        var color = this.ctx.request.query.color;

        var goodsResult = await this.ctx.model.Goods.find({ "_id": goods_id });
        if (goodsResult.length == 0) {
            this.ctx.body = {
                "success": false,
                msg: '修改数量失败'
            }
        } else {

            var cartList = this.service.cookies.get('cartList');
            var currentNum = 0; //当前数量
            var allPrice = 0; //总价格
            for (var i = 0; i < cartList.length; i++) {
                if (cartList[i]._id == goods_id && cartList[i].color == color) {
                    cartList[i].num += 1;
                    currentNum = cartList[i].num;

                }
                if (cartList[i].checked) {
                    allPrice += cartList[i].price * cartList[i].num;
                }
            }
            this.service.cookies.set('cartList', cartList);


            this.ctx.body = {
                "success": true,
                num: currentNum,
                allPrice: allPrice
            }
        }

    }

减少

    async decCart() {

        var goods_id = this.ctx.request.query.goods_id;

        var color = this.ctx.request.query.color;

        var goodsResult = await this.ctx.model.Goods.find({ "_id": goods_id });
        if (goodsResult.length == 0) {
            this.ctx.body = {
                "success": false,
                msg: '修改数量失败'
            }
        } else {

            var cartList = this.service.cookies.get('cartList');
            var currentNum = 0; //当前数量
            var allPrice = 0; //总价格
            for (var i = 0; i < cartList.length; i++) {
                if (cartList[i]._id == goods_id && cartList[i].color == color) {
                    if (cartList[i].num > 1) {
                        cartList[i].num -= 1;
                    }
                    currentNum = cartList[i].num;

                }

                if (cartList[i].checked) {
                    allPrice += cartList[i].price * cartList[i].num;
                }
            }
            this.service.cookies.set('cartList', cartList);


            this.ctx.body = {
                "success": true,
                num: currentNum,
                allPrice: allPrice
            }
        }



    }

view

app/view/default/cart.html
                            <div class="cart_number">
                                <div class="input_left decCart" goods_id="<%=cartList[i]._id%>" color="<%=cartList[i].color%>">-</div>

                                <div class="input_center">
                                    <input id="num" name="num" readonly="readonly" type="text" value="<%=cartList[i].num%>" />
                                </div>
                                <div class="input_right incCart" goods_id="<%=cartList[i]._id%>" color="<%=cartList[i].color%>">+</div>
                            </div>

前端js

app/public/default/js/cart.js
(function($) {

    var app = {
        init: function() {
            this.initCheckBox();

            this.changeCartNum();
        },
        initCheckBox() {
            $("#checkAll").click(function() {
                if (this.checked) {
                    $(":checkbox").prop("checked", true);
                } else {
                    $(":checkbox").prop("checked", false);
                }
            });



            $(".cart_list input:checkbox").click(function() {
                var chknum = $(".cart_list input:checkbox").size(); //checkbox总个数


                var chk = 0; //checkbox checked=true总个数
                $(".cart_list input:checkbox").each(function() {
                    if ($(this).prop("checked") == true) {
                        chk++;
                    }
                });
                if (chknum == chk) { //全选
                    $("#checkAll").prop("checked", true);
                } else { //不全选
                    $("#checkAll").prop("checked", false);
                }
            });
        },
        changeCartNum() {

            $('.decCart').click(function() {


                var goods_id = $(this).attr('goods_id');


                var color = $(this).attr('color');

                // alert(color);

                $.get('/decCart?goods_id=' + goods_id + '&color=' + color, function(response) {
                    console.log(response);


                    if (response.success) {

                        $("#allPrice").html(response.allPrice + '元');

                        $(this).siblings('.input_center').find('input').val(response.num);


                        var price = parseFloat($(this).parent().parent().siblings('.price').html());

                        $(this).parent().parent().siblings('.totalPrice').html(response.num * price + "元");
                    }
                }.bind(this)); //注意this指向

            })



            $('.incCart').click(function() {


                var goods_id = $(this).attr('goods_id');

                var color = $(this).attr('color');

                $.get('/incCart?goods_id=' + goods_id + '&color=' + color, function(response) {

                    if (response.success) {


                        $("#allPrice").html(response.allPrice + '元');

                        $(this).siblings('.input_center').find('input').val(response.num);


                        var price = parseFloat($(this).parent().parent().siblings('.price').html());

                        $(this).parent().parent().siblings('.totalPrice').html(response.num * price + '元');
                    }
                }.bind(this));

            })



        }


    }



    $(function() {
        app.init();
    })

})($)

效果

clipboard.png

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值