Blame view

public/js/product/detail/brand-coupon.js 3.3 KB
陈轩 authored
1 2 3 4 5
'use strict';

/**
 *  商品详情: 品牌券
 */
lijing authored
6 7 8
let tip = require('plugin/tip');
let $ = require('yoho-jquery');
let $body = $(document.body);
陈轩 authored
9
lijing authored
10
let brandCoupon = {
陈轩 authored
11 12 13 14 15 16 17 18
    skn: null,
    brandId: null,
    $entry: null,
    $couponDrawer: null,

    template: require('product/detail/coupon-list.hbs'),

    init: function(skn, brandId) {
lijing authored
19
        let self = this;
陈轩 authored
20 21 22 23 24 25 26 27

        this.skn = skn;
        this.brandId = brandId;

        if (!(skn && brandId)) {
            return;
        }
沈志敏 authored
28 29 30 31 32 33 34 35 36 37
        setTimeout(() => {
            this.fetchCoupons(this.skn, this.brandId)
                .done(function(data) {
                    if (data.length) {
                        self.render(data);
                        self.domInit();
                        self.bindEvents();

                        self.$entry.removeClass('hide');
                    }
38 39

                    window.rePosFooter && window.rePosFooter();
沈志敏 authored
40 41 42
                })
                .fail();
        }, 200);
陈轩 authored
43 44 45
    },

    domInit: function() {
46
        this.$entry = $('.brand-coupon').removeClass('hide');
陈轩 authored
47 48 49
    },

    bindEvents: function() {
lijing authored
50
        let self = this;
陈轩 authored
51 52

        this.$entry.on('click', function() {
53
            self.toggleDrawer(true);
陈轩 authored
54
        });
55 56 57 58

        this.$couponDrawer
            .on('click', '.coupon-drawer-mask', $.proxy(this.toggleDrawer, this, false))
            .on('click', '.coupon-btn-valid', $.proxy(this.saveCouponHandler, this));
陈轩 authored
59 60 61
    },

    render: function(data) {
62 63 64 65
        this.$couponDrawer = $(this.template({
            coupons: data
        }));
        this.$couponDrawer.appendTo('.good-detail-page');
陈轩 authored
66 67 68 69 70 71 72 73 74 75 76 77

        return this;
    },

    // 获取 品牌券
    fetchCoupons: function(skn, brandId) {
        return $.get('/product/detail/coupon.json', {
            skn: skn,
            brandId: brandId
        });
    },
78 79 80 81 82 83 84 85
    saveCoupon: function(couponId, callback) {
        $.post('/product/detail/coupon/save.json', {
            couponId: couponId
        }).done(function(res) {
            tip.show(res.message);

            if (res.code === 200) {
                callback(); // eslint-disable-line
李靖 authored
86
            } else if (res.code === 9999992) {
李靖 authored
87
                tip.show(res.message);
陈轩 authored
88
            } else {
89 90 91
                tip.show(
                    res.message || '抱歉,您不符合领用条件'
                );
92
陈轩 authored
93 94 95 96 97
                if (res.redirect) {
                    setTimeout(function() {
                        location.href = res.redirect;
                    }, 1000);
                }
98
            }
陈轩 authored
99 100
        }).fail(function() {
            tip.show('网络异常,请稍后再试');
101 102 103
        });
    },
陈轩 authored
104
    // 收藏 品牌券
105
    saveCouponHandler: function(event) {
lijing authored
106 107
        let $btn = $(event.target);
        let couponId = $btn.closest('.coupon').data('coupon');
108 109 110

        this.saveCoupon(couponId, function() {
            $btn.prop('disabled', true)
陈轩 authored
111
                .removeClass('coupon-btn-valid')
112 113 114 115 116
                .text('已领取');
        });

        event.stopPropagation();
    },
陈轩 authored
117
118 119 120
    toggleDrawer: function(bool) {
        this.$couponDrawer.toggleClass('open', bool);
        $body.toggleClass('coupon-drawer-open', bool);
陈轩 authored
121 122 123 124
    }
};

$(function() {
125 126 127 128 129
    if ($('#product-coupon-switch').val() === 'true') {
        brandCoupon.init(
            $('#productSkn').val(),
            $('#brand-id').val()
        );
130
    }
沈志敏 authored
131
});