sale-handler.js 7.5 KB
/*
 * @Author: Targaryen
 * @Date:   2016-05-25 18:16:59
 * @Last Modified by:   Targaryen
 * @Last Modified time: 2016-05-26 13:33:33
 */

'use strict';
const _ = require('lodash');

/**
 * 处理用于筛选的 URL
 * @param  {[type]} originParam [description]
 * @param  {[type]} newParam    [description]
 * @return {[type]}             [description]
 */
const handleFilterUrl = (originParam, newParam) => {
    let dest = '';
    let tempOriginParam = {};

    // 创建 originParam、newParam 键的数组
    let originParamArray = _.keys(originParam);
    let newParamArray = _.keys(newParam);

    // 判断参数是否已经存在
    let isExist = !_.isEmpty(_.find(originParamArray, function(o) {
        return o === newParamArray[0];
    }));

    if (isExist) {
        tempOriginParam = Object.assign(tempOriginParam, originParam);

        _.forEach(newParam, function(value, key) {
            tempOriginParam[key] = value;
        });
    } else {
        tempOriginParam = Object.assign(tempOriginParam, originParam, newParam);
    }

    _.forEach(tempOriginParam, function(value, key) {
        dest = dest + key + '=' + value + '&';
    });

    return _.trim(dest, '&');
};

/**
 * 处理商品列表数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
exports.handleSaleGoodsListData = (origin) => {
    var dest = [];

    if (!_.isEmpty(origin)) {

        _.forEach(origin, function(value) {
            let oneGoods = {};

            oneGoods.tags = value.tags;
            oneGoods.thumb = value.default_images;
            oneGoods.url = '/product/pro_' + value.product_id + '_' + value.goods_list[0].goods_id + '/' +
                value.cn_alphabet + '.html';
            oneGoods.goodsList = value.goods_list;
            oneGoods.name = value.product_name;
            oneGoods.brand = {};
            oneGoods.brand.url = value.brand_domain + '.yohobuy.com'; // 待处理
            oneGoods.brand.name = value.brand_name;
            oneGoods.marketPrice = value.market_price;

            dest.push(oneGoods);
        });
    }

    return dest;
};

/**
 * 处理筛选数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
exports.handleSaleFilterData = (origin, params) => {
    var dest = {};

    dest.brand = {};
    dest.price = [];
    dest.channel = [];

    dest.opts = {};

    // 处理 品牌 筛选数据
    dest.brand.default = [];
    if (!_.isEmpty(origin.brand)) {
        _.forEach(origin.brand, function(value) {
            let brand = {};

            brand.checked = false;
            brand.href = handleFilterUrl(params, { brand: value.id });
            brand.name = value.brand_name;

            dest.brand.default.push(brand);
        });
    }

    // 处理 价格 筛选数据
    if (!_.isEmpty(origin.priceRange)) {
        _.forEach(origin.priceRange, function(value, key) {
            let price = {
                checked: false,
                href: handleFilterUrl(params, { price: key }),
                name: value
            };

            dest.price.push(price);
        });
    }

    // 处理频道数据
    for (let i = 0; i < 2; i++) {
        dest.channel[i] = {};

        dest.channel[i].checked = false;
    }

    dest.channel[0].name = '男生';
    dest.channel[0].href = handleFilterUrl(params, { gender: '1,3' });
    dest.channel[1].name = '女生';
    dest.channel[1].href = handleFilterUrl(params, { gender: '2,3' });

    return dest;
};

/**
 * 处理 opts 排序数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
exports.handleSaleOptsData = (params) => {
    var dest = {};

    dest.countPerPage = 60;
    dest.start = 1;
    dest.end = 20;
    dest.sortType = [];

    for (let i = 0; i < 2; i++) {
        let opt = {};

        opt.href = handleFilterUrl(params, { order: 's_p_asc' });
        opt.name = '价格';
        opt.hasSortOrient = {};
        opt.hasSortOrient.active = {};
        opt.hasSortOrient.active.desc = {};

        dest.sortType.push(opt);
    }

    return dest;
};

/**
 * 处理页面左侧分类筛选数据
 * @return {[type]} [description]
 */
exports.handleSaleSortData = (origin, params) => {
    var leftContent = {};

    leftContent.allSort = {};
    leftContent.allSort.list = [];

    _.forEach(origin, function(value) {
        let category = {};

        category.name = value.category_name;
        category.num = value.node_count;
        category.childList = [];

        _.forEach(value.sub, function(subValue) {
            category.childList.push({
                name: subValue.category_name,
                num: subValue.node_count,
                href: handleFilterUrl(params, { sort: subValue.relation_parameter.sort })
            });
        });

        leftContent.allSort.list.push(category);
    });


    return leftContent;
};

/**
 * 处理断码区分类筛选数据 待处理
 * @return {[type]} [description]
 */
exports.handleSalebreakingYardsSortData = (origin, params) => {
    var dest = [];

    _.forEach(origin[0], function(value) {
        let size = {};
        
        size.checked = false;
        size.href = handleFilterUrl(params, { size: value.size_id });
        size.name = value.size_name;

        dest.push(size);
    });

    return dest;
};

/**
 * 处理折扣专区活动数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
exports.handleSaleActivityData = (origin) => {
    var dest = {};

    dest.big = [];
    dest.normal = [];
    _.forEach(origin, function(value, key) {
        let activity = {};

        activity.link = value.web_url;
        activity.img = value.cover_url;
        activity.time = value.left_time;
        activity.brand = value.logo_url;
        activity.discount = value.title;

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

/**
 * 处理首页 banner 数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
exports.handleSaleBannerData = (origin) => {
    var dest = {};

    dest.list = [];
    _.forEach(origin, function(value) {
        if (value.template_name === 'focus') {
            _.forEach(value.data, function(subValue) {
                let banner = {};

                banner.bannerHeight = 450;
                banner.href = subValue.url;
                banner.img = subValue.src;

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

        if (value.template_name === 'small_pic') {
            _.forEach(value.data, function(picList) {
                let smallPic = {};

                smallPic.link = picList.url;
                smallPic.icon = picList.src;
                smallPic.title = picList.title;
                smallPic.desc = picList.alt;

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

    });

    return dest;
};

/**
 * 处理首页 banner 小图
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
exports.handleSaleBannerSmallData = (origin) => {
    var dest = [];

    _.forEach(origin, function(value) {

        if (value.template_name === 'small_pic') {
            _.forEach(value.data, function(picList) {
                let smallPic = {};

                smallPic.link = picList.url;
                smallPic.icon = picList.src;
                smallPic.title = picList.title;
                smallPic.desc = picList.alt;

                dest.push(smallPic);
            });
        }

    });

    return dest;
};