global.js 4.38 KB
'use strict';

const utils = '../../../utils';
const productProcess = require(`${utils}/product-process`);
const globalapi = global.yoho.GlobalAPI;
const $ = require('cheerio');
const _ = require('lodash');

exports.list = (param) => {
    return globalapi.get('product/api/v2/detail/getlist', param).then((result) => {
        if (!result || !result.data) {
            return {};
        }

        return {
            filter: productProcess.processFilter(result.data.filter || []),
            list: result.data.product_list.map((data) => {
                return {
                    product_url: data.is_global === 'Y' ? `//m.yohobuy.com/product/global/${data.product_skn}.html` : `//m.yohobuy.com/product/${data.product_skn}.html`, // eslint-disable-line
                    product_img: data.default_images,
                    price: data.formart_final_price,
                    product_name: data.product_name,
                    country_name: data.country_name,
                    is_stock: data.is_stock,
                    is_limited: data.is_limited,
                    is_plane: data.is_plane
                };
            })
        };
    });
};

exports.getBrand = (param) => {
    return globalapi.get('editor/api/v1/brand/get', param).then((data) => {
        return data.data || {};
    });
};

exports.detail = (param) => {
    return globalapi.get('product/api/v2/detail/get', param).then((result) => {
        if (!result || !result.data) {
            return {};
        }
        result = result.data;

        let goods = result.goods_list || [];

        if (goods.length === 1) {
            result.bannerTop = {
                img: (goods[0].images_list[0] || {}).image_url,
                imgAlt: _.get(result, 'brand_info.brand_name', '') + '|' + _.get(result, 'product_name', '')
            };
        } else {
            result.bannerTop = {
                list: goods.map((g) => {
                    return {
                        img: (g.images_list[0] || {}).image_url,
                        imgAlt: _.get(result, 'brand_info', 'brand_name', '') + '|' + _.get(result, 'product_name', '')
                    };
                })
            };
        }

        result.show_final_price = result.formart_final_price;
        if (result.formart_final_price !== result.formart_orign_price) {
            result.show_orign_price = result.formart_orign_price;
        }

        result.show_sales_price = result.format_sales_price;
        if (result.format_market_price !== result.format_sales_price) {
            result.show_market_price = result.format_market_price;
        }

        if (result.brand_info && result.brand_info.brand_id) {
            result.brand_info.brand_url = `//m.yohobuy.com/product/global/list?brand=${result.brand_info.brand_id}`;
        }

        if (result.illustrate_contents && result.illustrate_contents.length) {
            result.illustrate = {
                title1: (result.illustrate_contents[0] || {}).title,
                content1: (result.illustrate_contents[0] || {}).content,
                title2: (result.illustrate_contents[1] || {}).title,
                content2: (result.illustrate_contents[1] || {}).content
            };
        }

        return result;
    });
};

exports.gethtml = (param) => {
    return globalapi.get('product/api/v1/detail/gethtml', param, {
        cache: true
    }).then((result) => {
        result = _.isString(result) ? result : '';
        result = $.load(result);
        result = result('.good-detail-page');

        // 修改为你优选url
        result.find('.recommend-for-you a.swiper-slide').each((index, el)=> {
            el = $(el);

            let href = el.attr('href');

            if (href) {
                let o = (href.split('?')[1] || '').replace('openby:yohobuy=', '');

                o = JSON.parse(o);
                el.attr('href', `//m.yohobuy.com/product/global/${o.params.skn}.html`);
            }
        });

        return (result.html() || '').replace(/<img src=/g, '<img class="lazy" src="data:image/gif;' +
            'base64,R0lGODlhAQABAJEAAAAAAP///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw=="' +
            ' data-original=').replace(/<img border="0" src=/g, '<img border="0" class="lazy" ' +
            'src="data:image/gif;base64,' +
            'R0lGODlhAQABAJEAAAAAAP///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw=="' +
            ' data-original=').replace(/http:\/\//g, '//');
    });
};