floor-data-handler.js 4.34 KB
/**
 * 获取各个楼层的数据
 * @author: 赵彪<bill.zhao@yoho.cn>
 * @date: 2016/08/05
 */

'use strict';

const _ = require('lodash');

/**
 * 获取楼层title
 * @param {String || Object} 原始title
 * @return {Object}  转换后的title
 */
const _getTitle = t => {
    const reg = /\w+/g;
    let arr = [];
    let r;
    let cn;
    let en;

    if (!t) {
        return {
            en: '',
            cn: ''
        };
    }

    if (_.isObject(t)) {
        t = t.title;
    }

    do {
        r = reg.exec(t);

        if (r) {
            arr.push(r[0]);
        }

    } while (r);

    en = arr.join(' ');
    cn = t.replace(en, '');

    return {
        en,
        cn
    };
};

/**
 * 获取slider楼层数据
 * @param {Object} d 接口返回的楼层数据
 * @return {Object} 处理之后的数据
 */
const getSliderData = d => {
    _.forEach(d, img => {
        img.src = _.split(img.src, '?')[0];
    });

    return {
        slider: d
    };
};

/**
 * 获取BrandsAd楼层数据
 * @param {Object} d 接口返回的楼层数据
 * @return {Object} 处理之后的数据
 */
const getBrandAdFloor = d => {
    const list = d.list;

    _.forEach(list, data => {
        data.btnText = 'shop now';
    });

    return {
        brandsAd: list
    };
};

/**
 * 获取new arrivals楼层数据
 * @param {Object} d 接口返回的楼层数据
 * @return {Object} 处理之后的数据
 */
const getNewArrivals = d => {
    const list = d.list;
    const title = _getTitle(d.title);

    _.forEach(list, (data, index) => {
        if (index === 0 || index === list.length - 1) {
            data.smallImg = true;
        }

        if (index % 2 !== 0) {
            data.even = true;
        }
    });

    return {
        floorZh: title.cn,
        floorEn: title.en,
        newArrivals: list
    };
};

/**
 * 获取classic brands楼层数据
 * @param {Object} d 接口返回的楼层数据
 * @return {Object} 处理之后的数据
 */
const getClassicBrands = d => {
    let brands = [];
    let subArr;
    let i = 0;

    const list = d.list;
    const title = _getTitle(d.title);


    _.forEach(list, (data, index) => {
        if (index === 0 || index === 1 || index === 6 || index === 7) {
            brands.push({
                big: [list[index]]
            });
        } else if ((index > 1 && index < 6 || index > 7 && index < 12) && index % 2 === 0) {
            if (i < 4) {
                subArr = list.slice(index, index + 2);
                brands[i].small = subArr;
                i += 1;
            }
        }
    });

    _.forEach(brands, (data, index) => {
        if (index < 2) {
            data.bottomSpace = true;
        }

        if (index === 1 || index === 3) {
            data.right = true;
        }
    });

    return {
        floorZh: title.cn,
        floorEn: title.en,
        classicBrands: brands
    };
};

/**
 * 获取潮流标志楼层数据
 * @param {Object} d 接口返回的楼层数据
 * @return {Object} 处理之后的数据
 */
const getStyleIcon = d => {
    const list = d.list;
    const title = _getTitle(d.title);

    _.forEach(list, data => {
        data.btnText = '去看看';
    });

    return {
        floorZh: title.cn,
        floorEn: title.en,
        styleIcon: list
    };
};

/**
 * 获取广告楼层数据
 * @param {Object} d 接口返回的楼层数据
 * @return {Object} 处理之后的数据
 */
const getAdBanner = d => {
    return {
        adBanner: d
    };
};

/**
 * 获取咨询楼层数据
 * @param {Object} d 接口返回的楼层数据
 * @return {Object} 处理之后的数据
 */
const getEditorial = d => {
    const list = d.list;
    const title = _getTitle(d.title);

    console.log(d);

    let e = {
        big: {},
        small: []
    };

    _.forEach(list, (data, index) => {
        if (index === 0) {
            e.big = data;
        } else {
            e.small.push(data);
        }
    });

    _.forEach(e.small, (data, index) => {
        if (index === 0 || index === 1) {
            data.bottomSpace = true;
        }

        if (index === 0 || index === 2) {
            data.rightSpace = true;
        }
    });

    return {
        floorZh: title.cn,
        floorEn: title.en,
        editorial: e
    };
};

module.exports = {
    getSliderData,
    getBrandAdFloor,
    getNewArrivals,
    getClassicBrands,
    getStyleIcon,
    getAdBanner,
    getEditorial

};