item.js 5.22 KB
/**
 * 商品详情页model
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/7/11
 */
'use strict';

const _ = require('lodash');
const itemAPI = require('./item-api');
const helpers = global.yoho.helpers;

// const getForceSourceUrl = (img) => {
//     if (img) {
//         img = img.split('\?imageView')[0];
//         img = img.split('\?imageMogr2')[0];
//     }
//     return img;
// };

/**
 * 设置品牌banner数据
 * @param  brand 品牌相关数据
 * @return Object
 */
const _setBrandBanner = (brand) => {
    let data = {
        bgColor: '#93897d',
        brandLogo: {
            link: `#${brand.brandDomain}`, // 品牌跳转链接 -- 待处理
            img: brand.brandIco
        },
        brandHome: {
            link: `#${brand.brandDomain}` // 品牌跳转链接 -- 待处理
        }
    };

    return {
        brandBanner: data
    };
};

/**
 * 设置商品数据
 * @param  base 商品基础数据
 * @return Object
 */
const _setProductData = (base) => {
    let data = {
        name: base.productName,
        brandName: _.has(base, 'brand.brandName') ? base.brand.brandName : '',
        intro: base.salesPhrase,
        sellPrice: base.productPriceBo.salesPrice,
        marketPrice: base.productPriceBo.marketPrice,
        total: 0
    };

    let chooseSkuFlag;

    if (_.toNumber(data.sellPrice) >= _.toNumber(data.marketPrice)) {
        // 售价与吊牌价相同,只显示售价
        _.unset(data, 'marketPrice');
    }

    // 遍历颜色尺寸
    if (base.goodsList) {
        let goods = [];

        // 处理商品数据
        _.forEach(base.goodsList, function(value) {
            let group = {},
                thumbs = [],
                sizes = [];

            // 如果status为0,即skc下架时就跳过该商品
            if (!value.status) {
                return;
            }

            if (value.goodsImagesList) {
                group = {
                    name: value.colorName,
                    title: `${data.name} ${value.colorName}`,
                    total: 0
                };

                // 目前没有RGB值先以背景图方式实现
                group.rgb = `url('${helpers.image(value.colorImage, 30, 30)}')`;

                // 商品颜色列表
                _.forEach(value.goodsImagesList, function(subValue) {
                    thumbs.push(subValue.imageUrl);
                });
                group.thumbs = thumbs;

                // 缩略图空,不显示
                if (!thumbs.length) {
                    return;
                }

                // 默认第一张图片
                if (!_.has(data, 'img')) {
                    data.img = value.colorImage;
                }

                // 商品尺码列表
                _.forEach(value.goodsSizeBoList, function(subValue) {
                    let size = {
                        name: subValue.sizeName,
                        title: subValue.sizeName,
                        sku: subValue.goodsSizeSkuId,
                        num: _.toInteger(subValue.goodsSizeStorageNum),
                        goodsId: subValue.goodsId
                    };

                    // 虚拟商品,增加为一件
                    if (subValue.attribute === 3) {
                        size.num = size.num > 1 ? 1 : 0;
                    }

                    // 如果status为0,即skc下架时,则库存设为0
                    if (subValue.status === 0) {
                        size.num = 0;
                    }

                    // 单个sku商品的总数累加
                    group.total += size.num;

                    // 默认选中该sku商品
                    if (group.total > 0 && !chooseSkuFlag) {
                        group.focus = true; // 选中sku商品
                        chooseSkuFlag = true;
                    }

                    // 商品的总数累加
                    data.total += size.num;

                    sizes.push(size);
                });
                group.sizes = sizes;
            }

            goods.push(group);
        });

        if (goods.length && !chooseSkuFlag) {
            goods[0].focus = true;
        }

        data.colors = goods;
    }

    return data;
};

const _setBrandIntro = (brand) => {
    let barndIntro = {};

    if (brand) {
        barndIntro = {
            brand: {
                titleEn: 'BRAND',
                titleCn: '品牌介绍',
                logo: brand.brandIco,
                intro: brand.brandIntro
            }
        }
    }
    return barndIntro;
}

let getProductItemData = (params) => {
    let pid = params[0];

    // let gid = params[1];

    return Promise.all([
        itemAPI.getProductBaseAsync(pid)
    ]).then(result => {
        let data = {};

        if (!result[0].productName &&
            !result[0].erpProductId &&
            !result[0].productPriceBo) {
            return data;
        }

        if (result[0].brand) {
            Object.assign(data, _setBrandBanner(result[0].brand));

            Object.assign(data, _setBrandIntro(result[0].brand));
        }

        Object.assign(data, {
            goodInfo: _setProductData(result[0])
        });

        console.log(data);

        return data;
    });
};

module.exports = {
    getProductItemData
};