recommend-for-you.js 2.39 KB
'use strict';

var api = global.yoho.API;
const helpers = global.yoho.helpers;
const _ = require('lodash');

/**
 * 分享页面基础参数
 * @param  {object} sizeInfo [接口原始数据]
 * @return {object}          [description]
 */
const getPreferenceData = (data) => {
    var dest = {};

    let list = data.data || {};

    // list = camelCase(list);

    let distGoods = [];

    _.forEach(list.productList, function(value) {

        if (!value.productSkn || !value.goodsList || !value.goodsList.length) {
            return;
        }
        value.goodsId = value.goodsList[0].goodsId;

        // let goods = value;

        let goods = [];

        goods.push({
            productSkn: value.product_skn,
            productName: value.product_name,
            defaultImages: value.default_images,
            isSoonSoldOut: value.is_soon_sold_out,
            url: helpers.urlFormat(`/product/pro_${value.product_id}_${value.goodsId}/${value.cn_alphabet}.html`),
            marketPrice: value.market_price,
            salesPrice: value.sales_price
        });

        // goods.url = helpers.urlFormat(`/product/pro_${value.product_id}_${value.goodsId}/${value.cn_alphabet}.html`);

        if (_.get(goods, 'tags[0]', null)) {
            goods.tags = [];
            if (goods.isNew === 'Y') {
                goods.tags.push({is_new: true});
            } else if (goods.is_advance === 'Y') {
                goods.tags.push({isAdvance: true});
            } else if (goods.is_discount === 'Y') {
                goods.tags.push({isDiscount: true});
            } else if (goods.is_yohoood === 'Y') {
                goods.tags.push({isYohoood: true});
            } else if (goods.is_limited === 'Y') {
                goods.tags.push({isLimited: true});
            }
        }
        distGoods.push(goods);
    });



    dest.code = list.code;
    dest.goods = distGoods;
    dest.rec_id = list.recId;
    dest.message = list.message;

    // 清空变量,释放内存
    data = {};
    return dest;
};

/**
 * 获取为你优选数据
 */
exports.getPreference = (data) => {
    var defaultParam = {
            method: 'app.home.newPreference'
        },
        infoData = Object.assign(defaultParam, data); // 处理完成后,发给后端

    return api.get('', infoData).then(result => {

        return getPreferenceData(result);
    }); // 所有数据返回一个 Promise,方便 Promise.all 调用
};