search.js 4.2 KB
/**
 * Models-service
 * @author: gxh<xuhui.ge@yoho.cn>
 * @date: 2017/02/28
 */

'use strict';
const utils = '../../../utils';
const api = global.yoho.API;
const helpers = global.yoho.helpers;
const searchApi = require('./search-api');
const headerModel = require('../../../doraemon/models/header');

// const searchHandler = require('./search-handler');
const _ = require('lodash');
const needParams = ['query', 'msort', 'misort'];

/**
 * 获取搜索数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
// exports.getSearchData = (params, channel) => {
//     let searchParams = searchHandler.getSearchParams(params);
//
//     switch (channel) {
//         case 'boys':
//             searchParams.physical_channel = 1;
//             break;
//         case 'girls':
//             searchParams.physical_channel = 2;
//             break;
//         case 'kids':
//             searchParams.physical_channel = 3;
//             break;
//         case 'lifestyle':
//             searchParams.physical_channel = 4;
//             break;
//         default:
//             break;
//     }
//
//     // 调用接口
//     let apiMethod = [
//         headerModel.requestHeaderData(channel),
//         searchApi.getSortList(Object.assign({}, searchParams, {msort: '', misort: '', sort: ''})),
//         searchApi.getProductList(searchParams),
//         searchApi.getShopList(params)
//     ];
//
//     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, params)
//             };
//         }
//
//         // 获取商品数据和顶部筛选条件
//         if (result[2].code === 200) {
//
//             let filters = Object.assign(searchHandler.handleFilterDataAll(result[2].data, params),
//                 finalResult.search.leftContent.sort);
//
//             filters.checkedConditions.conditions = _.concat(filters.checkedConditions.conditions,
//                 finalResult.search.leftContent.checked);
//
//             Object.assign(finalResult.search,
//                 searchHandler.handlePathNavData({total: result[2].data.total }, params, 'search', channel),
//                 {
//                     filters: filters,
//                     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;
//     });
// };

/**
 * 处理搜索提示
 * @type {[type]}
 */
const handleSuggest = (data) => {
    let suggest = [];

    _.forEach(data, function(value) {
        suggest.push({
            href: helpers.urlFormat('', {query: value.keyword}, 'search'),
            keyword: value.keyword,
            count: value.count
        });
    });

    return suggest;
};


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

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

        return dest;
    });
};

module.exports = {
    handleSuggest,
    getSuggest
};