coupons.page.js 3.71 KB
// 不要使用es6
'use strict';

var $ = require('yoho-jquery'),
    couponsListHbs = require('home/coupons-list.hbs'),
    notResultHbs = require('home/coupon-not-result.hbs');

var $couponTab1 = $('.coupon-tab1'),
    $couponTab2 = $('.coupon-tab2'),
    $couponTab3 = $('.coupon-tab3'),
    $couponUl = $('.coupon-tab'),
    page = 1,
    indexNum,
    couponFun,
    scrollHandler,
    $tabDom;

require('home/coupons.page.css');
require('../common');

$('body').css({'background-color': '#f2f2f2'});

couponFun = function() {
    $tabDom = $couponUl.find('li.activate');

    if (!$tabDom) {
        return false;
    }

    if ($tabDom.data('AjaxFlag')) {
        return false;
    }

    $tabDom.data('AjaxFlag', true);
    page = $tabDom.data('page') || 1;
    indexNum = $tabDom.index() * 1;

    // 首屏渲染page = 2
    if (indexNum === 0 && !$tabDom.data('page')) {
        page = 2;
    }

    $.ajax({
        type: 'POST',
        url: '/home/couponsAjax',
        dataType: 'json',
        data: {
            status: indexNum,
            page: page
        },
        success: function(data) {
            var $activeDom;

            if (indexNum === 1) {
                $activeDom = $couponTab2;
            } else if (indexNum === 2) {
                $activeDom = $couponTab3;
            } else {
                $activeDom = $couponTab1;
            }

            if (page <= 1 && data.length <= 0) {
                $activeDom.html(notResultHbs());
                return false;
            }

            if (data.length <= 0) {
                return false;
            }

            $tabDom.data('page', ++page);
            $tabDom.data('AjaxFlag', false);
            $activeDom.append(couponsListHbs({list: data}));
            window.rePosFooter(); // 重新计算底部位置
        }
    });
};

scrollHandler = function() {
    if ($(window).scrollTop() + $(window).height() > $('body').height() - 100) {
        couponFun();
        return;
    }
};

$(window).scroll(function() {
    window.requestAnimationFrame(scrollHandler);
});

// tab 事件
$couponUl.on('click', 'li', function() {
    $(this).addClass('activate').siblings().removeClass('activate');
    $('.coupon-list').addClass('hide').eq($(this).index()).removeClass('hide');
    couponFun();
    window.rePosFooter(); // 重新计算底部位置
});

// 详细信息事件
$('.coupon-list').on('click', '.info-btn', function() {
    var $this = $(this);

    if ($this.hasClass('down')) {
        $this.removeClass('down').addClass('up');
        $this.closest('.coupon-group').find('.coupon-footer').removeClass('hide');
    } else {
        $this.removeClass('up').addClass('down');
        $this.closest('.coupon-group').find('.coupon-footer').addClass('hide');
    }

    // 优惠券埋点
    window._yas && window._yas.sendCustomInfo && window._yas.sendCustomInfo({
        op: 'YB_COUPON_DETAIL_C',
        param: JSON.stringify({
            C_ID: window._ChannelVary[window.cookie('_Channel')] || 1,
            COUPON_ID: $this.closest('.coupon-group').data('coupon-id')
        })
    }, true);
});

// 立即使用
$('.coupon-list').on('click', 'a.btn', function() {
    var $this = $(this);

    // 优惠券埋点
    window._yas && window._yas.sendCustomInfo && window._yas.sendCustomInfo({
        op: 'YB_COUPON_IMMEDIATE_USE_C',
        param: JSON.stringify({
            C_ID: window._ChannelVary[window.cookie('_Channel')] || 1,
            COUPON_ID: $this.closest('.coupon-group').data('coupon-id')
        })
    }, true);

    document.location.href = $this.data('link');
});

// 首屏渲染无结果判断
if ($('.coupon-tab1').find('.coupon-group').length <= 0) {
    $('.coupon-tab1').html(notResultHbs());
    window.rePosFooter(); // 重新计算底部位置
}