intro.js 9.2 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 library = '../../../library';
const API = require(`${library}/api`).API;
const sign = require(`${library}/sign`);
const _ = require('lodash');

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

var api = new 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].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)) {
        dest.reference = {};
        dest.reference.title = '模特试穿';
        dest.reference.enTitle = 'REFERENCE';
        dest.reference.detail = {};
        dest.reference.detail.list = {};

        // 控制是否显示备注
        // let showRemark = false;
        let remarkList = {};

        remarkList[0] = {};
        remarkList[0].param = '备注';

        for (let i = 0; i < 7; i++) {
            dest.reference.detail.list[i] = {};
            dest.reference.detail.list[i].params = {};
            dest.reference.detail.list[0].params[0] = {};
        }
        dest.reference.detail.list[0].params[0].param = '';
        dest.reference.detail.list[1].params[0].param = '模特';
        dest.reference.detail.list[2].params[0].param = '身高';
        dest.reference.detail.list[3].params[0].param = '体重';
        dest.reference.detail.list[4].params[0].param = '三围';
        dest.reference.detail.list[5].params[0].param = '吊牌尺码';
        dest.reference.detail.list[6].params[0].param = '试穿描述';

        // _.forEach(sizeInfo.modelBos, function(value) {
        //     // 待处理
        // });

        // 显示模特备注
        // if (showRemark) {

        // }

    }

    // 商品材质
    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;
};

module.exports = (data) => {
    var finalResult;

    return api.get('', sign.apiSign({
        method: 'h5.product.intro',
        productskn: data.productskn,
        udid: 'f528764d624db129b32c21fbca0cb8d6'
    })).then(result => {
        finalResult = getSizeInfo(result);

        return finalResult;
    });
};