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

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

/**
 * 设置品牌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;
};

/**
 * 设置品牌介绍
 * @param  brand 品牌相关数据
 * @return Object
 */
const setBrandIntro = brand => {
    let barndIntro = {};

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

/**
 * 设置商品描述
 * @param  brand 品牌相关数据
 * @return Object
 */
const setDescriptionInfo = (sizeInfo, comfortInfo) => {
    let resData = {};

    if (_.has(sizeInfo, 'productDescBo.erpProductId')) {
        let description = {
            titleEn: 'DESCRIPTION',
            titleCn: '商品信息'
        };
        let sex;

        switch (sizeInfo.productDescBo.gender) {
            case 1:
                sex = '男款';
                break;
            case 2:
                sex = '女款';
                break;
            default:
                sex = '通用';
                break;
        }

        let basic = [
            { key: '编号', value: sizeInfo.productDescBo.erpProductId },
            { key: '颜色', value: sizeInfo.productDescBo.colorName },
            { key: '性别', value: sex }
        ];

        if (_.isArray(sizeInfo.productDescBo.standardBos)) {
            _.forEach(sizeInfo.productDescBo.standardBos, function(value) {
                basic.push({
                    key: value.standardName,
                    value: value.standardVal
                });
            });
        }

        description.basic = basic;

        if (comfortInfo) {
            let comfort = [];

            _.forEach(comfortInfo, function(value) {
                let i = 1;
                let blocks = [];
                let flag = false;

                do {
                    if (i === +value.wearSense.value) {
                        flag = true;
                        blocks.push({
                            cur: true
                        });
                    } else {
                        blocks.push({});
                    }
                    i++;
                } while (i <= 5);

                if (flag) {
                    comfort.push({
                        name: value.caption.caption,
                        minDes: value.caption.low,
                        blocks: blocks,
                        maxDes: value.caption.high
                    });
                }
            });

            description.comfort = comfort;
        }

        resData = {
            description: description
        };
    }

    return resData;
};

module.exports = {
    setBrandBanner,
    setProductData,
    setBrandIntro,
    setDescriptionInfo
};