Blame view

public/js/cart/index/bundle.js 4.86 KB
郭成尧 authored
1 2 3 4
/*
 * @Author: Targaryen
 * @Date: 2017-04-06 16:51:52
 * @Last Modified by: Targaryen
郭成尧 authored
5
 * @Last Modified time: 2017-04-14 18:32:07
郭成尧 authored
6 7 8 9 10
 */

const $ = require('yoho-jquery');

let tip = require('plugin/tip');
郭成尧 authored
11
let loading = require('plugin/loading');
郭成尧 authored
12 13 14 15 16

let bundle = {
    init(handle) {
        let self = this;
郭成尧 authored
17
        let $bundle = $('.bundle'); // 限定作用域
郭成尧 authored
18 19 20 21 22 23

        self.handle = handle;

        /**
         * 减少套餐数量
         */
郭成尧 authored
24
        $bundle.on('click', '.bundle-nums .num-opt .btn-opt-minus', function(e) {
郭成尧 authored
25 26 27 28 29 30
            let skuIdsArray = [];

            $(e.delegateTarget).find('.good-item').each(function() {
                skuIdsArray.push($(this).data('id'));
            });
郭成尧 authored
31
            self.decrBundle({
郭成尧 authored
32 33
                activity_id: $(e.delegateTarget).data('activityid'),
                batch_no: $(e.delegateTarget).data('poolbatchno'),
郭成尧 authored
34
                sku_ids: skuIdsArray.join(',')
郭成尧 authored
35 36 37 38 39 40
            }, e);
        });

        /**
         * 增加套餐数量
         */
郭成尧 authored
41
        $bundle.on('click', '.bundle-nums .num-opt .btn-opt-plus', function(e) {
郭成尧 authored
42 43 44 45 46 47
            let skuIdsArray = [];

            $(e.delegateTarget).find('.good-item').each(function() {
                skuIdsArray.push($(this).data('id'));
            });
郭成尧 authored
48 49 50 51 52
            if ($(e.delegateTarget).data('buynum') >= $(e.delegateTarget).data('maxnum')) {
                tip.show('库存不足');
                return false;
            }
郭成尧 authored
53
            self.incrBundle({
郭成尧 authored
54 55
                activity_id: $(e.delegateTarget).data('activityid'),
                batch_no: $(e.delegateTarget).data('poolbatchno'),
郭成尧 authored
56
                sku_ids: skuIdsArray.join(',')
郭成尧 authored
57 58
            }, e);
        });
郭成尧 authored
59
郭成尧 authored
60 61 62
        /**
         * 套餐选中/取消
         */
郭成尧 authored
63 64 65
        $bundle.on('click', '.bundle-title .opt .select', function(e) {
            self.bundleCheckHandle(e);
        });
郭成尧 authored
66 67 68 69 70 71 72

        /**
         * 编辑状态下套餐选中/取消
         */
        $bundle.on('click', '.bundle-title .opt .edit', function(e) {
            self.bundleEditCheckHandle(e);
        });
郭成尧 authored
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    },

    /**
     * 增加套餐数量
     * @param {*} params
     */
    incrBundle(params, e) {
        let self = this;

        $.post('/cart/index/new/incrbundle', params, function(result) {
            let bundleNum = $(e.delegateTarget).find('.good-num');
            let defaultValue = parseInt(bundleNum.val(), 10);

            if (result.code === 200) {
                bundleNum.val(defaultValue + 1);
                self.handle.refreshPage();
            } else {
                tip.show(result.message);
            }
        });
    },
毕凯 authored
95
    /**
郭成尧 authored
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
     * 减少套餐数量
     * @param {*} params
     */
    decrBundle(params, e) {
        let self = this;

        $.post('/cart/index/new/decrbundle', params, function(result) {
            let bundleNum = $(e.delegateTarget).find('.good-num');
            let defaultValue = parseInt(bundleNum.val(), 10);

            if (result.code === 200) {
                bundleNum.val(defaultValue - 1);
                self.handle.refreshPage();
            } else {
                tip.show(result.message);
            }
        });
    },
郭成尧 authored
114 115 116 117 118 119

    /**
     * 套餐选中
     * @param {*} e
     */
    bundleCheckHandle(e) {
郭成尧 authored
120
        let self = this;
郭成尧 authored
121
        let selectCheck = $(e.delegateTarget).find('.select');
郭成尧 authored
122 123
        let goodItem = $(e.delegateTarget).find('.good-item');
        let skuData = [];
郭成尧 authored
124 125 126 127 128
        let selected = 'Y';

        if (selectCheck.hasClass('checked')) {
            selected = 'N';
        }
郭成尧 authored
129 130 131 132

        goodItem.each(function(index, element) {
            skuData.push({
                product_sku: $(element).data('id'),
郭成尧 authored
133
                buy_number: $(e.delegateTarget).data('buynum'),
郭成尧 authored
134
                activity_id: $(e.delegateTarget).data('activityid'),
郭成尧 authored
135
                batch_no: $(e.delegateTarget).data('poolbatchno'),
郭成尧 authored
136
                selected: selected
郭成尧 authored
137 138
            });
        });
郭成尧 authored
139
        loading.showLoading();
郭成尧 authored
140 141 142 143 144 145 146
        $.ajax({
            type: 'post',
            url: '/cart/index/new/select',
            data: {
                skuList: JSON.stringify(skuData)
            },
            success: function(result) {
郭成尧 authored
147 148
                if (result.code === 200) {
                    selectCheck.toggleClass('checked');
郭成尧 authored
149
                    self.handle.refreshPage(result.data);
郭成尧 authored
150 151
                }
                loading.hideLoading();
郭成尧 authored
152 153
            }
        });
郭成尧 authored
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    },

    /**
     * 编辑状态下套餐选中/取消
     * @param {*} e
     */
    bundleEditCheckHandle(e) {
        let editCheck = $(e.delegateTarget).find('.edit');
        let goodItem = $(e.delegateTarget).find('.good-item');

        if (editCheck.hasClass('checked')) {
            editCheck.removeClass('checked');
            goodItem.find('.opt .edit').removeClass('checked');
        } else {
            editCheck.addClass('checked');
            goodItem.find('.opt .edit').addClass('checked');
        }
    },
郭成尧 authored
172 173 174
};

module.exports = bundle;