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


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

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

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

let goodid,
    goodIds = [],
    prdLoad;

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

    requesting = true;

    if (preferenceUrl) {
        $.get(preferenceUrl, {yhChannel: window.cookie('_Channel') || 'boys'}).then(function(html) {
            if (typeof html !== 'string') {
                return;
            }

            $recommendForYou.html(html.replace(/https?:/gi, '')).show();
            lazyLoad($recommendForYou.find('img.lazy'));

            // 为你优选埋点- 加载
            $('.new-goods.container').find('.good-info').each(function() {
                goodid = $(this).data('good-id');

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

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

            window.givePoint({
                REC_POSE: 100013,
                REC_ID: RECID,
                PRD_ID: prdLoad,
                PRD_NUM: goodIds.length,
                ACTION_ID: 0,
                PAGE_NUM: 1
            });

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

                window.givePoint({
                    REC_POSE: 100013,
                    REC_ID: RECID,
                    PRD_ID: PRD_ID,
                    PRD_NUM: PRD_NUM,
                    ACTION_ID: 1,
                    PAGE_NUM: 1
                });

                // return false;
            });

            window.rePosFooter && window.rePosFooter();

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

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

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