prefer.js 1.46 KB
'use strict';
const api = global.yoho.API;
const _ = require('lodash');
const config = global.yoho.config;

/**
 * 为你优选
 *
 * @param data
 */
const _procProductImg = (product) => {
    if (product.gender === '2,3' || product.gender === '2' || product.gender === '3') {
        return product.cover_2 || product.images_url || product.cover_1 || '';
    }

    return product.cover_1 || product.images_url || product.cover_2 || '';
};

const _processProduct = (list) => {
    let newRes = [];

    list = list || [];

    list.forEach(item => {
        let defaultGoods = _.find(item.goods_list, {is_default: 'Y'});

        // 无默认商品取商品列表第一个
        if (!defaultGoods) {
            defaultGoods = item.goods_list[0];
        }

        item.default_images = _procProductImg(defaultGoods);
        item.goodsUrl = `${config.siteUrl}/product/pro_${item.product_id}_${defaultGoods.goods_id}/${item.cn_alphabet}.html`; // eslint-disable-line
    });

    _.chunk(list, 5).forEach(item => {
        newRes.push({
            list: item
        });
    });
    return newRes;
};

exports.getlist = (params, uid) => {
    if (uid !== 0 && uid !== null) {
        params.uid = uid;
    }

    return api.get('', _.assign({
        method: 'app.home.newPreference',
        limit: 30,
        page: 1,
        rec_pos: 100003
    }, params), {
        code: 200,
        catch: true
    }).then(data => {
        return _processProduct(data.data.product_list);
    });
};