product-process.js 7.81 KB
'use strict';
const _ = require('lodash');
const camelCase = require('../library/camel-case');
const helpers = require('../library/helpers');

/**
 * 根据性别来决定  默认图片获取字段   如果是 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 || '';
};

/**
 * 商品搜索商品数据处理
 */
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.productId || !product.goodsList.length) {
            return;
        }

        // 市场价和售价一样,则不显示市场价
        if (product.marketPrice === product.salesPrice) {
            product.marketPrice = false;
        } else if (product.marketPrice && product.salesPrice) {
            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
        });

        if (options.showPoint) {
            product.marketPrice += '.00';
            product.salesPrice += '.00';

            if (product.vip1Price) {
                product.vip1Price = parseInt(product.vip1Price, 10) + '.00';
            }

            if (product.vip2Price) {
                product.vip2Price = parseInt(product.vip2Price, 10) + '.00';
            }

            if (product.vip3Price) {
                product.vip3Price = parseInt(product.vip3Price, 10) + '.00';
            }
        }

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

        // 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.isNew = false;
            } else if (product.tags.isDiscount &&
                (product.tags.isNew || product.tags.isLimited || product.tags.isYohood || product.tags.isAdvance)) {
                // 打折与其它组合则隐藏打折
                product.tags.isDiscount = false;
            } else if (product.tags.isYohood && product.tags.isNew) {
                // YOHOOD和新品组合显示YOHOOD
                product.tags.isNew = false;
            }
        }

        pruductList.push(product);
    });
    return 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;
};