product-process.js 7.36 KB
'use strict';
require('../app');
const _ = require('lodash');
const helpers = global.yoho.helpers;

const toArray = (obj) => {
    if (_.isArray(obj)) {
        return obj;
    }
    let arr = [];

    _.forEach(obj, (v, k) => {
        if (_.isObject(v)) {
            v._key = k;
        } else {
            v = {
                _key: k,
                _value: v
            };
        }
        arr.push(v);
    });

    return arr;
};

/**
 * 按照数组中指定字段排序二维数组
 *
 * @param  array list 需要排序的数组
 * @param  string key 字段名称
 * @param  boolean 有 desc 时候降序排列,默认为false
 */
const _sortListByField = (list, key, desc) => {
    let array = toArray(list);

    array = array.sort((a, b) => {
        let matchNumber = /([\d]+)/g;

        // 有键,使用键的值排序
        if (a[key] && b[key]) {
            let numA = +(_.toArray(a[key].match(matchNumber))[0] || 0); // 取第一个出现的数字排序,如果不存在,取0
            let numB = +(_.toArray(b[key].match(matchNumber))[0] || 0);

            return (desc ? numA > numB : numA < numB) ? -1 : 1;
        }

        // 无键, 使用本身
        let numA = +(_.toArray(a._value.match(matchNumber))[0] || 0);
        let numB = +(_.toArray(b._value.match(matchNumber))[0] || 0);

        return numA < numB ? -1 : 1;
    });

    return array;
};

/**
 * 商品搜索商品数据处理
 */
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);

    _.forEach(list, (product) => {
        if (!product) {
            return;
        }

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

        // 如果库存为0,显示已抢完
        // if (product.storage_num === 0) {
        //     product.noStorage = true;
        // }

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

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

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

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

            _.forEach(product.tags, tag => {
                tags[tag] = true;
            });

            product.tags = tags;

            if (tags.is_solded === true) {
                product.is_solded = true;
            }

        }

        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: 'brand_name',
            firstSub: 0,
            dataType: 'brand',
            sortNum: '1'
        },
        color: {
            name: '所有颜色',
            title: '颜色',
            dataId: 'color_id',
            subsName: 'color_name',
            firstSub: 0,
            dataType: 'color',
            sortNum: '4'
        },
        discount: {
            name: '所有商品',
            title: '折扣',
            dataId: 'key',
            subsName: 'name',
            firstSub: '0.1,0.9',
            dataType: 'p_d',
            sortNum: '7'
        },
        gender: {
            name: '所有性别',
            title: '性别',
            dataId: 'key',
            subsName: 'flag',
            firstSub: '1,2,3',
            dataType: 'gender',
            sortNum: '0'
        },
        group_sort: {
            name: '所有品类',
            title: '品类',
            dataId: 'relation_parameter',
            subsName: 'category_name',
            firstSub: 0,
            dataType: 'sort',
            sortNum: '3'
        },
        priceRange: {
            name: '所有价格',
            title: '价格',
            dataId: 'key',
            subsName: 'flag',
            firstSub: 0,
            dataType: 'price',
            sortNum: '6'
        },
        size: {
            name: '所有尺码',
            title: '尺码',
            dataId: 'size_id',
            subsName: 'size_name',
            firstSub: 0,
            dataType: 'size',
            sortNum: '5'
        },
        ageLevel: {
            name: '所有人群',
            title: '人群',
            dataId: 'id',
            subsName: 'name',
            firstSub: 0,
            dataType: 'ageLevel',
            sortNum: '2'
        }
    };

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

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

        if (!filtersType[key]) {
            return;
        }

        if ((options.hideSize && key === 'size') || (options.hideSort && key === 'group_sort')) {
            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
        });

        // 折扣,价格区间,需要排序
        if (key === 'discount' || key === 'priceRange') {
            item = _sortListByField(item, 'name');
        }

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

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

            if (key === 'priceRange') {
                subs.name = sub._value;
            } else 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[filtersType[key].sortNum] = classify;
    });

    return filters;
};