preference.js 1.53 KB
/*
 * @Author: Targaryen
 * @Date:   2016-05-18 11:42:11
 * @Last Modified by:   Targaryen
 * @Last Modified time: 2016-05-30 13:48:17
 */

'use strict';
const _ = require('lodash');

const api = global.yoho.API;
const helpers = global.yoho.helpers;

const yhchannelMap = {
    boys: '1',
    girls: '2',
    kids: '3',
    lifestyle: '4'
};

const _formatProduct = (data) => {
    let list = [];

    _.forEach(data, function(value) {
        if (!value.product_skn || !value.goods_list || !value.goods_list.length) {
            return;
        }
        value.goodsId = value.goods_list[0].goods_id;

        let goods = {
            salePrice: value.sales_price ? value.sales_price : '',
            price: value.market_price ? value.market_price : '',
            url: helpers.urlFormat(`/product/pro_${value.product_id}_${value.goodsId}/${value.cn_alphabet}.html`),
            thumb: value.default_images,
            name: value.product_name

        };

        // 市场价和售价一样,则不显示市场价
        if (goods.salePrice === goods.price) {
            goods.price = false;
        }

        list.push(goods);
    });

    return list;
};

module.exports = (data) => {
    let finalResult = {};

    return api.get('', {
        method: 'h5.preference.Search',
        productskn: data.productskn,
        yhchannel: yhchannelMap[data.yhchannel],
        brandId: data.brandId
    }).then(result => {
        if (result) {
            finalResult.recommendList = _formatProduct(result);
        }

        return finalResult;
    });
};