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

'use strict';

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

const utils = require('./utils');

/**
 * 处理商品
 */
function _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
        };
    });
}

function index(skn, limit) {

    return this.get({
        data: {
            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 goodsImg = hp.default_images || '';

                if (goodsImg && goodsImg.indexOf('static.') < 0) {
                    goodsImg = utils.getUploadImgAbsoluteUrl(goodsImg, 'goodsimg');
                }

                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: goodsImg && helper.image(goodsImg, 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]}        [{}]
 */
function recommend(params) {
    params.limit = params.limit || 30;

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

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);

        this.index = index.bind(this);
        this.recommend = recommend.bind(this);
    }
};