coupon.js 4.16 KB
var $ = require('yoho-jquery'),
    Swiper = require('yoho-swiper'),
    tip = require('../../plugin/tip');

require('../../common');

var shopCoupon = {};

shopCoupon = {
    common: {
        appVersion: window.queryString.app_version || window.queryString.appVersion,
        uid: window.queryString.uid,
        shopId: parseInt($('.shop-id').val(), 10) || null,
        brandId: parseInt($('.brand-header').data('id'), 10) || null,
        couponTemplate: require('product/shop/coupon.hbs')
    },
    init: function() {
        // 如果都为空,则不请求
        if (!this.common.shopId && !this.common.brandId) {
            return true;
        }

        this.loginCoupon();
        this.getShopCouponsList();
    },
    getShopCouponsList: function() {
        var that = this,
            _url = location.protocol + '//m.yohobuy.com';

        if (that.common.shopId) {
            // 店铺领券
            _url += '/product/index/getShopCouponsList';
        } else {
            // 品牌领券
            _url += '/product/index/getBrandCouponsList';
        }

        $.ajax({
            method: 'GET',
            url: _url,
            async: false,
            data: {
                shopId: that.common.shopId,
                brandId: that.common.brandId,
                uid: that.common.uid
            },
            xhrFields: {
                withCredentials: true
            },
            success: function(result) {
                $('.coupon-group').html(that.common.couponTemplate(result));
                new Swiper('.coupon-content', {
                    slideElement: '.coupon-small',
                    slidesPerView: 'auto',
                    watchSlidesVisibility: true
                });
                that.userCoupon();
            }
        });
    },
    userCoupon: function() {
        var $self = this;

        $('.coupon-content .receive-btn').closest('.swiper-slide').on('click', function() {
            var that = this,
                code = $(this).data('id') || '';

            if (that.isCouponClick === false) {
                return false;
            }

            that.isCouponClick = false;

            $.ajax({
                method: 'POST',
                url: '/product/index/userCoupon',
                data: {
                    appVersion: $self.common.appVersion,
                    couponID: code,
                    uid: $self.common.uid
                },
                xhrFields: {
                    withCredentials: true
                },
                success: function(data) {

                    that.isCouponClick = true;

                    if (data.code === 200) {
                        tip.show('领取成功');
                        $(that).find('.receive-btn').html('已领取');
                        return true;
                    } else if (data.code === 4401) {
                        $('#shop-login').remove();
                        $('body').append('<a href=\'' + data.url + '\'><span id="shop-login"><span></a>');
                        $('#shop-login').click();

                        // 未登录状态下
                        window.setCookie('shopBrandCoupon', code);
                    } else if (data.code === 401) {
                        tip.show('已领取过');
                    } else {
                        tip.show(data.message);
                    }
                },
                error: function() {
                    tip.show('网络断开连接了~');
                    that.isCouponClick = true;
                }
            });
        });
    },

    // 登录状态下,领取优惠券
    loginCoupon: function() {
        var $self = this,
            code = window.cookie('shopBrandCoupon') || false;

        window.setCookie('shopBrandCoupon', null);
        if (!code) {
            return false;
        }

        $.ajax({
            method: 'POST',
            url: '/product/index/userCoupon',
            async: false,
            data: {
                couponID: code,
                uid: $self.common.uid
            },
            xhrFields: {
                withCredentials: true
            }
        });
    }
};

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