search.js 2.63 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');

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

    let mlimit = params && params.limit && params.limit > 0 ? params.limit - 1 : 59;

    // 调用接口
    let apiMethod = [
        headerModel.requestHeaderData(),
        searchApi.getSortList(),
        searchApi.getProductList(Object.assign({}, params, {limit: mlimit}))
    ];

    // 搜索店铺
    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) {
            finalResult.search = {
                leftContent: searchHandler.handleSortData(result[1].data.sort, params)
            };
        }

        // 获取商品数据和顶部筛选条件
        if (result[2].code === 200) {
            Object.assign(finalResult.search, {
                pathNav: searchHandler.handlePathNavData({total: result[2].data.total }, params, 'search'),
                filters: searchHandler.handleFilterData(result[2].data.filter, 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}, params)),
                latestWalk: 7,
                hasNextPage: searchHandler.handleNextPage(params, result[2].data.total)//,
                //brandBanner :  isset($data['brand']) && !empty($data['brand']) ? self::getBannerFormat($data['brand'], $options['brandBanner']) : array();
            });
        }
        return finalResult;
    });
};

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

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

        return dest;
    });
};