resources-process.js 3.64 KB
'use strict';

const _ = require('lodash');
const processTime = require('./time-process');

/**
 * 处理楼层数据
 * @param  {[array]} list
 * @return {[array]}
 */
module.exports = (list) => {
    const formatData = [];

    list = list || [];

    _.forEach(list, (floor) => {
        floor[_.camelCase(floor.template_name)] = true;

        // 特殊资源位处理
        // PLUS
        if (floor.singleNameImage && floor.data) {
            floor.data.title = {
                title: floor.data.title
            };
        }

        // 潮流时装/经典裤裙/时尚靴履/潮人配饰/潮流上装
        if (floor.recommendContentOne && floor.data) {
            if (floor.data.big_image && floor.data.big_image.length > 1) {
                floor.data.big_image = {
                    bigList: floor.data.big_image
                };
            }
        }

        // OUTLETS
        if (floor.titleImage && floor.data) {
            if (floor.data.image) {
                floor.data.big_image = {
                    bigList: [floor.data.image]
                };
            }

            floor.data.title = {
                more_name: floor.data.more_name,
                more_url: floor.data.more_url,
                title: floor.data.title
            };
        }

        // 折扣专场
        if (floor.discountActivity && floor.data && floor.data.list.length) {
            _.map(floor.data.list, (item) => {
                Object.assign(item, processTime(item.left_time));
            });
        }

        // 断码区 , 此资源位数据结构比较坑
        if (floor.offCodeArea && floor.data && floor.data.list.length) {
            floor.data = {
                title: floor.data.title,
                data: [floor.data.list[0]],
                left: [floor.data.list[1]],
                right: floor.data.list.slice(2)
            };
        }

        // 会员专享
        if (floor.vipFloor && floor.data) {
            floor.data.data = floor.data.image;
        }

        // 单张轮播图
        floor.singleImage &&
        floor.data.length === 1 &&
        (floor.singleOne = true);

        // vip专享 人气单品背景处理
        if ((floor.vipUse || floor.popularSingleProduct || floor.newUserFloor) && floor.data && floor.data.background) {
            let a = floor.data.background.src;
            a = a.substr(0, a.indexOf('?'));
            floor.data.background.src = a;
        }

        // 处理价格两位小数
        if ((floor.vipUse || floor.popularSingleProduct || floor.newUserFloor) && floor.data) {
            for (let item of floor.data.list) {
                item.vip1_price && (item.vip1_price = item.vip1_price.toFixed(2));
                item.vip2_price && (item.vip2_price = item.vip2_price.toFixed(2));
                item.vip3_price && (item.vip3_price = item.vip3_price.toFixed(2));
                item.sales_price && (item.sales_price = item.sales_price.toFixed(2));
            }
        }

        // 图标楼层背景图
        if (floor.appIconList && floor.back_image) {
            let a = floor.back_image;
            a = a.substr(0, a.indexOf('?'));
            floor.back_image = a;
        }

        // sale banner名字
        if (floor.sale1T1L4R && floor.data && floor.data.big_image) {
            floor.data.banner_image = floor.data.big_image;
        }

        // 分隔图楼层
        if (floor.divideImage && floor.divideImage.data && floor.divideImage.data.src) {
            let a = floor.divideImage.data.src;
            a = a.substr(0, a.indexOf('?'));
            floor.divideImage.data.src = a; 
        } 
        formatData.push(floor);
    });


    return formatData;
};