search.js 2.19 KB
'use strict';

const _ = require('lodash');
const api = global.yoho.API;
const prettyFilter = require(`${global.utils}/beautify/filters`);
const processProductList = require(`${global.utils}/beautify/product`);

const search = {
    products(params) {
        if (params.hasOwnProperty('priceRange')) {
            params.price = params.priceRange;
            delete params.priceRange;
        }

        const data = _.merge({
            method: 'app.search.li'
        }, params);

        return api.get('', data, {
            cache: true,
            code: 200
        }).then(result => {
            result = result || {};
            if (result.data) {
                // java端不愿意将boys改为wen,所以没办法,只能在这边改了。
                if (result.data.filter) {
                    const gender = result.data.filter.gender || {};

                    for (let k in gender) {
                        if (k.indexOf('1') > -1) {
                            gender[k] = 'MEN';
                        } else if (k.indexOf('2') > -1) {
                            gender[k] = 'WOMEN';
                        }
                    }
                }

                prettyFilter(result.data.filter);
                result.data.product_list = processProductList(result.data.product_list, {
                    gender: params.gender
                });
            }
            return result;
        });
    },

    /**
     * [根据商品SKN获取商品的简要信息]
     * @param  {[array]} sknString [skns]
     * @return {[type]}
     */
    productInfoBySkns(sknString) {
        let limit = sknString && sknString.split(',').length;

        // 调用搜索接口
        let param = {
            method: 'app.search.recomandLi',
            query: sknString,
            order: 's_t_desc',
            limit: limit || 1
        };

        return api.get('', param, {
            cache: true
        }).then(result => {
            result = result || {};
            if (result.data) {
                result.data.product_list = processProductList(result.data.product_list);
            }
            return _.get(result, 'data.product_list', []);
        });
    }
};

module.exports = search;