individuation.js 4.11 KB
import $ from 'jquery';
import jsonp from './jsonp';
import utils from './utils';
import cookies from './cookies';

let _replaceData = function(el, cond, data, wh) {
    el.find('.brand-name').html(data.brand_name);
    el.find('a.product-detail').attr('href', `//m.yohobuy.com/product/pro_${data.product_id}_${data.goods_id}/${data.cn_alphabet}.html?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":${data.product_skn}}}`);
    el.find('.product-detail-img').attr('src', utils.image(data.default_images, wh.w, wh.h, 2, 60));
    el.find('.product-name').html(data.product_name);
    el.find('.sale-price').html(${data.sales_price}`);
    el.find('.market-price').html(${data.market_price}`);

    let brandDom = el.find('a.product-brand');
    if (brandDom) {
        if (data.shop_id) {
            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}}}`);
        } 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}}}`);
        }
    }

    let listDom = el.find('a.list-product');
    if (listDom) {
        let 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);
    }
};

let _getProduct = function(param) {
    $('.product-source').each(function(indx, el){
        el = $(el);
        let cloneitem = el.attr('cloneitem'),
            cond = JSON.parse(el.attr('condition') || '{}');

        jsonp({
            url: '//m.yohobuy.com/activity/individuation?callback=?',
            data: Object.assign({}, param, cond)
        }).then((res)=> {
            if (res && res.length) {
                let goods = el.find('.feature-product-info');
                if (!goods.length) {
                    return;
                }

                if (cloneitem) { // 可复制item
                    let cnt = cond.limit || 10;
                    while (goods.length <= cnt) {
                        goods.push(goods[0].clone().appendTo(el));
                    }
                }

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

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

                goods.each(function(indx, el) {
                    data = res[indx];
                    if (!data || !data.default_images) {
                        return;
                    }
                    _replaceData($(el), cond, data, wh);
                });
            }
        });
    });
};

export default {
    init(uid) {
        if (utils.isApp()) {
            document.addEventListener('deviceready', function() {
                window.yohoInterface.triggerEvent(function(data) {
                    // 获取个性话数据
                    _getProduct({
                        uid: data.uid,
                        udid: data.udid
                    });
                }, function() {}, {
                    method: 'get.analyticAppData'
                });
            }, false);
        } else {
            // 获取个性话数据
            _getProduct({
                uid: uid,
                udid: cookies.cookie('_yasvd')
            });
        }
    }
};