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

let _getProduct = function(param) {
    $('.product-source').each(function(indx, el){
        el = $(el);
        let 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 data,
                    goods = el.find('.feature-product-info');

                if (!goods.length) {
                    return;
                }

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

                let w = imgwh[0] || 450,
                    h = imgwh[1] || 500;

                goods.each(function(indx, el) {
                    data = res[indx];
                    if (!data) {
                        return;
                    }
                    
                    el = $(el);
                    el.find('.brand-name').html(data.brand_name);
                    el.find('a.product-brand').attr('href', `//m.yohobuy.com/product/index/brand?domain=${data.brand_domain}`);
                    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, w, 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}`);
                });
            }
        });
    });
};

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')
            });
        }
    }
};