seo-service.js 4.28 KB


const redis = global.yoho.redis;
const helpers = global.yoho.helpers;
const logger = global.yoho.logger;

const _ = require('lodash');
const headerModel = require('../../../doraemon/models/header');

const SearchApi = require('./search-api');
const searchHandler = require('./search-handler');
const utils = '../../../utils';
const productProcess = require(`${utils}/product-process-simple`);

const _setHotKeywordData = (result, params, channel) => {
    let changeQuery = Object.assign({}, params);
    let finalResult = {
        headerData: Object.assign(result[0].headerData, {
            header: true
        })
    };

    _.unset(changeQuery, 'query');

    // 获取商品数据和顶部筛选条件
    if (result[1].code === 200) {
        let data = result[1].data;

        Object.assign(finalResult,
            searchHandler.handlePathNavData({total: data.total}, params, 'search', channel),
            {
                product: {
                    opts: searchHandler.handleOptsData(changeQuery, data.total),
                    totalCount: data.total,
                    footPager: searchHandler.handlePagerData(data.total, changeQuery),
                    goods: productProcess.processProductList(data.product_list,
                        Object.assign({showDiscount: false, from: {type: 'hot', params: params}}, params)),
                    hasNextPage: searchHandler.handleNextPage(changeQuery, data.total)
                }
            }
        );

        finalResult.hotBrands = _.get(data, 'filter.brand', []);

        finalResult.hotBrands.forEach((val) => {
            val.href = helpers.urlFormat(`/list/bd${val.id}.html`);

            return val;
        });

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


    if (result[2].code === 200) {
        let data = result[2].data;

        finalResult.latestWalkExtra = [{
            extraTabName: '相关推荐',
            extraGoodsList: productProcess.processProductList(data.product_list,
                Object.assign({showDiscount: false, from: {type: 'hot', params: params}}, params))
        }];
    }

    return finalResult;
};

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);

        this.searchApi = new SearchApi(ctx);
    }

    getSearchProduct(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;
        }

        searchParams.need_filter = 'yes';

        return Promise.all([
            headerModel.requestHeaderData(channel),
            this.searchApi.getSeoProductList(searchParams, 'fuzzySearch'),
            this.searchApi.getSeoProductList(Object.assign(searchParams, {
                order: 's_n_desc',
                limit: 5
            }), 'fuzzySearch'),
        ]).then(result => {
            return _setHotKeywordData(result, params, channel);
        });
    }

    getHotKeywordDate(id, params, channel) {
        return redis.all([
            ['get', `global:yoho:seo:hot:keywords:id:${id}`]
        ]).then(redisData => {
            let keyword = redisData[0];

            try {
                keyword = JSON.parse(keyword);
            } catch (e) {
                logger.debug('getProductList cache data parse fail.');
            }

            if (!_.get(keyword, 'name')) {
                return Promise.reject(`cannot find hot keywords by id(${id})`);
            }
            params.query = keyword.name;

            return this.getSearchProduct(params, channel).then(result => {
                result.hotKeys = (keyword.data || []).map(val => {
                    val.href = helpers.urlFormat(`/hot/${val.id}.html`);
                    return val;
                });

                result.keyword = keyword;
                result.latestWalk = 5;

                return result;
            });
        })
    }
};