detail-hotarea-service.js 2.15 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 api = require('./detail-hotarea-api');

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

        if (!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(
                        _.head(goods.goodsImagesList).productId,
                        _.head(goods.goodsImagesList).goodsId,
                        cur.product.cnAlphabet
                    )
                };
                acc.push(point);

                return acc;
            }, []);

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

        }, []);

    })();
};

module.exports = {
    indexAsync
};