intro.js 11.6 KB
/*
 * @Author: Targaryen
 * @Date:   2016-05-18 10:02:21
 * @Last Modified by: Targaryen
 * @Last Modified time: 2017-05-22 10:06:22
 */

'use strict';
const $ = require('cheerio');
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 (_.has(sizeInfo, 'productDescBo.erpProductId')) {
        let sex = '通用';

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

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

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

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

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

        // 参考尺码
        let boyReference = _.get(sizeInfo, 'productExtra.boyReference', null);
        let girlReference = _.get(sizeInfo, 'productExtra.girlReference', null);
        let gender = _.get(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 && sizeInfo.sizeInfoBo &&
            sizeInfo.sizeInfoBo.sizeBoList && sizeInfo.sizeInfoBo.sizeBoList[0] &&
            sizeInfo.sizeInfoBo.sizeBoList[0].boyReferSize) ||
            (girlReference && sizeInfo.sizeInfoBo && sizeInfo.sizeInfoBo.sizeBoList &&
            sizeInfo.sizeInfoBo.sizeBoList[0] && sizeInfo.sizeInfoBo.sizeBoList[0].girlReferSize);

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

        if (_.has(sizeInfo, 'sizeInfoBo.sizeAttributeBos')) {

            // [{param: attrName}]   th
            let sizeNameList = [];

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

            // {id: [{param: str},......]} sizeBoGroup[id][0] 属性名,  sizeBoGroup[id][index] 属性值
            let sizeBoGroup = {};

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

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

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

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

                    _.forEach(value.sortAttributes, function(attr) {
                        temp = {};
                        temp.param = 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] = {};
                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 (sizeInfo.sizeImage) {
        dest.measurementMethod = {};

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

    // 模特试穿, 竖着输出排列显示
    if (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 (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 (sizeInfo.washTipsBoList) {
        dest.washTips = {};
        dest.washTips.list = [];
        _.forEach(sizeInfo.washTipsBoList, function(value) {
            dest.washTips.list.push(value);
        });
    }

    // 详情配图
    if (_.has(sizeInfo, 'productIntroBo.productIntro')) {
        let productIntro = '';

        if (_.has(sizeInfo, 'productDescBo.phrase')) {
            productIntro = productIntro + sizeInfo.productDescBo.phrase +
                '<br />';
        }
        if (_.has(sizeInfo, 'productIntroBo.productIntro')) {
            productIntro = productIntro + sizeInfo.productIntroBo.productIntro;
        }
        if (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;
    });
};

let getintroIntro = (data) => {
    return api.get('', {
        method: 'app.product.intro',
        product_skn: data.productskn,
        app_version: '5.6.0'
    }, {
        cache: true
    }).then(result => {
        result = result || '';
        result = $.load(result);

        result = result('#productDesc');

        return (result.html() || '').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=');
    });
};

module.exports = {
    getintroData,
    getintroIntro
};