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

/**
 * 商品搜索商品数据处理
 */
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 || !_.get(product, 'goods_list.length', 0)) {
            return;
        }

        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: product.default_images || defaultColorImg
        });

        // 处理标签
        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;
        }

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