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

const utils = '../../../utils';
const productNameProcess = require(`${utils}/product-name-process`);
var api = global.yoho.API;
const helpers = global.yoho.helpers;
const _ = require('lodash');

/**
 * 分享页面基础参数
 * @return {object}          [description]
 * @param data
 */
const getPreferenceData = (data) => {
    let dest = {
        goods: []
    };

    let list = data.data || {};

    _.forEach(list && list.product_list, value => {

        if (!value.product_skn || !value.goods_list || !value.goods_list.length) {
            return;
        }
        value.goodsId = value.goods_list && value.goods_list[0] && value.goods_list[0].goods_id;

        if (value.cn_alphabet) {
            value.cn_alphabet = productNameProcess(value.cn_alphabet);
        }

        let goods = {
            product_skn: value.product_skn,
            product_name: value.product_name,
            default_images: value.default_images,
            is_soon_sold_out: value.is_soon_sold_out === 'Y',
            url: helpers.urlFormat(`/product/p${value.product_id}.html`),
            market_price: value.market_price,
            sales_price: value.sales_price
        };

        // if (_.get(value, 'tags[0]', null)) {
        //     goods.tags = [];
        //     if (goods.is_new === '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});
        //     }
        // }

        let tags = {};

        _.forEach(value.tags, tag => {
            tags[tag] = true;
        });

        // tags.is_solded = true;

        goods.tags = tags;

        if (tags.is_solded === true) {
            goods.is_solded = true;
        }

        dest.goods.push(goods);
    });



    dest.code = data.code;
    dest.rec_id = list.rec_id;
    dest.message = list.message;

    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 调用
};