recommend.js 2.33 KB
/**
 * 推荐商品(店铺推荐|为您优选|最近浏览)model
 * @author: wsl<shuiling.wang@yoho.cn>
 * @date: 2016/11/21
 */

'use strict';

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

/**
 * 根据性别来决定  默认图片获取字段   如果是 2、3
 *
 * 则优先从cover_2 --》 cover_1 -- 》 images_url
 * 否则优先从cover_1 --》 cover_2 -- 》 images_url
 *
 */
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;
};

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

const getRecommendShop = (productSkn) => {
    return api.get('', {
        method: 'web.product.shopRecommend',
        limit: 20,
        page: 1,
        product_skn: productSkn
    }, {
        code: 200
    }).then(data => {
        return _processProduct(data.data.product_list);
    });
};

const recentPreview = (skn, limit) => {
    return api.get('', {
        method: 'h5.product.batch',
        productSkn: skn,
        limit: limit,
        page: 1
    }, {
        code: 200
    }).then(data => {
        return _processProduct(data.data.product_list);
    });
};

module.exports = {
    getRecommendProduct,
    getRecommendShop,
    recentPreview
};