product-process-simple.js 7.25 KB
'use strict';
const _ = require('lodash');
const helpers = global.yoho.helpers;
const logger = global.yoho.logger;

/**
 * 处理接口返回图片链接,兼容非正常链接(如:/2015/10/22/08/023a5aa1cbdac7bdcd1685bfdab118b0c5.jpg)
 *
 */
const handleGoodsThumb = (url, bucket) => {
    url = url || '';

    if (!url || url.indexOf('http') >= 0) {
        return url;
    }

    let urlArr = url.split('/'),
        stag = urlArr[urlArr.length - 1].substr(0, 2),
        domain = `static.yhbimg.com/${bucket}`;

    url = domain + url.split('?')[0] + '?imageMogr2/thumbnail/{width}x{height}/background/d2hpdGU=/position/center/quality/80'; // eslint-disable-line

    if (stag === '01') {
        return `//img11.${url}`;
    } else if (stag === '03') {
        return `//flv01.${url}`;
    } else {
        return `//img12.${url}`;
    }
};


/**
 * 商品搜索商品数据处理
 */
exports.processProductList = (list, options) => {
    const pruductList = [];

    options = Object.assign({
        showTags: true,
        showNew: true,
        showSale: true,
        showFew: true,
        showLimit: true,
        showDiscount: true, // 显示折扣
        newCoverSort: false, // 新封面排序
        width: 290,
        height: 388,
        isApp: false,
        showPoint: true,
        gender: '2,3',
        from: {} // 来源
    }, options);

    _.forEach(list, (product) => {
        // 全球购接口与普通接口返回商品差异属性预处理
        if (options.isGlobal && product) {
            Object.assign(product, {
                goods_list: [{
                    images_url: product.default_images,
                    status: 1
                }],
                sales_price: product.final_price || product.orign_price,
                market_price: null,
                tbl_country_name: product.country_name,
                tbl_brand_id: product.brand_id
            });
        }

        // 商品信息有问题,则不显示
        if (!product || !product.product_skn) {
            return;
        }

        // 6.7 搜索去掉goods_list, 兼容处理
        if (_.isEmpty(product.goods_list)) {
            product.goods_list = [{
                images_url: product.default_images
            }];
        }

        let proInfo = {
            skn: product.product_skn,
            product_name: product.product_name,
            market_price: product.market_price,
            sales_price: product.sales_price,
            is_few: product.is_soon_sold_out === 'Y'
        };

        // 市场价和售价一样,则不显示市场价, 不显示折扣信息
        if (proInfo.market_price <= proInfo.sales_price) {
            delete proInfo.market_price;
        } else if (options.showDiscount) {
            proInfo.discount = (proInfo.sales_price / proInfo.market_price * 10).toFixed(1);
        }

        // 商品链接
        if (product.is_global === 'Y') {
            proInfo.url = helpers.urlFormat(`/product/global/${product.product_skn}.html`, null);
        } else if (product.product_skn) {
            proInfo.url = helpers.getUrlBySkc(product.product_skn);
        }

        // 店铺链接
        if (product.is_global === 'Y' && product.tbl_brand_id) {
            proInfo.brandUrl = helpers.urlFormat('/product/global/list', {brand: product.tbl_brand_id});
        } else if (product.shop_id * 1) {
            Object.assign(proInfo, {
                brand_name: product.shop_name,
                brandUrl: helpers.urlFormat(`/shop/${product.shop_domain}-${product.shop_id}.html`, null)
            });
        }

        let defaultColorImg,
            goodsList = [];

        // 处理商品颜色封面
        _.forEach(_.orderBy(product.goods_list, ['is_default'], ['desc']), goods => {
            if (goods.is_default === 'Y' && !defaultColorImg) {
                defaultColorImg = goods.images_url; // 颜色默认封面
            }

            if (+goods.status) {
                goodsList.push({
                    images_url: goods.images_url,
                    color_name: goods.color_name,
                    url: proInfo.url,
                    status: 1
                });
            }
        });

        Object.assign(proInfo, {
            goods_list: goodsList,
            thumb: handleGoodsThumb(product.default_images || defaultColorImg, 'goodsimg')
        });

        // 处理标签
        if (options.showTags) {
            let tags = [],
                isfew = false;

            if (product.is_global === 'Y') {
                tags.push({
                    is_global: true,
                    plane: product.tbl_plane === 'Y',
                    name: product.tbl_country_name
                });
            }

            _.get(product, 'tags', []).forEach((value) => {
                let tag = {};

                switch (value) {
                    case 'is_soon_sold_out': // 即将售磬
                        options.showFew && (tag.is_few = true, isfew = true);
                        break;
                    case 'is_solded': // 已售磬
                        product.is_solded = true;
                        break;
                    case 'is_new': // 新品NEW
                        options.showNew && (tag.is_new = true);
                        break;
                    case 'is_discount': // SALE
                        options.showSale && (tag.is_sale = true);
                        break;
                    case 'is_limited': // 限量
                        options.showLimit && (tag.is_limit = true);
                        break;
                    case 'is_yohood': // YOHOOD
                        tag.is_new_festival = true;
                        break;
                    case 'is_advance': // 再到着
                        tag.is_re_new = true;
                        break;
                    case 'midYear':// 年中热促
                        tag.is_year_mid_promotion = true;
                        break;
                    case 'yearEnd':// 年终大促
                        tag.is_year_end_promotion = true;
                        break;
                    case 'is_presell':// 预售
                        tag.is_presell = true;
                        break;
                    default:
                        break;
                }
                tags.push(tag);
            });

            proInfo.tags = tags;
            isfew ? proInfo.is_few = isfew : delete proInfo.is_few;
        }

        let genderStr = '';

        if (_.get(options, 'from.params.gender') === '1,3') {
            genderStr = '男士';
        } else if (_.get(options, 'from.params.gender') === '2,3') {
            genderStr = '女士';
        } else {
            genderStr = '';
        }

        proInfo.productTitle = `【${genderStr}${product.small_sort ? product.small_sort : ''}${product.product_name}`;

        if (options.query && _.isString(proInfo.product_name)) {
            try {
                let qreg = new RegExp(options.query.replace('\\', '\\\\'), 'ig');

                proInfo.product_name = proInfo.product_name.replace(qreg, '<span style="color:#c00;">$&</span>');
            } catch (e) {
                logger.debug(`product_name replace query fail:${e.toString()}`);
            }
        }

        pruductList.push(proInfo);
    });

    return pruductList;
};