search.js 7.04 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;

/**
 * 封面图
 * @type {{boys: string, gilrs: string}}
 */
const _coverChannel = {
    boys: '1,3',
    gilrs: '2,3'
};

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

    if (params.shop_id) {
        method = 'app.search.li';
    } else if (params.brand) {
        method = 'app.search.brand';
    }

    // 排除基本筛选项默认值为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;
    }

    if (params.query) {
        params.query = params.query.replace(/\+/g, ',');
    }

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

    // 学生返币查询
    if (params.coin) {
        method = 'app.student.rebate';
        delete params.filter_poolId;

        if (params.type === 'newest') {
            delete params.order;
            delete params.type;
        }
    }

    if (params.isblknew) {
        method = 'app.search.newProduct';
        params.app_type = 1;
    }

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

/**
 * 获取商品数据
 */
const getSearchData = (params) => {
    return _searchGoods(params).then((result) => {
        if (result && result.code === 200) {
            let newList = {};

            newList.list = productProcess.processProductList(result.data.product_list || [], {
                isApp: params.isApp || (params.appVersion && params.appVersion !== 'false'),
                gender: _coverChannel[params.coverChannel]
            });

            if (parseInt(params.page) === 1) {
                newList.total = result.data.total;
            }

            return newList;
        } 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'
    }, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            return result.data;
        } 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
    }, {
        cache: true
    }).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
    }, {
        cache: true,
        code: 200
    }).then(result => {
        if (result && result.data) {
            return result.data;
        } else {
            return {};
        }
    });
};

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