chanpin.js 2.44 KB
'use strict';

const utils = '../../../utils';
const redis = require(`${utils}/redis`);
const _ = require('lodash');
const helpers = global.yoho.helpers;
const camelCase = global.yoho.camelCase;

class List extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
    }

    list(params) {
        let options = {
            data: {
                method: 'web.search.forseo',
                sales: params.sales,
                outlets: params.outlets,
                stocknumber: params.stocknumber,
                need_filter: params.need_filter,
                query: params.query,
                type: params.type,
                page: params.page || 1,
                limit: params.limit || 24,
                order: params.order || 0
            },
            param: {
                code: 200
            }
        };

        return this.get(options).then(result => {
            return result;
        });
    }

    getSearchKeywordDataById(params) {
        return redis.all([
            ['get', `global:yoho:seo:keywords:id:${params.id}`]
        ]).then(redisData => {
            return redisData;
        });
    }

    index(params) {
        return Promise.all([
            this.getSearchKeywordDataById(params),
        ]).then(result => {
            let resu = {
                list: [],
                fuzzyWord: []
            };

            if (_.get(result, '[0][0]')) {
                let build = [];
                let redisData = JSON.parse(result[0][0]);

                return this.list(Object.assign(params, {query: redisData.name})).then(listData => {
                    _.forEach(_.slice(redisData.data, 0, 12), value => {
                        build.push({
                            name: value.keyword,
                            link: helpers.urlFormat(`/mip/list/${value.id}.html`, null)
                        });
                    });
                    resu.name = redisData.name;
                    resu.fuzzyWord = build;
                    if (_.get(listData, 'data.product_list')) {
                        resu.list = camelCase(listData.data.product_list);
                        _.forEach(resu.list, value => {
                            value.isSoonSoldOut = value.isSoonSoldOut === 'Y';
                        });
                    }
                    return resu;
                });
            } else {
                return resu;
            }
        });
    }
}

module.exports = List;