newsale.js 3.38 KB
'use strict';

const api = global.yoho.API;
const _ = require('lodash');
const serviceAPI = global.yoho.ServiceAPI;
const logger = global.yoho.logger;

const getHotRank = (codeKey) => {
    return serviceAPI.get('operations/api/v5/resource/get', {
        content_code: codeKey
    }, {
        cache: true
    }).then((result) => {

        if (result && result.code === 200) {
            let formData = {};

            if (result.data[0].data.length === 1) {
                formData.url = result.data[0].data[0].url;
                formData.img = result.data[0].data[0].src;
            } else {
                let list = [];

                _.forEach(result.data[0].data, function(val) {
                    list.push({
                        url: val.url,
                        img: val.src
                    });
                });

                formData.list = list;
            }
            return formData;
        } else {
            logger.error('getHotRank data return code is not 200');
            return {};
        }
    });
};

const selectHotrank = (yhChannel, gender, sort, tabId, limit, page, notab) => {
    let param = {
        method: 'app.search.top',
        gender: gender,
        yh_channel: yhChannel,
        page: page,
        limit: limit
    };

    if (sort) {
        param.sort = sort;
    }

    if (tabId) {
        param.tab_id = tabId;
    }

    return api.get('', param, {
        cache: true
    }).then((result) => {

        if (result && result.code === 200 && result.data.product_list) {

            let formData = {};

            if (notab && parseInt(notab, 10) === 0 && result.data.tabs) {
                let tabs = [];

                _.forEach(result.data.tabs, function(val, index) {
                    let obj = {};

                    if (index === 0) {
                        obj = _.assign(obj, {
                            focus: true
                        });
                    }

                    obj = _.assign(obj, {
                        title: val,
                        dataId: index
                    });

                    tabs.push(obj);
                });

                formData.tabs = tabs;
            }

            if (result.data.product_list && result.data.product_list.length !== 0) {

                let goods = [];

                _.forEach(result.data.product_list, function(data, index) {
                    let one = {};

                    if (data === null || !data.product_skn || !data.goods_list[0]) {
                        return false;
                    }

                    one = _.assign(one, {
                        url: '/product/pro_' + data.product_id + '_' + data.goods_list[0].goods_id +
                             '/' + data.cn_alphabet + '.html',
                        thumb: data.default_images,
                        rank: limit * (page - 1) + index + 1,
                        name: data.product_name,
                        salePrice: data.sales_price
                    });

                    if (data.sales_phrase) {
                        one = _.assign(one, {
                            sales_phrase: data.sales_phrase
                        });
                    }
                    goods.push(one);
                });

                formData.goods = goods;
            }

            return formData;
        }

    });
};

module.exports = {
    getHotRank,
    selectHotrank
};