search.js 2.16 KB
/**
 * search model
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/07/21
 */
'use strict';
const utils = '../../../utils';
const logger = global.yoho.logger;
const productProcess = require(`${utils}/product-process`);
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 _searchGoods = (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
    });
};

/**
 * 获取商品数据
 */
const getSearchData = (params) => {
    return _searchGoods(params).then((result) => {
        if (result && result.code === 200) {
            return productProcess.processProductList(result.data.product_list || []);
        } else {
            logger.error('get product search api return code is not 200');
            return [];
        }
    });
};

/**
 * 获取筛选数据
 * @param  {[object]} params
 * @return {[array]}
 */
const getFilterData = (params) => {
    return _searchGoods(params).then((result) => {
        if (result && result.code === 200) {
            return productProcess.processFilter(result.data.filter || []);
        } else {
            logger.error('get filter data api return code is not 200');
            return [];
        }
    });
};

module.exports = {
    getSearchData,
    getFilterData
};