search.js 3.89 KB
/**
 * search
 * @Author: sefon 2016-7-12 16:31:56
 */

'use strict';
const utils = '../../../utils';
const api = global.yoho.API;
const searchApi = require('./search-api');
const headerModel = require('../../../doraemon/models/header');
const productProcess = require(`${utils}/product-process`);
const searchHandler = require('./search-handler');
const _ = require('lodash');
const needParams = ['query', 'msort', 'misort'];

exports.getKeyActivity = (query) => {
    return searchApi.getKeyActivityAsync(query).then(result => {
        return _.get(result, 'data.data.urlobj.appUrl', '');
    });
};

/**
 * 获取搜索数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
exports.getSearchData = (params, channel) => {

    let searchParams = searchHandler.getSearchParams(params);

    // 调用接口
    let apiMethod = [
        headerModel.requestHeaderData(channel),
        searchApi.getSortList(Object.assign({}, searchParams, {msort: '', misort: ''})),
        searchApi.getProductList(searchParams),
        searchApi.getShopList(params)
    ];

    // 搜索店铺
    /* if (params.query) {
        apiMethod.push(searchApi.getBrandShop(params.query));
    }*/

    return api.all(apiMethod).then(result => {
        let finalResult = {
            headerData: Object.assign(result[0].headerData, {
                header: true
            }),
            search: {}
        };

        // 获取左侧类目数据
        if (result[1].code === 200) {
            let dps = {};

            _.forEach(needParams, (value) => {
                if (params[value]) {
                    dps[value] = params[value];
                }
            });

            finalResult.search = {
                leftContent: searchHandler.handleSortData(result[1].data.sort, dps)
            };
        }

        // 获取商品数据和顶部筛选条件
        if (result[2].code === 200) {
            Object.assign(finalResult.search,
                searchHandler.handlePathNavData({total: result[2].data.total }, params, 'search', channel),
                {
                    filters: searchHandler.handleFilterDataAll(result[2].data, params),
                    opts: searchHandler.handleOptsData(params, result[2].data.total, result[2].data.filter),
                    totalCount: result[2].data.total,
                    footPager: searchHandler.handlePagerData(result[2].data.total, params),
                    goods: productProcess.processProductList(result[2].data.product_list,
                        Object.assign({showDiscount: false, from: {type: 'search', params: params}}, params)),
                    latestWalk: 6,
                    hasNextPage: searchHandler.handleNextPage(params, result[2].data.total),
                    shopEntry: result[3]
                });

            finalResult.criteo = {skn: searchHandler.getCriteo(_.get(finalResult.search, 'goods'))};
        }
        return finalResult;
    });
};

/**
 * 搜索提示
 */
exports.getSuggest = (params) => {
    return searchApi.getSuggest(params).then(result => {
        let dest = [];

        if (result.code === 200) {
            dest = searchHandler.handleSuggest(result.data);
        }

        return dest;
    });
};

exports.getBrands4Filter = (params) => {

    let nparams = {};

    if (params && params.price) {
        let mp = params.price.split(',');
        let nmp = [];

        nmp[0] = (mp && mp[0]) || 0;
        nmp[1] = (mp && mp[1]) || 99999;
        nparams.price = nmp.join(',');
    }

    // 生成路径包含callback 导致多次调用多个 callback问题
    if (params && params.callback) {
        delete params.callback;
    }

    return searchApi.getBrands4Filter(Object.assign({}, params, nparams)).then(result => {

        let dest = [];

        if (result.code === 200) {
            dest = searchHandler.handleFilterBrands(result.data, params);
        }

        return dest;
    });
};