coupon.js 2.59 KB
/**
 * Created by Administrator on 2016/4/13.
 */
var $ = require('jquery'),
    Swiper = require('yoho.iswiper'),
    $receive = $('.main-right-receive'),
    $mask = $('.floor-mask'),
    $message = $('.floor-message'),
    $tooltip = $('.floor-tooltip'),
    tip = require('../plugin/tip');

var bannerSwiper;

// 获取url中的参数
function getUrlParam(name) {

    // 构造一个含有目标参数的正则表达式对象
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');

    // 匹配目标参数
    var r = window.location.search.substr(1).match(reg);

    // 返回参数值
    if (r != null) {
        return r[2];
    }

    return null;
}

$receive.on('click', function() {
    var $curDom = $(this);

    $.ajax({
        url: '/coupon/receiveCoupon',
        data: {
            couponID: $curDom.parents('.coupon-floor').attr('coupon-id')
        },
        dataType: 'json',
        success: function(data) {
            var msg = data.msg,
                status = data.status,
                newUrl;

            if (data.noLogin === true) {
                newUrl = data.url + '?code=' + getUrlParam('code');

                if ($('#intimacy-link').length <= 0) {
                    $('body').append('<a href=\'' + newUrl + '\' style="display:none;" id="intimacy-link">' +
                        '<span class="intimacy-link"></span></a>');
                }

                $('.intimacy-link').click();
            } else {
                if (status) {
                    $curDom.hide();
                    $curDom.next().show();
                    $tooltip.show();

                    setTimeout(function() {
                        $tooltip.hide();
                    }, 3000);
                } else {
                    $message.find('.coupon-message-content').text(msg);
                    $mask.show();
                    $message.show();
                }
            }
        },
        error: function() {
            tip.show('网络异常!');
        }
    });
});

$('.coupon-floor a, .banner-top a').on('click', function() {
    if ($(this).attr('href').length <= 0 || $(this).attr('href') === '#') {
        return false;
    }
});

if ($('.banner-swiper').find('li').size() > 1) {
    bannerSwiper = new Swiper('.banner-swiper', {
        lazyLoading: true,
        lazyLoadingInPrevNext: true,
        loop: true,
        autoplay: 3000,
        autoplayDisableOnInteraction: false,
        paginationClickable: true,
        slideElement: 'li',
        pagination: '.banner-top .pagination-inner'
    });
}

$('.coupon-message-op-rel').on('click', function() {
    location.reload();
});