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

/**
 * 品牌名称处理
 * @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.toLowerCase(),
                brandName: obj.brandName.toLowerCase()
            });
        });
    });

    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;
};

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

    /* tar add 160823 店铺销售类目 */
    if (params.filter_poolId) {
        method = 'app.search.pool';
        Object.assign(params, {
            productPool: params.filter_poolId
        });
        delete params.filter_poolId;
    }

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

    if (params.channel) {
        params.yh_channel = searchProcess.getChannelType(params.channel);
        delete params.channel;
    }

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

    if (params.order) {
        params.order = searchProcess.getTypeCont(params.type || '', params.order);
    } else {
        params.order = 's_t_asc';
    }

    return api.get('', _.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 [];
        }
    });
};

/**
 * 获取筛选数据
 * @param  {[object]} params
 * @return {[array]}
 */
const getFilterSearchData = (params) => {
    return _searchGoods(params).then((result) => {
        if (result && result.code === 200) {
            return result.data;
        } else {
            logger.error('get filter data api return code is not 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('get category name api return code is not 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('get brand all name data api return code is not 200');
            return {};
        }
    });
};

/**
 * 获取热门搜索
 **/
const getSearchIndex = () => {
    return api.get('', {
        method: 'app.search.getTerms'
    }).then((result) => {
        if (result && result.code === 200) {
            return result.data.hotTerms;
        } else {
            logger.error('Hot Search return code is not 200');
            return {};
        }
    });
};

/**
 * 获取联想词
 * @param params
 * @returns {*|Promise.<TResult>}
 */
const getFuzzyDatas = (params) => {
    return api.get('', {
        method: 'app.search.fuzzy',
        keyword: params
    }).then((result) => {
        if (result && result.code === 200) {
            return result.data;
        } else {
            logger.error('FuzzyDatas return code is not 200');
            return {};
        }
    });
};

/**
 * 查询关键词是否正参加活动
 * @param params
 * @returns {*|Promise.<TResult>}
 */
const searchKeyActivity = (params) => {
    return api.get('', {
        method: 'app.search.word',
        query: params
    }, {code: 200}).then(result => {
        if (result.data) {
            return result.data;
        } else {
            return {};
        }
    });
};

module.exports = {
    getSearchData,
    getFilterData,
    getFilterSearchData,
    getAllBrandNames,
    getClassNames,
    getSearchIndex,
    getFuzzyDatas,
    searchKeyActivity
};