recommend-for-you-product-desc.js 2.29 KB
/**
 * 为您优选
 * @author: liangzhifeng<zhifeng.liang@yoho.cn>
 * @date: 2015/11/16
 */


var lazyLoad = require('yoho-jquery-lazyload'),
    $ = require('yoho-jquery');

var $recommendForYou = $('.recommend-for-you'),
    preferenceUrl = $('#preferenceUrl').val(),
    winH = $(window).height(),
    end = false,
    requesting = false;

var RECID = (new Date().getTime() + '_H5_YOHOBUY_' + Math.floor(Math.random() * 1000000 + 1000000) +
    '_' + Math.floor(Math.random() * 1000000 + 1000000));

var C_ID, goodid, goodIds = [], prdLoad;

C_ID = window._ChannelVary[window.cookie('_Channel')];

function yasRequest(PRD_ID, PRD_NUM, ACTION_ID) {
    if (window._yas && window._yas.sendCustomInfo) {
        window._yas.sendCustomInfo({
            op: 'YB_CHOOSE_FOR_YOU_Y',
            param: JSON.stringify({
                REC_POSE: 100013,
                REC_ID: RECID,
                PRD_ID: PRD_ID,
                PRD_NUM: PRD_NUM,
                C_ID: C_ID,
                ACTION_ID: ACTION_ID,
                PAGE_NUM: 1
            })
        }, true);
    }
}

function request() {
    if (requesting || end) {
        return;
    }

    requesting = true;

    if (preferenceUrl) {
        $.get(preferenceUrl).then(function(html) {
            $recommendForYou.html(html).show();
            lazyLoad($recommendForYou.find('img.lazy'));

            // 为你优选埋点- 加载

            $('.goods-container').find('.good-info').each(function() {
                goodid = $(this).data('good-id');

                if (goodid) {
                    goodIds.push(goodid);
                }
            });

            prdLoad = JSON.stringify(goodIds).replace(/\[|\]|/g, '');

            yasRequest(prdLoad, goodIds.length, 0);

            // 点击商品
            $('.good-info').on('click', function() {
                var PRD_ID = $(this).data('good-id'),
                    PRD_NUM = $(this).index();

                yasRequest(PRD_ID, PRD_NUM, 1);

                // return false;
            });

        }).fail(function() {
            $recommendForYou.hide();
        });
    }
}

function scrollHandler() {
    if (!end || $(window).scrollTop() + winH >= $(document).height() - 200) {
        request();
    }
}

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