cart.js 5.7 KB
Newer Older
D
v1.2.0  
devil_gong 已提交
1 2
$(function()
{
D
devil 已提交
3 4 5 6 7 8 9 10
    /**
     * 计算选择的商品总数和总价
     * @author  Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2020-03-21
     * @desc    description
     */
D
Devil 已提交
11
    function CartBaseTotal()
D
v1.2.0  
devil_gong 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    {
        var total_stock = 0;
        var total_price = 0.00;
        var ids = [];
        $('.am-table input[type="checkbox"]').each(function(k, v)
        {
            if($(this).prop('checked'))
            {
                var stock = parseInt($(this).parents('tr').find('.stock-tag input').val());
                var price = parseFloat($(this).parents('tr').find('.stock-tag').data('price'));
                total_stock += stock;
                total_price += stock*price;
                ids.push($(this).val());
            }
        });
D
devil 已提交
27
        ids = ids.toString() || 0;
D
v1.2.0  
devil_gong 已提交
28
        $('.cart-nav .selected-tips strong').text(total_stock);
D
devil 已提交
29
        $('.cart-nav .nav-total-price').text(__currency_symbol__+FomatFloat(total_price));
D
v1.2.0  
devil_gong 已提交
30
        $('.cart-nav input[name="ids"]').val(ids.toString() || 0);
D
devil 已提交
31
        $('.cart-nav .nav-delete-submit').attr('data-id', ids);
D
v1.2.0  
devil_gong 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    }

    /**
     * 购物车数量更新
     * @author   Devil
     * @blog    http://gong.gg/
     * @version 1.0.0
     * @date    2018-10-11
     * @desc    description
     * @param   {[object]}        self  [氮气罐对象]
     * @param   {[int]}           stock [数量]
     */
    function CardNumberUpdate(self, stock)
    {
        var id = parseInt(self.parents('tr').data('id'));
        var goods_id = parseInt(self.parents('tr').data('goods-id'));
        var inventory = parseInt(self.parents('.stock-tag').data('inventory'));
        var price = parseFloat(self.parents('.stock-tag').data('price'));
        var type = self.data('type');

        if(stock > inventory)
        {
            stock = inventory;
        }
        if(stock <= 0)
        {
            stock = 1;
        }

        // 开启进度条
        $.AMUI.progress.start();

        // ajax请求
        $.ajax({
            url: self.parents('.stock-tag').data('ajax-url'),
            type: 'post',
            dataType: "json",
            timeout: 10000,
            data: {"id": id, "goods_id": goods_id, "stock": stock},
            success: function(result)
            {
                $.AMUI.progress.done();
                if(result.code == 0)
                {
D
devil 已提交
76
                    self.parents('.stock-tag').find('input').val(stock);
D
devil 已提交
77
                    self.parents('tr').find('.total-price-content').text(__currency_symbol__+FomatFloat(stock*price, 2));
D
devil 已提交
78
                    
D
devil 已提交
79
                    Prompt(result.msg, 'success');
D
v1.2.0  
devil_gong 已提交
80 81 82 83 84

                    // 数量更新
                    self.parents('tr').find('.wap-number').text('x'+stock);

                    // 计算选择的商品总数和总价
D
Devil 已提交
85
                    CartBaseTotal();
D
v1.2.0  
devil_gong 已提交
86
                } else {
D
devil 已提交
87
                    Prompt(result.msg);
D
v1.2.0  
devil_gong 已提交
88 89 90 91 92
                }
            },
            error: function(xhr, type)
            {
                $.AMUI.progress.done();
D
Devil 已提交
93
                Prompt(HtmlToString(xhr.responseText) || '异常错误', 'danger', 30);
D
v1.2.0  
devil_gong 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106
            }
        });
    }

    // 购物车数量操作
    $('.stock-tag .stock-submit').on('click', function()
    {
        var stock = parseInt($(this).parents('.stock-tag').find('input').val());
        var type = $(this).data('type');
        var temp_stock = (type == 'add') ? stock+1 : stock-1;
        CardNumberUpdate($(this), temp_stock);
    });

D
devil 已提交
107
    // 输入事件
D
v1.2.0  
devil_gong 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121
    $('.stock-tag input[type="number"]').on('blur', function()
    {
        var stock = $(this).val() || null;
        if(stock == null)
        {
            stock = 1;
        }
        $(this).val(stock);
        CardNumberUpdate($(this), stock);
    });

    // 全选/反选
    $('.select-all-event').on('click', function()
    {
D
devil_gong 已提交
122
        if($(this).find('input').is(':checked'))
D
v1.2.0  
devil_gong 已提交
123
        {
D
devil_gong 已提交
124
            $(this).find('span.el-text').text('反选');
D
Devil 已提交
125
            $('.am-table').find('input[type="checkbox"]').not(':disabled').uCheck('check');
D
v1.2.0  
devil_gong 已提交
126
        } else {
D
devil_gong 已提交
127
            $(this).find('span.el-text').text('全选');
D
Devil 已提交
128
            $('.am-table').find('input[type="checkbox"]').not(':disabled').uCheck('uncheck');
D
v1.2.0  
devil_gong 已提交
129 130 131
        }

        // 计算选择的商品总数和总价
D
Devil 已提交
132
        CartBaseTotal();
D
v1.2.0  
devil_gong 已提交
133 134 135 136 137 138
    });

    // 选择
    $('.am-table input[type="checkbox"]').on('click', function()
    {
        // 计算选择的商品总数和总价
D
Devil 已提交
139
        CartBaseTotal();
D
v1.2.0  
devil_gong 已提交
140 141 142 143
    });

    // 导航固定
    var nav_top = $('.cart-nav').length > 0 ? $('.cart-nav').offset().top : 0;
D
Devil 已提交
144
    function CartNavPop()
D
v1.2.0  
devil_gong 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157
    {
        var scroll = $(document).scrollTop();
        var location = scroll+$(window).height()-100;
        var bottom = ($(window).width() < 640) ? '49px' : '0';
        if(location < nav_top)
        {
            $('.cart-nav').css({"position":"fixed", "bottom":bottom, "width":$('.cart-content').width()+"px", "z-index":1000});
            $('body').css({"padding-bottom":"50px"});
        } else {
            $('.cart-nav').css({"position":"relative", "bottom":0, "z-index":0, "width":"100%"});
            $('body').css({"padding-bottom":"0"});
        }
    }
D
Devil 已提交
158
    CartNavPop();
D
v1.2.0  
devil_gong 已提交
159 160
    $(window).scroll(function()
    {
D
Devil 已提交
161
        CartNavPop();
D
v1.2.0  
devil_gong 已提交
162 163 164 165 166 167
    });

    // 浏览器窗口实时事件
    $(window).resize(function()
    {
        // 导航固定初始化
D
Devil 已提交
168
        CartNavPop();
D
v1.2.0  
devil_gong 已提交
169 170 171 172 173 174
    });

    // 结算事件
    $('.separate-submit').on('click', function()
    {
        // 计算选择的商品总数和总价
D
Devil 已提交
175
        CartBaseTotal();
D
v1.2.0  
devil_gong 已提交
176 177 178 179 180

        // 获取购物车id
        var ids = $(this).parents('form').find('input[name="ids"]').val() || 0;
        if(ids == 0)
        {
D
devil 已提交
181
            Prompt('请选择商品');
D
v1.2.0  
devil_gong 已提交
182 183 184 185 186
            return false;
        }
    });

});