recent-view.js 2.41 KB
/**
 * recent view model
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/10/11
 */

'use strict';

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

/**
 * 处理商品
 */
const _handleProduct = (products) => {
    return products.map((product) => {
        return {
            href: helper.getUrlBySkc(product.product_skn),
            thumb: product.default_images,
            name: product.product_name,
            price: product.sales_price,
            productId: product.product_id
        };
    });
};

const index = (skn, limit) => {

    return api.get('', {
        method: 'h5.product.batch',
        productSkn: skn,
        limit: limit
    }).then(result => {

        if (result.code === 200) {
            let data = [];
            let historyProduct = result.data.product_list;

            _.forEach(historyProduct, hp => {
                if (!hp) {
                    return;
                }

                let mp = hp.market_price;
                let sp = hp.sales_price;

                let defaultGoods = _.find(hp.goods_list, {is_default: 'Y'});

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

                data.push({
                    market_price: mp === sp ? '' : ${helper.round(mp, 2)}`,
                    price: ${helper.round(sp, 2)}`,
                    product_name: hp.product_name,
                    url: helper.getUrlBySkc(hp.product_skn),
                    pic_url: helper.image(defaultGoods.images_url, 280, 382, 2, 70)
                });
            });

            return {
                code: 200,
                data: data,
                message: result.message
            };
        } else {

            // get list error
            return {
                code: result.code,
                message: result.message,
                data: []
            };
        }
    });
};

/**
 * [为你优选]
 * @param  {[type]} params [(yh_channel、uid、udid、rec_pos、limit)]
 * @return {[type]}        [{}]
 */
const recommend = (params) => {
    params.limit = params.limit || 30;

    return api.get('', Object.assign({
        method: 'app.home.newPreference'
    }, params)).then(d => {
        return _handleProduct(_.get(d, 'data.product_list', []));
    });
};

module.exports = {
    index,
    recommend
};