product-process.js 11.9 KB
'use strict';
const _ = require('lodash');

const camelCase = global.yoho.camelCase;
const helpers = global.yoho.helpers;
const images = require(`${global.utils}/images`);

/**
 * 根据性别来决定  默认图片获取字段   如果是 2、3
 *
 * 则优先从cover2 --》 cover1 -- 》 images_url
 * 否则优先从cover1 --》 cover2 -- 》 images_url
 *
 */
const procProductImg = (product, gender) => {
    if (gender === '2,3') {
        return product.cover2 || product.cover1 || product.imagesUrl || '';
    }

    return product.cover1 || product.cover2 || product.imagesUrl || '';
};


/**
 * 处理列表大图周边小图数据,拼接添加 href
 * @param origin goods 原始数据
 * @returns {{}} goods 输出数据,结构不变
 */
const handleGoodsListData = (origin) => {
    _.forEach(origin, (value, key) => {
        if (!_.isEmpty(value.goodsList)) {
            _.forEach(value.goodsList, (subValue, subKey) => {
                origin[key].goodsList[subKey].url =
                    helpers.urlFormat(`/product/pro_${value.productId}_${subValue.goodsId}/${value.cnAlphabet}.html`);
            });
        }
    });
    return origin;
};

/**
 * 根据性别来决定  默认图片获取字段   如果是 2、3
 *
 * 2、3: cover2 --> images_url  -> cover1
 * 1,3 :cover1 --> images_url  -> cover2
 * 其他: cover1 --> cover2 --> images_url
 *
 * @param array $images
 * @return string 商品图片
 */
const procProductImgs = (item, gender) => {
    let imgUrl = item.images_url ? item.images_url : '',
        cover1 = item.cover_1 ? item.cover_1 : '',
        cover2 = item.cover_2 ? item.cover_2 : '';

    if (gender === '2,3') {
        return cover2 ? cover2 : (imgUrl ? imgUrl : cover1);// eslint-disable-line
    } else if (gender === '1,3') {
        return cover1 ? cover1 : (imgUrl ? imgUrl : cover2);// eslint-disable-line
    } else {
        return cover1 ? cover1 : (cover2 ? cover2 : imgUrl);// eslint-disable-line
    }
};

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

    options = Object.assign({
        showTags: true,
        showNew: true,
        showSale: true,
        width: 290,
        height: 388,
        isApp: false,
        showPoint: true,
        gender: '2,3'
    }, options);

    list = camelCase(list);

    _.forEach(list, (product) => {


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

        // 如果库存为0,显示已抢完
        if (product.storageNum === 0 && product.status === 1) {
            product.isSaleOut = true;
        }

        // 市场价和售价一样,则不显示市场价, 不显示折扣信息
        if (product.marketPrice === product.salesPrice) {
            delete product.marketPrice;
        } else {
            product.discount = (product.salesPrice / product.marketPrice * 10).toFixed(1);
        }

        // 判别默认的商品是否将默认的图片URL赋值到skn
        let flag = false;

        // 如果设置了默认图片,就取默认的图片
        _.forEach(product.goodsList, (goods) => {
            if (flag) {
                return;
            }
            if (goods.isDefault === 'Y') {
                product.defaultImages = procProductImg(goods);
                flag = true;
            }
        });

        // 如果还未赋值,则取第一个skc产品的默认图片
        if (!flag) {
            product.defaultImages = procProductImg(product.goodsList[0]);
        }

        product = Object.assign(product, {
            id: product.productSkn,
            thumb: product.defaultImages
        });

        product.isSoonSoldOut = product.isSoonSoldOut === 'Y';
        product.url = helpers.urlFormat(`/product/pro_${product.productId}_${product.goodsList[0].goodsId}/${product.cnAlphabet}.html`); // eslint-disable-line

        // tar add 1606071146 品牌链接处理
        product.brandUrl = helpers.urlFormat('', '', product.brandDomain);

        // APP访问需要加附加的参数
        // 备注:如果以后APP的接口太多,可以把这边参数提取出来,变成一个公共的方法来生成,便于以后管理维护
        if (options.isApp) {
            product.url += `?openby:yohobuy={"action":"go.productDetail","params":{"product_skn":'${product.productId}'}}`; // eslint-disable-line
        }

        if (options.showTags) {
            product.tags = {};

            product.tags.isNew = options.showNew && product.isNew === 'Y'; // 新品
            product.tags.isDiscount = options.showSale && product.isDiscount === 'Y'; // 在售
            product.tags.isLimited = product.isLimited === 'Y'; // 限量
            product.tags.isYohood = product.isYohood === 'Y'; // YOHOOD
            product.tags.midYear = product.midYear === 'Y'; // 年中
            product.tags.yearEnd = product.yearEnd === 'Y'; // 年末
            product.tags.isAdvance = product.isAdvance === 'Y'; // 再到着

            // 打折与即将售完组合显示打折
            if (product.isSoonSoldOut && product.tags.isDiscount) {
                product.tags.isFew = false;
            } else if (product.tags.isDiscount &&
                (product.tags.isFew || product.tags.isLimited || product.tags.isYohood || product.tags.isAdvance)) {
                // 打折与其它组合则隐藏打折
                product.tags.isDiscount = false;
            } else if (product.tags.isYohood && product.tags.isFew) {
                // YOHOOD和新品组合显示YOHOOD
                product.tags.isFew = false;
            }
        }

        pruductList.push(product);
    });
    return handleGoodsListData(pruductList);
};


/**
 * 处理筛选数据
 * @param list
 * @param  string | options
 * @return array 处理之后的筛选数据
 */
exports.processFilter = (list, options) => {
    const filters = {
        classify: []
    };

    const filtersType = {
        brand: {
            name: '所有品牌',
            title: '品牌',
            dataId: 'id',
            subsName: 'brandName',
            firstSub: 0,
            dataType: 'brand'
        },
        color: {
            name: '所有颜色',
            title: '颜色',
            dataId: 'colorId',
            subsName: 'colorName',
            firstSub: 0,
            dataType: 'color'
        },
        discount: {
            name: '所有商品',
            title: '折扣',
            dataId: 'key',
            subsName: 'name',
            firstSub: '0.1,0.9',
            dataType: 'p_d'
        },
        gender: {
            name: '所有性别',
            title: '性别',
            dataId: 'key',
            subsName: 'flag',
            firstSub: '1,2,3',
            dataType: 'gender'
        },
        groupSort: {
            name: '所有品类',
            title: '品类',
            dataId: 'relationParameter',
            subsName: 'categoryName',
            firstSub: 0,
            dataType: 'sort'
        },
        priceRange: {
            name: '所有价格',
            title: '价格',
            dataId: 'key',
            subsName: 'flag',
            firstSub: 0,
            dataType: 'price'
        },
        size: {
            name: '所有尺码',
            title: '尺码',
            dataId: 'sizeId',
            subsName: 'sizeName',
            firstSub: 0,
            dataType: 'size'
        }
    };

    options = Object.assign({
        gender: '1,2,3', // 默认选择的性别,默认1,2,3表示所有
        exclude: null // 需要排除的字段
    }, options);
    list = camelCase(list);

    _.forEach(list, (item, key) => {
        let classify = {
            subs: []
        };

        if (key === 'group_sort') {
            return;
        }

        if ((options.hideSize && key === 'size') || (options.hideSort && key === 'groupSort')) {
            return;
        }

        classify.dataType = filtersType[key].dataType;
        classify.name = filtersType[key].name;
        classify.title = filtersType[key].title;

        classify.subs.push({
            chosed: true,
            dataId: filtersType[key].firstSub,
            name: filtersType[key].name
        });

        _.forEach(item, (sub, index) => {
            let subs = {};

            if (filtersType[key].dataId === 'key') {
                subs.dataId = index;
            } else if (filtersType[key].dataId === 'relationParameter') {
                subs.dataId = sub.relationParameter['sort']; // eslint-disable-line
            } else {
                subs.dataId = sub[filtersType[key].dataId];
            }

            if (filtersType[key].subsName === 'flag') {
                subs.name = sub;
            } else {
                subs.name = sub[filtersType[key].subsName];

                if (key === 'discount') {
                    subs.name = subs.name + '折商品';
                }
            }
            classify.subs.push(subs);
        });

        filters.classify.push(classify);
    });

    return filters;
};

// 处理单个商品
exports.processProduct = (productData, options) => {
    let result = {};

    options = Object.assign({
        showTags: true,
        showNew: true,
        showSale: true,
        width: 290,
        height: 388,
        showPoint: true
    }, options);

    // 商品信息有问题,则不显示
    if (!productData.product_skn || !productData.goods_list[0]) {
        return false;
    }

    // 市场价和售价一样,则不显示市场价
    if (parseInt(productData.market_price, 0) === parseInt(productData.sales_price, 0)) {
        productData.market_price = false;
    }

    // 设置默认图片
    _.forEach(productData.goods_list, item => {
        if (item.is_default === 'Y') {
            productData.default_images = procProductImgs(item, options.gender);
        }
    });

    if (!productData.default_images) {
        productData.default_images = procProductImgs(productData.goods_list[0], options.gender);
    }

    result.id = productData.product_skn;
    result.product_id = productData.product_id;
    result.thumb = images.getImageUrl(productData.default_images, options.width, options.height);
    result.name = productData.product_name;
    result.price = !productData.market_price ? false : productData.market_price;
    result.salePrice = productData.sales_price;
    if (options.showPoint) {
        result.price += '.00';
        result.salePrice += '.00';
    }

    result.is_soon_sold_out = (productData.is_soon_sold_out === 'Y');
    result.url = helpers.urlFormat(`/product/pro_${productData.product_id}_${productData.goods_list[0].goods_id}/${productData.cn_alphabet}.html`, null, 'list'); // eslint-disable-line


    if (options.showTags) {
        result.tags = {};
        result.tags.is_new = options.showNew && productData.is_new && productData.is_new === 'Y'; // 新品
        result.tags.is_discount = options.showSale && productData.is_discount && productData.is_discount === 'Y'; // 在售
        result.tags.is_limited = productData.is_limited && productData.is_limited === 'Y'; // 限量
        result.tags.is_yohood = productData.is_yohood && productData.is_yohood === 'Y'; // YOHOOD
        result.tags.midYear = productData['mid-year'] && productData['mid-year'] === 'Y'; // 年中
        result.tags.yearEnd = productData['year-end'] && productData['year-end'] === 'Y'; // 年末
        result.tags.is_advance = productData.is_advance && productData.is_advance === 'Y'; // 再到着

        if (result.is_soon_sold_out && result.tags.is_discount) {
            result.tags.is_new = false;// 打折与即将售完组合显示打折
        } else if (result.tags.is_discount &&
            (result.tags.is_new || result.tags.is_limited || result.tags.is_yohood || result.tags.is_advance)) {
            result.tags.is_discount = false;// 打折与其它组合则隐藏打折
        } else if (result.tags.is_yohood && result.tags.is_new) {
            result.tags.is_new = false;// YOHOOD和新品组合显示YOHOOD
        }
    }
    return result;
};