recent-view.js 1.75 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 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.urlFormat(
                        `/product/pro_${hp.product_id}_${defaultGoods.goods_id}/${hp.cn_alphabet}.html`, '', 'item'),
                    pic_url: helper.image(defaultGoods.images_url, 150, 200, 2, 70)
                });
            });

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

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

module.exports = {
    index
};