sale.page.js 3.24 KB
/**
 * sale页面
 * @author: da<abel.wang@yoho.cn>
 * @date: 2016/05/31
 */
var $ = require('yoho-jquery'),
    lazyLoad = require('yoho-jquery-lazyload'),
    product = require('./index/product');

var limitTime = [];

var $navItem = $('.sale-nav li');

var htmlCache = {};

require('../common');
require('./sale/banner');
require('../plugins/slider');
require('../plugins/filter');
require('../plugins/sort-pager');
require('./detail/latest-walk');

product.init(4);

lazyLoad($('img.lazy'));
$('.slide-container').slider();

// 请求带导航的列表数据
function getNaviData(items, refresh) {
    var location, url, goodItems, navTitle;

    navTitle = items.attr('urlLocation');

    location = '/product/sale/goods';

    goodItems = items.parent().parent('.sale-box').find('.goods');

    url = location + navTitle;

    if (refresh) {
        url += '&refresh=' + true;
    }

    // 本地有缓存数据,则取本地数据
    if (htmlCache[url]) {
        goodItems.html(htmlCache[url]);
        lazyLoad(items.closest('.sale-box').find('.lazy'));
        return;
    }

    $.ajax({
        type: 'GET',
        url: url,
        data: ''
    }).then(function(res) {
        if (res) {
            htmlCache[url] = res;
            goodItems.html(res);
            lazyLoad(items.closest('.sale-box').find('.lazy'));
        }
    });
}

$navItem.on('click', function() {
    $(this).siblings('.active').removeClass('active');
    $(this).addClass('active');

    getNaviData($(this));
});

//  初始化页面数据
(function() {
    if (window.cookie('_UID')) {
        getNaviData($('.sale-vip-box .sale-nav li').eq(0), true);
    }
}());

function convertTime(time) {
    var anHour = 3600000,
        aMinute = 60000,
        aSecond = 1000,
        showTime = '',
        d = 0,
        h = 0,
        m = 0,
        s = 0;

    if (time > 1000) {
        d = Math.floor(time / anHour / 24);
        h = Math.floor(time / anHour % 24);
        m = Math.floor(time / aMinute % 60);
        s = Math.floor(time / aSecond % 60);

        showTime += d > 0 ? d + '天' : '';
        showTime += h > 0 ? h + '时' : '';
        showTime += m > 0 ? m + '分' : '';
        showTime += s > 0 ? s + '秒' : '';
    }

    return showTime;
}

function setPageTime() {
    var i, text, showTime;

    for (i = 0; i < limitTime.length; i++) {
        if (limitTime[i].time < 0) {
            $('.time').addClass('hide');
            return;
        }

        limitTime[i].time -= 1000;
        showTime = convertTime(limitTime[i].time);
        if (showTime) {
            text = showTime;
            if (limitTime[i].pre) {
                text = limitTime[i].pre + ' ' + text;
            }

            $('.time-calculate').removeClass('hide');
            $('.time-calculate').text(showTime);
        } else {
            text = '活动已结束';
            $('.time').addClass('hide');
        }
    }
    if (limitTime.length === 0) {
        $('.time').addClass('hide');
    }
}

if ($('.sale-discount-page').length > 0) {

    $('.time-calculate').each(function() {
        var data = $(this).attr('data');

        if (data) {
            limitTime.push({
                dom: $(this),
                time: data
            });
        }
    });

    setInterval(setPageTime, 1000);
}