redshop-process.js 6.99 KB
/*
 * @Author: Targaryen
 * @Date: 2017-03-23 11:02:31
 * @Last Modified by: Targaryen
 * @Last Modified time: 2017-04-12 14:36:15
 */
/* 红人店铺数据处理 */

'use strict';

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

let countCarouselImage = 0; // 轮播图模块统计


/**
 * 链接处理
 * @param {*} linkParent
 */
const _linkhandle = (linkParent) => {
    if (linkParent.linkType) {
        switch (parseInt(linkParent.linkType, 10)) {
            case 0:
                return helpers.urlFormat('', {
                    categoryId: linkParent.resource
                }, 'list');
            case 1:
                return helpers.urlFormat('/product/' + linkParent.resource + '.html');
            case 2:
                return linkParent.resource;
            default:
                break;
        }
    } else {
        return false;
    }
};

/**
 * 图片处理
 * @param {*} moduleData
 */
const _picsHandle = (moduleData) => {
    let pics = [];

    _.forEach(_.get(moduleData, 'data', []), value => {
        let showProductInfo = false;

        if (_.has(value, 'text') && value.text) {
            if (_.has(value, 'showProductInfo')) {
                showProductInfo = value.showProductInfo;
            }
        }

        pics.push({
            src: value.pic,
            link: _linkhandle(value),
            text: value.text,
            showProductInfo: showProductInfo
        });
    });
    return pics;
};

/**
 * 红人店铺楼层数据处理工具
 */
const _tools = {

    /**
     * 模块标题
     * @param {*} moduleData
     */
    title(moduleData) {
        return {
            module_type: 'Title',
            text: _.get(moduleData, 'data[0].text', ''),
            isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0),
            link: _linkhandle(_.get(moduleData, 'data[0]', 0))
        };
    },

    /**
     * 视频模块
     * @param {*} moduleData
     */
    video(moduleData) {
        return {
            module_type: 'Video',
            title: _.get(moduleData, 'data[0].text.title', ''),
            content: _.get(moduleData, 'data[0].text.content', ''),
            pic: _.get(moduleData, 'data[0].pic', ''),
            isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0),
        };
    },

    /**
     * 轮播图
     * @param {*} moduleData
     */
    carouselImage(moduleData) {
        return {
            module_type: 'CarouselImage',
            num: countCarouselImage,
            pics: _picsHandle(moduleData),
            isModuleMargin: Boolean(_.get(moduleData, 'properties.isModuleMargin', 0)),
        };
    },

    /**
     * 一张图片
     * @param {*} moduleData
     */
    singleImage(moduleData) {
        return {
            module_type: 'SingleImage',
            pics: [{
                src: _.get(moduleData, 'data[0].pic', '')
            }],
            text: _.get(moduleData, 'data[0].text', ''),
            width: _.get(moduleData, 'properties.width', ''),
            height: _.get(moduleData, 'data[0].text', ''),
            moduleHeight: _.get(moduleData, 'properties.moduleHeight', ''),
            isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0),
        };
    },

    /**
     * 两张图片
     * @param {*} moduleData
     */
    doubleImage(moduleData) {
        return {
            module_type: 'DoubleImage',
            pics: _picsHandle(moduleData),
            isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0),
        };
    },

    /**
     * 三张图片
     * @param {*} moduleData
     */
    tripleImage(moduleData) {
        let displayType = _.get(moduleData, 'properties.displayType', 0);

        return {
            module_type: 'TripleImage',
            pics: _picsHandle(moduleData),
            isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0),
            displayType: parseInt(displayType, 10),
        };
    },

    /**
     * 四张图片
     * @param {*} moduleData
     */
    fourImage(moduleData) {
        return {
            module_type: 'FourImage',
            pics: _picsHandle(moduleData),
            isModuleMargin: _.get(moduleData, 'properties.isModuleMargin', 0)
        };
    },

    /**
     * 商品列表
     * @param {*} moduleData
     */
    sknList(moduleData) {
        let skns = '';

        _.forEach(_.get(moduleData, 'data', []), value => {
            skns += value + ',';
        });

        return {
            module_type: 'SknList',
            skns: skns,
        };
    }
};

/**
 * 处理红人店铺楼层
 * @param {*} decoratorsData
 */
const floor = (decoratorsData) => {
    let finalData = [];

    countCarouselImage = 0;
    _.forEach(decoratorsData, value => {
        try {
            value.module_data = JSON.parse(value.module_data);
        } catch (error) {
            console.log(error);
        }

        switch (value.module_type) {
            case 'Title':
                finalData.push(_tools.title(value.module_data));
                break;
            case 'Video':
                finalData.push(_tools.video(value.module_data));
                break;
            case 'CarouselImage':
                countCarouselImage++;
                finalData.push(_tools.carouselImage(value.module_data));
                break;
            case 'SingleImage':
                finalData.push(_tools.singleImage(value.module_data));
                break;
            case 'DoubleImage':
                finalData.push(_tools.doubleImage(value.module_data));
                break;
            case 'TripleImage':
                finalData.push(_tools.tripleImage(value.module_data));
                break;
            case 'FourImage':
                finalData.push(_tools.fourImage(value.module_data));
                break;
            case 'SknList':
                finalData.push(_tools.sknList(value.module_data));
                break;
            default:
                break;
        }
    });

    return finalData;
};

/**
 * 店铺介绍处理
 * @param {*} params
 */
const shopIntro = (params) => {
    if (params) {
        params.shop_intro_link = helpers.urlFormat('/product/index/intro', {shop_id: params.shops_id});
    }

    return params;
};

/**
 * 店铺分类处理
 * @param {*} params
 */
const category = (params, shopId) => {
    let categoryData = {
        list: [],
        url: ''
    };

    if (!params) {
        return categoryData;
    }

    if (params.length > 6) {
        categoryData.url = helpers.urlFormat('/product/index/category', {
            shop_id: shopId
        });
    } else {
        _.forEach(params, value => {
            categoryData.list.push({
                url: helpers.urlFormat('', {
                    shop_id: shopId,
                    sort: (value.relation_parameter && value.relation_parameter.sort) || ''
                }, 'search'),
                categoryId: value.category_id,
                name: value.category_name
            });
        });
    }

    return categoryData;
};

module.exports = {
    floor,
    shopIntro,
    category
};