intro.js 10.4 KB
/*
 * @Author: Targaryen
 * @Date:   2016-05-18 10:02:21
 * @Last Modified by:   Targaryen
 * @Last Modified time: 2016-05-18 11:39:11
 */

'use strict';

const _ = require('lodash');

// const log = require(`${library}/logger`);

var api = global.yoho.API;

/**
 * 商品尺码信息处理
 * @param  {object} sizeInfo [接口原始数据]
 * @return {object}          [description]
 */
const getSizeInfo = (sizeInfo) => {
    var dest = {},
        temp = {};

    dest.goodsDescription = {};
    dest.goodsDescription.detail = {};
    dest.goodsDescription.detail.list = [];

    if (!_.isEmpty(sizeInfo.productDescBo.erpProductId)) {
        let sex = '通用';

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

        dest.goodsDescription.title = '商品信息';
        dest.goodsDescription.enTitle = 'DESCRIPTION';
        temp = {};
        temp.param = '编号:' + sizeInfo.productDescBo.erpProductId;
        dest.goodsDescription.detail.list.push(temp);
        temp = {};
        temp.param = '颜色:' + sizeInfo.productDescBo.colorName;
        dest.goodsDescription.detail.list.push(temp);
        temp = {};
        temp.param = '性别:' + sex;
        dest.goodsDescription.detail.list.push(temp);
    }

    if (!_.isEmpty(sizeInfo.productDescBo.standardBos)) {
        _.forEach(sizeInfo.productDescBo.standardBos, function(value) {
            temp = {};
            temp.param = value.standardName + ':' +
                value.standardVal;

            dest.goodsDescription.detail.list.push(temp);
        });
    }
    if (_.isEmpty(sizeInfo.phrase)) {
        dest.goodsDescription.desc = sizeInfo.phrase;
    }

    // 尺码信息
    if (!_.isEmpty(sizeInfo.sizeInfoBo)) {
        dest.sizeInfo = {};
        dest.sizeInfo.title = '尺码信息';
        dest.sizeInfo.enTitle = 'SIZE INFO';
        dest.sizeInfo.detail = {};
        dest.sizeInfo.detail.list = [];

        // 参考尺码
        let boyReference = !_.isEmpty(sizeInfo.productExtra.boyReference);
        let girlReference = !_.isEmpty(sizeInfo.productExtra.girlReference);
        let gender = !_.isEmpty(sizeInfo.productDescBo.gender) ? sizeInfo.productDescBo.gender : 3;
        let referenceName = '参考尺码';

        if ((gender === 1 && boyReference) || (gender === 2 && girlReference)) {
            referenceName = '参考尺码';
        } else if (gender === 3 && boyReference) {
            referenceName = '参考尺码(男)';
        } else if (gender === 3 && girlReference) {
            referenceName = '参考尺码(女)';
        }

        let referenceList = [];

        // 判断是否显示参考尺码
        let showReference = (boyReference &&
            !_.isEmpty(sizeInfo.sizeInfoBo.sizeBoList[0].boyReferSize)) || (girlReference &&
            !_.isEmpty(sizeInfo.sizeInfoBo.sizeBoList[0].girlReferSize));

        if (showReference) {
            referenceList[0] = {};
            referenceList[0].param = referenceName;
        }

        if (!_.isEmpty(sizeInfo.sizeInfoBo.sizeAttributeBos)) {

            let sizeNameList = [];

            temp = {};
            temp.param = '吊牌尺码';
            sizeNameList.push(temp);

            let sizeBoGroup = {};

            _.forEach(sizeInfo.sizeInfoBo.sizeAttributeBos, function(attr) {
                sizeBoGroup[attr.id] = [];
                sizeBoGroup[attr.id][0] = {};
                sizeBoGroup[attr.id][0].param = _.isEmpty(attr.attributeName) ?
                    ' ' : attr.attributeName;
            });


            _.forEach(sizeInfo.sizeInfoBo.sizeBoList, function(value) {

                temp = {};
                temp.param = value.sizeName;
                sizeNameList.push(temp);

                if (boyReference && (gender === 1 || gender === 3)) {
                    temp = {};
                    temp.param = _.isEmpty(value.boyReferSize.referenceName) ?
                        ' ' : value.boyReferSize.referenceName;
                    referenceList.push(temp);
                } else if (girlReference && (gender === 2 || gender === 3)) {
                    temp = {};
                    temp.param = _.isEmpty(value.girlReferSize.referenceName) ?
                        ' ' : value.girlReferSize.referenceName;
                    referenceList.push(temp);
                } else {
                    showReference = false;
                }

                _.forEach(value.sortAttributes, function(attr) {
                    temp = {};
                    temp.param = _.isEmpty(attr.sizeValue) ? ' ' : attr.sizeValue;
                    sizeBoGroup[attr.id].push(temp);
                });
            });

            // 根据模板页面的显示,按表格一列一列来显示
            dest.sizeInfo.detail.list[0] = {};
            dest.sizeInfo.detail.list[0].params = sizeNameList;
            if (showReference) {
                dest.sizeInfo.detail.list[1].params = referenceList;
            }
            _.forEach(sizeBoGroup, function(value) {
                temp = {};
                temp.params = value;
                dest.sizeInfo.detail.list.push(temp);
            });
        } else {
            dest.sizeInfo.detail.list[0] && (dest.sizeInfo.detail.list[0].params[0] = '');
        }
    }

    // 测量方式
    if (!_.isEmpty(sizeInfo.sizeImage)) {
        dest.measurementMethod = {};

        dest.measurementMethod.title = '测量方式';
        dest.measurementMethod.enTitle = 'MEASUREMENT METHOD';
        dest.measurementMethod.img = sizeInfo.sizeImage;
    }

    // 模特试穿, 竖着输出排列显示
    if (!_.isEmpty(sizeInfo.modelBos)) {
        let reference = {
            title: '模特试穿',
            enTitle: 'REFERENCE'
        };

        dest.reference = reference;

        // 控制是否显示备注
        let showRemark = false;
        let remarkList = [{
            param: '备注'
        }];

        let detail = {
            list: [{
                params: [{
                    param: ''
                }]
            }, {
                params: [{
                    param: '模特'
                }]
            }, {
                params: [{
                    param: '身高'
                }]
            }, {
                params: [{
                    param: '体重'
                }]
            }, {
                params: [{
                    param: '三围'
                }]
            }, {
                params: [{
                    param: '吊牌尺码'
                }]
            }, {
                params: [{
                    param: '试穿描述'
                }]
            }]
        };

        reference.detail = detail;
        _.forEach(sizeInfo.modelBos, function(value) {
            detail.list[0].params.push({
                param: value.avatar.replace('http://', '//')
            });
            detail.list[1].params.push({
                param: value.modelName
            });
            detail.list[2].params.push({
                param: value.height
            });
            detail.list[3].params.push({
                param: value.weight
            });
            detail.list[4].params.push({
                param: value.vitalStatistics
            });
            detail.list[5].params.push({
                param: value.fitModelBo && value.fitModelBo.fit_size
            });
            detail.list[6].params.push({
                param: value.fitModelBo && value.fitModelBo.feel
            });

            if (value.fitModelBo && value.fitModelBo.fit_remark) {
                showRemark = true;
                remarkList.push({
                    param: value.fitModelBo && value.fitModelBo.fit_remark
                });
            } else {
                remarkList.push({
                    param: ''
                });
            }
        });

        // 显示模特备注
        if (showRemark) {
            detail.list[7] = {};
            detail.list[7].params = remarkList;
        }

    }

    // 商品材质
    if (!_.isEmpty(sizeInfo.productMaterialList)) {
        dest.materials = {};
        dest.materials.title = '商品材质';
        dest.materials.enTitle = 'MATERIALS';
        dest.materials.list = [];

        _.forEach(sizeInfo.productMaterialList, function(value) {
            temp = {};
            temp.img = value.imageUrl;
            temp.desc = value.remark;
            dest.materials.list.push(temp);
        });

    }

    // 洗涤提示
    if (!_.isEmpty(sizeInfo.washTipsBoList)) {
        dest.washTips = {};
        dest.washTips.list = [];
        _.forEach(sizeInfo.washTipsBoList, function(value) {
            dest.washTips.list.push(value);
        });
    }

    // 详情配图

    if (!_.isEmpty(sizeInfo.productIntroBo.productIntro)) {
        let productIntro = '';

        if (!_.isEmpty(sizeInfo.productDescBo.phrase)) {
            productIntro = productIntro + sizeInfo.productDescBo.phrase +
                '<br />';
        }
        productIntro = productIntro + sizeInfo.productIntroBo.productIntro;
        if (!_.isEmpty(productIntro) && productIntro !== '') {
            dest.productDetail = {};
            dest.productDetail.title = '商品详情';
            dest.productDetail.enTitle = 'DETAILS';

            dest.productDetail.desc = productIntro.replace(/\r\n\t/g, '').
                replace(/<\/p>/g, '').
                replace(/<img src=/g, '<img class="lazy" src="data:image/gif;' +
                    'base64,R0lGODlhAQABAJEAAAAAAP///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw=="' +
                    ' data-original=').
                replace(/<img border="0" src=/g, '<img border="0" class="lazy" ' +
                    'src="data:image/gif;base64,' +
                    'R0lGODlhAQABAJEAAAAAAP///93d3f///yH5BAEAAAMALAAAAAABAAEAAAICVAEAOw=="' +
                    ' data-original=').
                replace(/.jpg/g, '.jpg?imageMogr2/thumbnail/750x/quality/90');
        }

        // 清空变量,释放内存
        sizeInfo = {};
    }
    return dest;
};

let getintroData = (data, req) => {
    var finalResult;

    return api.get('', {
        method: 'h5.product.intro',
        productskn: data.productskn,
        udid: req.sessionID || 'yoho'
    }).then(result => {
        finalResult = getSizeInfo(result);

        return finalResult;
    });
};

module.exports = {
    getintroData
};