tick.js 11.8 KB
/**
 * [秒抢页面js]
 * author: 陈峰<feng.chen@yoho.cn>
 * date: 2016/09/08
 */

var $ = require('yoho-jquery'),
    tip = require('../plugin/tip');

var seckillObj = {};

var timeObj = '';

var offsetTime = 0;

var nowTime = 0;

var startTime = 0;

var endTime = 0,
    diffTime = 0;// 误差时间

var dateText = 0,
    newDate = 0,
    newMonth = 0,
    newDay = 0,
    newHour = 0,
    newMinus = 0;

var skuFlag,
    hideNum,
    hideIndex;

var oneIndex = 0,
    twoIndex = 0,
    secKillPrice = 0;

require('../common');

seckillObj = {
    el: {
        iScroll: null,
        currentTick: null
    },

    startTick: function(elem, offsetTime) {
        var that = this,
            $el = this.el,
            day = parseInt(offsetTime / (60 * 60 * 24), 10),
            hour = parseInt(offsetTime % (60 * 60 * 24) / (60 * 60), 10),
            minute = parseInt(offsetTime % (60 * 60) / 60, 10),
            second = offsetTime % 60;

        if (offsetTime >= -1) {
            if (day <= 0) {
                $('.day-c').addClass('hide');
            }
            $(elem).find('.tick.day').text(day < 0 ? '00' : (day < 10 ? ('0' + day) : day));
            $(elem).find('.tick.hour').text(hour < 0 ? '00' : (hour < 10 ? ('0' + hour) : hour));
            $(elem).find('.tick.minute').text(minute < 0 ? '00' : (minute < 10 ? ('0' + minute) : minute));
            $(elem).find('.tick.second').text(second < 0 ? '00' : (second < 10 ? ('0' + second) : second));

            if (offsetTime <= -1) { // 结束倒计时刷新状态
                window.location.reload();
            } else {
                $el.currentTick = setTimeout(function() {
                    that.startTick(elem, --offsetTime);
                }, 1000);
            }
        }

    }
};

$(
    function() {
        $('#goodsDiscount').hide();// 隐藏折扣楼层
        $('.cart-bar').hide();
        $('.current-price').hide();
        var ajaxUrl = '/product/seckillDetail/seckillData/' + $('#productSkn').val();
        var timestamp = Date.parse(new Date());

        $.ajax({
            type: 'GET',
            url: ajaxUrl + '?tamp=' + timestamp,
            success: function(data) {
                // 秒杀是否结束
                if (data == '' || data.status === 0 || data.status === 3) {
                    window.location.replace('/product/show_' + $('#productSkn').val() + '.html');

                    // $('.sold-out').hide();
                    // $('.cart-bar a:first').append('<a href="javascript:;" class="sold-out">已售罄</a>');
                    // $('.addto-cart').hide();
                    // $('.cart-bar').show();
                    // $('.current-price').show();
                } else {

                    getMarketPrice();// 获取市场价
                    // 秒杀开始前
                    if (data.status === 1) {
                        $('.cart-bar').before(
                            '<div class="seckill-count">' +
                                '<div class="seckill-count-bg"></div>' +
                                '<div class="seckill-count-num">距秒杀开始:' +
                                    '<span class="day-c"><i class="tick day">00</i>天</span>' +
                                    '<i class="tick hour">00</i>时' +
                                    '<i class="tick minute">00</i>分' +
                                    '<i class="tick second">00</i>秒' +
                                '</div>' +
                            '</div>'
                        );
                        $('.current-price').text('¥' + data.secKillPrice).show();
                        $('.price-date').append(
                            '<div class="seckill-time notStart">' +
                                '<span class="seckill-time-pic">秒杀预告</span>' +
                                '<span class="seckill-time-c">月日</span>' +
                            '</div>'
                        );
                        $('.sold-out').hide();
                        $('.cart-bar a:first').after('<a href="javascript:;" class="sold-out">即将开抢</a>');
                        $('.addto-cart').hide();
                        $('.cart-bar').show();
                        $('.current-price').show();
                    }

                    // 秒杀进行中
                    if (data.status === 2) {
                        $('.chose-panel .size-list').find('li[data-skuid != \'\']').each(
                            function() {
                                skuFlag = 0;
                                for (var i = 0; i < data.secKillSku.length; i++) {
                                    if (data.secKillSku[i].productSku === Number($(this).attr('data-skuid'))) {
                                        skuFlag++;

                                        // 如果秒杀库存为0
                                        if (data.secKillSku[i].storageNum === 0) {
                                            $(this).addClass('zero-stock');
                                            oneIndex = $(this).index() + 1;
                                            twoIndex = $(this).parent('ul').index() - 2;
                                            $('.chose-panel .color-list ul:eq(' + oneIndex + ') li:eq(' + twoIndex + ')').addClass('zero-stock');
                                        }

                                    } else {

                                        // alert("没有匹配");
                                    }
                                }
                                if (skuFlag === 0) {
                                    $(this).addClass('zero-stock').attr('data-num', '0').addClass('hide');
                                    oneIndex = $(this).index() + 1;
                                    twoIndex = $(this).parent('ul').index() - 2;

                                    $('.chose-panel .color-list ul:eq(' + oneIndex + ') li:eq(' + twoIndex + ')').addClass('zero-stock').addClass('hide');
                                }
                            }
                        );

                        seckHide();// 非秒杀库存置灰

                        // 插入倒计时
                        addTimeout($('.price-date'));
                        addTimeout($('.text-info'));
                        secKillPrice = toDecimal2(data.secKillPrice);
                        $('.current-price').text('¥' + secKillPrice).show();
                        $('.sale-price').text('¥' + secKillPrice).show();
                        $('.chose-items .num').find('.clearfix').append(
                            '<span class="limit-num-text">限购1件</span>'
                        );
                        $('.left-num').hide();
                        $('.btn-plus').removeClass('btn-plus');
                        $('.addto-cart').text('立即购买');
                        $('#chose-btn-sure').text('立即购买').addClass('isSecKill');
                        $('.cart-bar').show();
                        var seckillNum = 0;

                        for (var i = 0; i < data.secKillSku.length; i++) {
                            seckillNum = seckillNum + data.secKillSku[i].storageNum;
                        }
                        if (seckillNum === 0) {
                            $('.sold-out').hide();
                            $('.cart-bar a:first').append('<a href="javascript:;" class="sold-out">已售罄</a>');
                            $('.addto-cart').hide();
                        }
                        $('.current-price').show();
                    }

                    startTime = data.startTime;

                    endTime = data.endTime;

                    diffTime = data.currentTime;

                    // nowTime = Date.parse(new Date()) / 1000;

                    nowTime = diffTime;// 使用请求服务器时间作为当前时间

                    if (startTime > nowTime) {
                        offsetTime = startTime - nowTime;
                        timeObj = $('.seckill-count-num');
                    } else if (nowTime > startTime && nowTime < endTime) {
                        offsetTime = endTime - nowTime;
                        timeObj = $('.end-time');
                    }

                    dateText = Number(data.startTime * 1000);
                    newDate = new Date(dateText);
                    newMonth = newDate.getMonth() + 1;
                    newDay = newDate.getDate();
                    newHour = newDate.getHours();
                    newMinus = newDate.getMinutes();
                    $('.notStart').find('.seckill-time-c').text(newMonth + '月' + newDay + '日' + newHour + ':' + newMinus);
                    seckillObj.startTick(timeObj, offsetTime);
                }
            },
            error: function() {
                tip.show('网络异常~');
            }
        });

    }
);

function seckHide() {
    $('.chose-panel .size-list ul').each(
        function() {
            hideNum = 0;
            $(this).find('li').each(
                function() {
                    if ($(this).hasClass('hide')) {
                        oneIndex = $(this).index() + 1;
                        twoIndex = $(this).parent('ul').index() - 2;
                        $('.color-list ul:eq(' + oneIndex + ') li:eq(' + twoIndex + ')').hide();
                    } else {
                        hideNum++;
                    }
                }
            );
            if (hideNum === 0) {
                hideIndex = $(this).index() - 2;
                $('.color-list ul:first li:eq(' + hideIndex + ')').hide();
            }
        }
    );

    $('.chose-panel .color-list ul').each(
        function() {
            hideNum = 0;
            $(this).find('li').each(
                function() {
                    if ($(this).hasClass('hide')) {
                        oneIndex = $(this).index() + 1;
                        twoIndex = $(this).parent('ul').index() - 2;
                        $('.size-list ul:eq(' + oneIndex + ') li:eq(' + twoIndex + ')').hide();
                    } else {
                        hideNum++;
                    }
                }
            );
            if (hideNum === 0) {
                hideIndex = $(this).index() - 2;
                $('.size-list ul:first li:eq(' + hideIndex + ')').hide();
            }
        }
    );
}

function getMarketPrice() {
    var productId = $('#productId').val();
    var goodsId = $('#goodsId').val();
    var productSkn = $('#productSkn').val();

    if (productId && (goodsId || productSkn)) {
        $.ajax({
            type: 'POST',
            url: '/product/detail/info',
            data: {
                id: productId,
                goodsId: goodsId,
                productSkn: productSkn
            },
            success: function(data) {
                if (data.goodsPrice.previousPrice) {
                    $('.previous-price').text(data.goodsPrice.previousPrice);
                } else {
                    $('.previous-price').text(data.goodsPrice.currentPrice);
                }

            }
        });
    }
}

function addTimeout(obj) {
    obj.append(
        '<div class="seckill-time seckill-time-border">' +
            '<span>距结束&nbsp</span>' +
            '<span class="end-time">' +
                '<i class="tick day hide">00</i>' +
                '<i class="tick hour">00</i>:' +
                '<i class="tick minute">00</i>:' +
                '<i class="tick second">00</i>' +
            '</span>' +
        '</div>'
    );
}

// 强制保留2位小数点
function toDecimal2(num) {
    var f = parseFloat(num);
    if (isNaN(f)) {
        return false;
    }
    var f = Math.round(num * 100) / 100;
    var s = f.toString();
    var rs = s.indexOf('.');
    if (rs < 0) {
        rs = s.length;
        s += '.';
    }
    while (s.length <= rs + 2) {
        s += '0';
    }
    return s;
}