individuation.js 9.79 KB
var $ = require('../utils/element');
var jsonp = require('../utils/jsonp');
var utils = require('../utils/common');
var cookies = require('../utils/cookie');
var lazyLoad = require('./lazyLoad');
var abTestFlag = false; // ab测试
var getPname = function () {
    var pName = '';

    if (utils.isMobile.iOS()) {
        pName = 'iFP_SalesPromotion_h5';
    }

    if (utils.isMobile.Android()) {
        pName = 'aFP_SalesPromotion_h5';
    }

    return pName;
};

var _replaceData = function (el, cond, data, wh, fp) {
    if (!data) {
        return;
    }

    el.find('.brand-name').html(data.brand_name);
    el.find('.product-detail').attr('href', '//m.yohobuy.com/product/' + data.product_skn + '.html?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":' + data.product_skn + ',"from_page_name":"' + getPname() + '","from_page_param":"' + location.href.split('?')[0] + '"}}');
    el.find('.product-name').html(data.product_name);
    el.find('.vip-price-val').html('¥' + data.vip_price);
    el.find('.sale-price').html('¥' + data.sales_price);
    el.data('skn', data.product_skn);

    var $productDetailImg = el.find('.product-detail-img');

    if ($productDetailImg.hasClass('lazy')) {
        $productDetailImg.attr('data-original', utils.image(data.default_images, wh.w, wh.h, 2, 75));
    } else {
        $productDetailImg.attr('src', utils.image(data.default_images, wh.w, wh.h, 2, 75));
    }

    if (data.sales_price === data.market_price) {
        el.find('.market-price').hide();
    } else {
        el.find('.market-price').html('¥' + data.market_price).show();
    }

    var brandDom = el.find('.product-brand');
    if (brandDom) {
        if (data.shop_id) {
            // eslint-disable-next-line
            brandDom.attr('href', '//m.yohobuy.com/product/index/brand?domain=' + data.brand_domain + '&openby:yohobuy={"action":"go.shop","params":{"shop_id":' + data.shop_id + ',"shop_template_type":' + data.shop_template_type || "1" + ',"is_red_shop":' + data.is_red_shop || 1 + '}}');
        } else {
            brandDom.attr('href', '//m.yohobuy.com/product/index/brand?domain=' + data.brand_domain + '&openby:yohobuy={"action":"go.brand","params":{"brand_id":' + data.brand_id + '}}');
        }
    }

    // 楼层数据
    if (fp) {
        if (!fp.I_INDEX) fp.I_INDEX = 0;

        fp.I_INDEX++;
        el.find('.product-detail').attr('fp', JSON.stringify(fp));
        if (brandDom) {
            fp.I_INDEX++;
            brandDom.attr('fp', JSON.stringify(fp));
        }
    }

    var listDom = el.find('.list-product');
    if (listDom) {
        var sortStr = '',
            gender = cond.gender ? cond.gender : '1,3',
            url = '//list.m.yohobuy.com?gender=' + gender;

        if (listDom.data('sort')) {
            sortStr += ',"sort":' + data.small_sort_id;
            url += '&sort=' + data.small_sort_id;
        }
        if (listDom.data('misort')) {
            sortStr += ',"misort":' + data.middle_sort_id;
            url += '&misort=' + data.middle_sort_id;
        }
        if (listDom.data('msort')) {
            sortStr += ',"msort":' + data.max_sort_id;
            url += '&msort=' + data.max_sort_id;
        }

        url += '&openby:yohobuy={"action":"go.list","params":{"actiontype":1,"gender":' + gender + sortStr + '}}';
        listDom.attr('href', url);
    }
    el.html(el.html());
    return el;
};

var _getProductData = function (dataArr) {
    if (!dataArr.length) {
        return;
    }

    var data = dataArr.shift();
    if (!data || !data.default_images) {
        return _getProductData(dataArr);
    }
    return data;
};

var _jsonp = function (el, param, cond) {
    var cloneitem = el.attr('cloneitem');
    var loading = el.find('.loading');
    var goods = el.find('.feature-product-info');
    var rowNum = el.data('rownum');

    if (!goods.length) {
        return;
    }

    if (cond.abtest) {
        // ab测试,访客 和 >=512的uid 显示默认推荐
        if (!param.uid || (Number(param.uid) % 1024 >= 512)) {
            if (cloneitem) {
                $(goods[0]).remove();
            } else {
                goods.removeClass('novisible');
                loading.hide();
            }
            return;
        }
    }

    var imgwh = el.find('.imgwh').val() || ''; // 获取图片宽x高
    imgwh = imgwh.split('x') || [];

    var wh = {
        w: imgwh[0] || 300,
        h: imgwh[1] || 400
    };

    // 楼层数据
    var fp = el.attr('fp');
    fp = fp ? JSON.parse(fp) : '';
    jsonp({
        url: '//m.yohobuy.com/activity/individuation',
        jsonp: 'callback',
        data: $.extend({}, param, cond)
    }).then(function (res) {
        if (!res || !res.length) {
            if (cloneitem) {
                $(goods[0]).remove();
            } else {
                goods.removeClass('novisible');
                loading.hide();
            }
            return;
        }

        // 最后商品数量不足一行,截掉
        res.length = res.length - (res.length % rowNum);

        if (cloneitem) { // 可复制item
            var cnt = isNaN(Number(cloneitem)) ? res.length : Number(cloneitem);
            for (var indx = 0; indx < cnt; indx++) {
                var data = _getProductData(res); // 获取商品数据
                if (indx === 0) {
                    var gel = $(goods[0]);
                    var nel = _replaceData(gel, cond, data, wh, fp);
                    if (nel) {
                        nel.removeClass('novisible');
                    } else {
                        gel.remove();
                    }
                } else {
                    var nel = _replaceData($(goods[0]).clone(), cond, data, wh, fp);
                    if (nel) {
                        nel.removeClass('novisible');
                        nel.appendTo(el);
                    }
                }
            }
        } else {
            for (var i = 1; i < res.length; i++) {
                goods.last().after(goods.clone());
            }

            var $newGoods = el.find('.feature-product-info');

            $newGoods.each(function (indx, perProduct) {
                var $perProduct = $(perProduct);
                _replaceData($perProduct, cond, _getProductData(res), wh, fp);
            });
            $newGoods.removeClass('novisible');
            loading.hide();
        }
        lazyLoad({selector: '.lazy'});
    }, function () {
        if (cloneitem) {
            $(goods[0]).remove();
        } else {
            goods.removeClass('novisible');
            loading.hide();
        }
    });
};

var _abTest = function (param) {
    var mkt_code = 100000000000197; // 访客接口数据
    if (param.uid) {
        if (param.uid % 1024 >= 512) {
            mkt_code = 100000000000195; // 默认的值
        } else {
            mkt_code = 100000000000193; // 个性化,动态接口数据
        }
    }

    // cover覆盖mkt_code
    jsonp({
        url: '//m.yohobuy.com/activity/wechat/1111',
        jsonp: 'jQueryMketCode',
        data: {cover: 1, mkt_code: mkt_code, expires: (7 * 24 * 60 * 60 * 1000)}
    }).then(function () {
    });
    // 上报曝光事件
    var actionUrl = [];
    $('a').each(function () {
        var href = $(this).attr('href');
        if (href && !/^#.*$/.test(href) && !/^javascript.*$/i.test(href)) {
            actionUrl.push(href);
        }
    });

    var cnt = 0;
    var timer = setInterval(function () {
        cnt++;
        if (cnt > 3) {
            clearInterval(timer);
            return;
        }
        if (window._yas && window._yas.sendAppLogs && window.appBaseLogs) {
            clearInterval(timer);
            window._yas.sendAppLogs({
                appop: 'YB_H5_PAGE_AB_OPEN_L',
                param: JSON.stringify({
                    C_ID: utils.getQueryObj().yh_channel || 1,
                    PAGE_URL: window.originUrl,
                    PAGE_NAME: decodeURI(window.qs.title || document.title),
                    ACTION_URL: actionUrl,
                    CROWD_CODE: mkt_code + ''
                })
            }, true);

            $('a').click(function () {
                var furl = $(this).attr('href');
                if (furl && !/^#.*$/.test(furl) && !/^javascript.*$/i.test(furl)) {
                    window._yas.sendAppLogs({
                        appop: 'YB_H5_PAGE_AB_FLR_C',
                        param: JSON.stringify({
                            C_ID: utils.getQueryObj().yh_channel || 1,
                            PAGE_URL: window.originUrl,
                            PAGE_NAME: decodeURI(window.qs.title || document.title),
                            F_URL: furl,
                            CROWD_CODE: mkt_code + ''
                        })
                    }, true);
                }
            });
        }
    }, 1000);
};

var _getProduct = function (param) {
    var nodeList = param.nodeList;
    delete param.nodeList;
    $(nodeList).each(function (i, el) {
        el = $(el);
        var cond = el.attr('condition');
        if (!cond) {
            return;
        }
        cond = JSON.parse(cond);

        var yh_channel = utils.getQueryObj().yh_channel;
        if (cond.abtest) {
            abTestFlag = true;
        }

        if (yh_channel) {
            cond.yh_channel = yh_channel;
        }

        if (i > 3) {
            setTimeout(function () {
                _jsonp(el, param, cond);
            }, 300);
        } else {
            _jsonp(el, param, cond);
        }
    });

    abTestFlag && _abTest(param);
};

module.exports = function (list, user, env) {
    if (!user.uid && env === "app") {
        _getProduct({
            nodeList: list,
            uid: '',
            udid: utils.getQueryObj().udid || ''
        });
    } else {
        // 获取个性话数据
        _getProduct({
            nodeList: list,
            uid: user.uid,
            udid: cookies.cookie('_yasvd')
        });
    }
};