detail-hotarea-service.js 2.36 KB
/**
 * Created by TaoHuang on 2016/6/14.
 */

'use strict';

const Promise = require('bluebird');
const co = Promise.coroutine;
const _ = require('lodash');
const helpers = global.yoho.helpers;
const HorareaModel = require('./detail-hotarea-api');

module.exports = class extends global.yoho.BaseModel {
    constructor(ctx) {
        super(ctx);
        this.api = new HorareaModel(ctx);
    }

    /**
     * 获取某一个商品的热区数据
     */
    indexAsync(pid) {
        return co(function *() {
            let data = yield this.api.indexAsync(pid);

            if (!data || !data.code || data.code !== 200) {
                return [];
            }

            return data.data.reduce((result, area) => {
                if (!area.infos) {
                    return result;
                }

                let item = {};

                if (area.imageUrl) {
                    item.img = helpers.getForceSourceUrl(area.imageUrl);
                }

                item.list = area.infos.reduce((acc, cur, index) => {
                    if (!cur.product || !cur.product.goodsList) {
                        return acc;
                    }

                    let point = {
                        label: index + 1,
                        top: cur.top,
                        left: cur.left,
                        height: cur.height,
                        width: cur.width
                    };

                    let goods = _.head(cur.product.goodsList);

                    // 封面图
                    point.img = helpers.getForceSourceUrl(goods.colorImage, 60, 60);

                    // 商品相关信息
                    point.product = {
                        salePrice: cur.product.productPriceBo.formatSalesPrice,
                        marketPrice: cur.product.productPriceBo.formatMarketPrice,
                        productName: cur.product.productName,
                        href: helpers.getUrlBySkc(
                            _.get(cur.product, 'productPriceBo.productSkn', ''))
                    };
                    acc.push(point);

                    return acc;
                }, []);

                if (_.isEmpty(item)) {
                    return result;
                } else {
                    result.push(item);
                    return result;
                }

            }, []);

        }).bind(this)();
    }
};