search.js 1.79 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 publicHandler = require('./public-handler');

// const _ = require('lodash');
//
// const log = global.yoho.logger;

/**
 * 获取搜索数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
exports.getSearchData = (params) => {
    return api.all([
        headerModel.requestHeaderData(),
        searchApi.getSortList(),
        searchApi.getProductList(params)
    ]).then(result => {
        let finalResult = {
            headerData: Object.assign(result[0].headerData, {
                header: true
            }),
            search: {}
        };

        // 获取商品数据和顶部筛选条件
        if (result[1].code === 200) {
            finalResult.search.leftContent = publicHandler.handleSaleSortData(result[1].data.filter.group_sort, params);
            finalResult.search.pathNav = publicHandler.handlePathNavData(result[1].data.filter.group_sort, params);

        }

        // 获取左侧类目数据
        if (result[2].code === 200) {
            Object.assign(finalResult.search, {
                filters: publicHandler.handleSaleFilterData(result[2].data.filter, params),
                opts: publicHandler.handleSaleOptsData(params, result[2].data.total),
                totalCount: result[2].data.total,
                footPager: publicHandler.handlePagerData(result[2].data.total, params),
                goods: productProcess.processProductList(result[2].data.product_list)
            });
        }
        return finalResult;
    });
};