search.js 4.09 KB
/**
 * search model
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/07/21
 */
'use strict';
const utils = '../../../utils';
const logger = global.yoho.logger;
const camelCase = global.yoho.camelCase;
const productProcess = require(`${utils}/product-process`);
const _ = require('lodash');
const api = global.yoho.API;

/**
 * 排序转换
 */
const typeCont = {
    price: ['s_p_desc', 's_p_asc'],
    discount: ['p_d_desc', 'p_d_asc'],
    sale: ['s_n_desc', 's_n_asc'],
    newest: ['s_t_desc', 's_t_asc'],
    stock: ['s_s_desc', 's_s_asc'],
    all: ['d_s_desc', 'd_s_asc'],
    category: ['s_t_desc', 's_t_asc']
};

/**
 * 商品搜索接口请求
 * @param  {[object]} params
 * @return {[array]}
 */
const _searchSales = (params) => {
    let method = 'app.search.li';

    // 排除基本筛选项默认值为0的对象
    for (let str in params) {
        if (str !== 'order' && params[str] === '0' || params[str] === null) {
            delete params[str];
        }
    }

    // params.yh_channel = channelType[params.yh_channel];

    params = Object.assign({
        limit: '60',
        status: 1,
        sales: 'Y',
        stocknumber: 1,
        attribute_not: 2
    }, params);

    if (typeCont[params.type]) {
        params.order = typeCont[params.type][params.order];
    }

    return api.get('', Object.assign({
        method: method
    }, params), {
        cache: true
    });
};

/**
 * 品牌名称处理
 * @param  {[object]} list
 * @return {[object]}
 */
const _processBrandNames = (list) => {
    const formatData = [];

    list = list || [];
    list = camelCase(list);

    _.forEach(list, (item) => {
        _.forEach(item, (obj) => {
            formatData.push({
                brandDomain: obj.brandDomain,
                brandName: obj.brandName
            });
        });
    });

    return formatData;
};

/**
 * 品牌名称处理
 * @param  {[object]} list
 * @return {[object]}
 */
const _processClassNames = (list) => {
    const formatData = {
        first: {},
        second: {}
    };

    list = list || [];
    list = camelCase(list);

    _.forEach(list, (item) => {
        _.forEach(item, (obj) => {
            formatData.first[obj.categoryId] = obj.categoryName;

            if (obj.sub) {
                _.forEach(obj.sub, (sub) => {
                    formatData.second[sub.categoryId] = sub.categoryName;
                });
            }

        });
    });

    return formatData;
};


/**
 * 获取商品数据
 */
const getSearchData = (params) => {
    return _searchSales(params).then((result) => {
        if (result && result.code === 200) {
            return productProcess.processProductList(result.data.product_list || []);
        } else {
            logger.error('商品搜索返回 code 不是 200');
            return [];
        }
    });
};

/**
 * 获取筛选数据
 * @param  {[object]} params
 * @return {[array]}
 */
const getFilterData = (params) => {
    return _searchSales(params).then((result) => {
        if (result && result.code === 200) {
            return productProcess.processFilter(result.data.filter || []);
        } else {
            logger.error('商品搜索返回 code 不是 200');
            return [];
        }
    });
};

/**
 * 获取所有的品牌名称
 **/
const getAllBrandNames = () => {
    return api.get('', {
        method: 'app.brand.brandlist'
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            return _processBrandNames(result.data.brands);
        } else {
            logger.error('品牌名称接口返回 code 不是 200');
            return {};
        }
    });
};

/**
 * 获取所有的品类名称
 **/
const getClassNames = () => {
    return api.get('', {
        method: 'app.sort.get'
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            return _processClassNames(result.data);
        } else {
            logger.error('品类名称接口返回 code 不是 200');
            return {};
        }
    });
};

module.exports = {
    getSearchData,
    getFilterData,
    getAllBrandNames,
    getClassNames
};