sale-handler.js 6.61 KB
/*
 * @Author: Targaryen
 * @Date:   2016-05-25 18:16:59
 * @Last Modified by:   Targaryen
 * @Last Modified time: 2016-06-20 21:31:11
 */

'use strict';
const helpers = global.yoho.helpers;
const _ = require('lodash');
const anHour = 3600000;
const aDay = anHour * 24;
const timeFormat = {
    d: '剩{d}天',
    h: '剩{h}小时',
    m: '剩{m}分钟',
    s: '剩{s}秒',
    dh: '剩{d}天{h}小时',
    dhms: '剩{d}天{h}小时{m}分钟{s}秒',
    hms: '剩{h}小时{m}分钟{s}秒',
    ms: '剩{m}分钟{s}秒'
};

/**
 * 折扣专场专题列表过期时间处理
 * @param time 接口返回的时间
 * @returns {*|string}
 */
const processTime = (time) => {
    let data = {};
    let type = '';

    if (time < anHour) {
        data.warnColor = true;
        data.time = '即将结束';
    } else {
        if (time > aDay) {
            type = 'dh';
        } else {
            type = 'h';
        }

        data.time = helpers.dateDiffFormat(timeFormat[type], time, 'ms');
    }

    return data.time;
};

/**
 * 折扣文本切割
 * @param text 要切割的文本
 * @returns {{discount: *, discountText: *}}
 */
const discountSplit = (text) => {
    let endNum = 0,
        i;

    for (i = 0; i < text.length; i++) {
        if (/^([0-9]|\%)*$/.test(text[i])) {
            endNum = i + 1;
        }
    }

    return {
        discount: text.slice(0, endNum),
        discountText: text.slice(endNum, text.length)
    };
};

/**
 * 处理折扣专区活动数据
 * @param origin 原始数据
 * @param channel 频道 boys girls kids lifestyle
 * @returns {{}}
 */
exports.handleSaleActivityData = (origin, channel) => {
    let dest = {
        big: [],
        normal: []
    };

    _.forEach(origin, function(value, key) {
        let activity = {
            link: helpers.urlFormat('/product/sale/discount/detail', {id: value.id, channel: channel}),
            img: value.web_cover_url,
            time: processTime(parseInt(value.left_time, 10) * 1000),
            brand: value.logo_url,
            title: value.title
        };

        activity = Object.assign(activity, discountSplit(value.promotion_name));

        if (key < 3) {
            dest.big.push(activity);
        } else if (key < 27) {
            dest.normal.push(activity);
        }
    });
    return dest;
};

/**
 * 处理折扣专区标题折扣倒计时等数据
 * @param origin 要处理的原始数据
 * @returns {{}}
 */
exports.handleDiscountTitleData = (origin) => {
    let dest = {
        title: origin.title,
        up: true,
        time: parseInt(origin.left_time, 10) * 1000
    };

    Object.assign(dest, discountSplit(origin.promotion_name));

    return dest;
};

/**
 * 处理首页 banner 数据
 * @param origin 要处理的原始数据
 * @returns {{}}
 */
exports.handleSaleBannerData = (origin) => {
    let dest = {
        list: []
    };

    _.forEach(origin, function(value) {
        if (value.template_name === 'focus') {
            _.forEach(value.data, function(subValue) {
                let banner = {
                    bannerHeight: 450,
                    href: subValue.url,
                    img: subValue.src
                };

                dest.list.push(banner);
            });
        }
    });

    return dest;
};

/**
 * 处理首页 banner 小图 只取前三张
 * @param origin 要处理的原始数据
 * @returns {Array}
 */
exports.handleSaleBannerSmallData = (origin) => {
    let dest = [];
    let count = 0;

    _.forEach(origin, function(value) {
        if (value.template_name === 'threePicture') {
            _.forEach(value.data, function(picList) {
                if (count++ < 3) {
                    let smallPic = {
                        link: picList.url,
                        icon: picList.src,
                        title: picList.title,
                        desc: picList.alt
                    };

                    dest.push(smallPic);
                }
            });
        }
    });

    return dest;
};

/**
 * 处理首页楼层分类数据
 * 修改为取大类前 4 个
 * @param origin 要处理的原始数据
 * @param saleType 获取数据分属哪个类目:断码区,会员专享,折扣专场等
 * @param channel 频道
 * @param breakingSizeSort 为断码去额外拼接参数,接口比较垃圾嘛,没办法
 * @returns {{}}
 */
exports.handleSaleCategoryData = (origin, saleType, channel, breakingSizeSort) => {

    let dest = {
        navItem: [
            {
                title: '今日推荐',
                vip: true
            }
        ]
    };

    let urlLocation = {};

    // title urlLocation 处理
    switch (saleType) {
        case '5':
            dest.title = '断码区';
            urlLocation = {
                url: '?saleType=' + saleType + '&order=s_t_desc&channel=' + channel +
                '&breakSize=' + breakingSizeSort.breakSize + '&breakSort=' + breakingSizeSort.breakSort,
                localUrl: '?saleType=' + saleType + '&order=s_t_desc&channel=' + channel,
                limit: 14
            };
            break;
        case '2':
            dest.title = 'VIP会员专享';
            urlLocation = {
                url: '?saleType=' + saleType + '&order=s_t_desc&channel=' + channel,
                limit: 11
            };
            break;
        case '3':
            dest.title = '最新降价';
            urlLocation = {
                url: '?saleType=0&order=s_t_desc&channel=' + channel,
                limit: 14
            };
            break;
        default:
            break;
    }
    dest.urlLocation = dest.navItem[0].urlLocation = urlLocation.url + `&limit=${urlLocation.limit}`;

    let count = 0;

    _.forEach(origin, (value) => {

        if (count++ < 4) {
            let forBreakingUrl = '';

            if (saleType === '1') {
                forBreakingUrl = urlLocation.localUrl;
            } else {
                forBreakingUrl = urlLocation.url;
            }

            let nav = {
                urlLocation: forBreakingUrl + '&sort=' + value.relation_parameter.sort +
                `&limit=${(urlLocation.limit + 1)}`,
                title: value.category_name,
                newDiscount: true
            };

            dest.navItem.push(nav);
        }
    });

    return dest;
};

/**
 * 处理断码区尺码数据,输出整合后的筛选条件
 * @param origin
 */
exports.handleSaleBreakingSizeData = (origin) => {
    let dest = {
        breakSize: '',
        breakSort: ''
    };

    _.forEach(origin, value => {
        dest.breakSort += value.sort_id;

        _.forEach(value.sub, subValue => {
            dest.breakSize += subValue.size_id + ',';
        });
    });

    return dest;
};