sale.js 12.9 KB
/*
 * @Author: Targaryen
 * @Date:   2016-05-19 10:20:08
 * @Last Modified by:   Targaryen
 * @Last Modified time: 2016-06-07 13:48:17
 */

'use strict';
const library = '../../../library';
const utils = '../../../utils';
const API = require(`${library}/api`).API;
const api = new API();
const saleApi = require('./sale-api');
const saleHandler = require('./sale-handler');
const productProcess = require(`${utils}/product-process`);
const _ = require('lodash');


/**
 * 资源位code码
 */
const contentCode = {
    boys: {
        sale: 'f6400250a73a2a1a8764a0d9be73abf2',
        breakCode: 'a97451680222cb1b215de14bbef64921',
        vip: '0400ac0dffbb2fefe84949200cf2be4a',
        newSale: '92a69093a2ce4fc44be36907f57b01ba'
    },
    girls: {
        sale: 'd74adc70f939a6658b68386ee39199e7',
        breakCode: '91e604f06449783a5bd93c6f4f99a396',
        vip: 'e261419fde3613781a3ec9d367a2e534',
        newSale: '1c4b4939334f7f01fea3fd2e0eb311b5'
    },
    kids: {
        sale: '01937a472a5a3ccf7204f734f2506865',
        breakCode: '4c1dff47c68aace0608eb821fbaf40fd',
        vip: '40db7c9a491548fa8d441815cc7a0617',
        newSale: '0f47d972d463822f77f19a3aa5f9e57e'
    },
    lifestyle: {
        sale: '32944e49d627a031f1d7e14a1f90ccac',
        breakCode: 'c605c9d2f1a426cecad232a7d41e6b97',
        vip: '6ac5f0ee1cf0f2dab89c9cf80263028a',
        newSale: '109cd4b3c9ac17e472f3dea22b92b24d'
    }
};


/**
 * 获取Sale首页商品列表数据 仅 Ajax 调用 Controller 调用
 * @return {[type]} [description]
 */
exports.getSaleGoodsData = (params) => {

    return api.all([
        saleApi.getSaleGoodsList(params),
        saleApi.getUserProfile(params.uid)
    ]).then(result => {
        let finalResult = {};

        if (result[0].code === 200) {
            finalResult.goods = productProcess.processProductList(result[0].data.product_list);
        }

        // 处理 VIP 商品数据
        if (result[1].code === 200 && parseInt(params.saleType, 10) === 2) {
            _.forEach(finalResult.goods, (value, key) => {

                switch (result[1].curLevel) {
                    case '1':
                        finalResult.goods[key].vipPrice = value.vip1Price;
                        finalResult.goods[key].vip1 = true;
                        break;
                    case '2':
                        finalResult.goods[key].vipPrice = value.vip2Price;
                        finalResult.goods[key].vip2 = true;
                        break;
                    case '3':
                        finalResult.goods[key].vipPrice = value.vip3Price;
                        finalResult.goods[key].vip3 = true;
                        break;
                    default:
                        finalResult.goods[key].vip = true;
                        break;
                }

            });
        }

        // 需要判断是否为今日推荐,今日推荐需要添加最后的全部商品图片
        // 今日推荐没有 sort params 所以判断 sort 即可
        if (_.isEmpty(params.sort)) {

            // 2:VIP 会员专享 1:断码区
            switch (params.saleType) {
                case '1':
                    finalResult.goods.push({
                        thumb: 'http://img02.yohoboys.com/staticimg/2016/06/06/15/027f2ebe0f35a0f6dc29ffb11d40d58c68.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/90',
                        url: '/sale/breakingYards'
                    });
                    break;
                case '2':
                    finalResult.goods.push({
                        thumb: 'http://img02.yohoboys.com/staticimg/2016/06/06/15/02c513694df76e78449781997d1ae2a67e.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/90',
                        url: '/sale/vip',
                        vipPicture: true // 为了处理首页最后一张图片
                    });
                    break;
                case '3':
                    finalResult.goods.push({
                        thumb: 'http://img02.yohoboys.com/staticimg/2016/06/06/15/02161115728d763520bd4695d2530b2132.jpg?imageMogr2/thumbnail/{width}x{height}/extent/{width}x{height}/background/d2hpdGU=/position/center/quality/90',
                        url: '/sale/newSale'
                    });
                    break;
                default:
                    break;
            }
        }

        return finalResult;
    });
};

/**
 * 获取 Sale 首页数据 Controller 调用
 * @return {[type]} [description]
 */
exports.getSaleIndexData = (channel) => {

    return api.all([
        saleApi.getSaleActivityList(channel),
        saleApi.getSaleBannerList(contentCode[channel].sale),
        saleApi.getSaleGoodsList({ channel: channel, saleType: '2', limit: '1' }), // 会员专享分类
        saleApi.getSaleGoodsList({ channel: channel, saleType: '1', limit: '1' }), // 断码区分类
        saleApi.getSaleGoodsList({ channel: channel, saleType: '3', limit: '1' }) // 最新降价分类
    ]).then(result => {
        var finalResult = {};

        if (result[0].code === 200) {
            finalResult.brandSale = saleHandler.handleSaleActivityData(result[0].data);
        }

        if (result[1].code === 200) {
            finalResult.topBanner = saleHandler.handleSaleBannerData(result[1].data);
            finalResult.activityEnter = saleHandler.handleSaleBannerSmallData(result[1].data);
        }

        // 分类处理
        finalResult.saleCategory = [];

        // 会员专享分类处理
        if (result[2].code === 200) {

            finalResult.saleCategory.push(
                saleHandler.handleSaleCategoryData(result[2].data.filter.group_sort, '2', channel)
            );
        }

        // 断码区分类处理
        if (result[3].code === 200) {

            finalResult.saleCategory.push(
                saleHandler.handleSaleCategoryData(result[3].data.filter.group_sort, '1', channel)
            );
        }

        // 最新降价分类处理
        if (result[4].code === 200) {

            finalResult.saleCategory.push(
                saleHandler.handleSaleCategoryData(result[4].data.filter.group_sort, '3', channel)
            );
        }
        return finalResult;
    });
};

/**
 * 获取 VIP 会员专享和新降价专区数据 Controller 调用
 * @return {[type]} [description]
 */
exports.getSaleOthersData = (params, channel) => {

    let promiseObject;

    // saleType: 1表示断码区,2表示会员专区,3 折扣专区,4表示奥莱
    if (!_.isEmpty(params) && params.saleType === '2') {
        promiseObject = [
            saleApi.getSaleGoodsList(params),
            saleApi.getSaleBannerList(contentCode[channel].vip),
            saleApi.getSaleGoodsList({ saleType: '2', limit: '1', channel: channel }),
            saleApi.getUserProfile(params.uid)
        ];
    } else {
        promiseObject = [
            saleApi.getSaleGoodsList(params),
            saleApi.getSaleBannerList(contentCode[channel].vip),
            saleApi.getSaleGoodsList({ saleType: '3', limit: '1', channel: channel })
        ];
    }


    return api.all(promiseObject).then(result => {
        let finalResult = {};

        // 获取商品数据
        if (result[0].code === 200) {
            finalResult.goods = productProcess.processProductList(result[0].data.product_list);
        }

        // 获取焦点图数据
        if (result[1].code === 200) {
            finalResult.topBanner = saleHandler.handleSaleBannerData(result[1].data);
        }

        // 获取分类筛选数据
        if (result[2].code === 200) {

            // 获取左侧类目数据
            finalResult.leftContent = saleHandler.handleSaleSortData(result[2].data.filter.group_sort, params);

            // 顶部筛选条件
            finalResult.filters = saleHandler.handleSaleFilterData(result[2].data.filter, params);

            // 处理排序数据
            finalResult.opts = saleHandler.handleSaleOptsData(params, result[2].data.total);
            finalResult.totalCount = result[2].data.total;
            finalResult.pager = saleHandler.handleSalePagerData(result[2].data.total, params);

            // 处理所有商品标题数据
            finalResult.saleTitle = {};

            finalResult.saleTitle.name = '所有商品';
            finalResult.saleTitle.count = finalResult.totalCount;
        }

        if (!_.isEmpty(result[3])) {
            // 处理 VIP 商品数据
            if (result[3].code === 200 && parseInt(params.saleType, 10) === 2) {
                _.forEach(finalResult.goods, (value, key) => {

                    switch (result[3].curLevel) {
                        case '1':
                            finalResult.goods[key].vipPrice = value.vip1_price;
                            finalResult.goods[key].vip1 = true;
                            break;
                        case '2':
                            finalResult.goods[key].vipPrice = value.vip2_price;
                            finalResult.goods[key].vip2 = true;
                            break;
                        case '3':
                            finalResult.goods[key].vipPrice = value.vip3_price;
                            finalResult.goods[key].vip3 = true;
                            break;
                        default:
                            finalResult.goods[key].vipPrice = value.vip_price;
                            finalResult.goods[key].vip = true;
                            break;
                    }

                });
            }
        }

        return finalResult;
    });
};

/**
 * 获取折扣专场数据 Controller 调用
 * @return {[type]} [description]
 */
exports.getSaleDiscountData = (params, channel) => {
    return api.all([
        saleApi.getSaleGoodsList(params),
        saleApi.getSaleBannerList(contentCode[channel].sale),
        saleApi.getSaleActivityList(params)
    ]).then(result => {
        let finalResult = {};

        // 处理商品数据,顶部分类
        if (result[0].code === 200) {
            finalResult.saleList = {};
            finalResult.saleList.goods = productProcess.processProductList(result[0].data.product_list);
            finalResult.leftContent = saleHandler.handleSaleSortData(result[0].data.filter.group_sort, params);
            finalResult.saleList.totalCount = result[0].data.total;
            finalResult.saleList.pager = saleHandler.handleSalePagerData(result[0].data.total, params);

            // 处理所有商品标题数据
            finalResult.saleTitle = {};

            finalResult.saleTitle.name = '所有商品';
            finalResult.saleTitle.count = finalResult.saleList.totalCount;
        }

        // 处理焦点图数据
        if (result[1].code === 200) {
            finalResult.topBanner = saleHandler.handleSaleBannerData(result[1].data);
        }

        // 处理折扣专场标题数据
        if (result[2].code === 200) {
            if (_.size(result[2].data) === 1) {
                finalResult.saleTitle = saleHandler.handleDiscountTitleData(result[2].data[0]);
            }
        }

        return finalResult;
    });
};

/**
 * 获取断码区数据 Controller 调用
 * @param  {[type]} params [description]
 * @return {[type]}        [description]
 */
exports.getSalebreakingYardsData = (params, channel) => {
    return api.all([
        saleApi.getSaleGoodsList(params),
        saleApi.getSalebreakingYardsSortList(params),
        saleApi.getSaleBannerList(contentCode[channel].breakCode),
        saleApi.getSaleGoodsList({ channel: channel, saleType: '1', limit: '1' })
    ]).then(result => {
        let finalResult = {};

        if (result[0].code === 200 && result[1].code === 200) {
            finalResult.goods = productProcess.processProductList(result[0].data.product_list);

            _.forEach(finalResult.goods, (value, key) => {
                finalResult.goods[key].tags.isNew = false; // 屏蔽 new 标签
            });

            // 这边整合了尺码数据处理
            finalResult.filters = saleHandler.handleSaleFilterData(
                Object.assign(result[0].data.filter, { size: result[1].data }),
                params);

            finalResult.opts = saleHandler.handleSaleOptsData(params, result[0].data.total);
            finalResult.totalCount = result[0].data.total;
            finalResult.pager = saleHandler.handleSalePagerData(result[0].data.total, params);

            // 处理所有商品标题数据
            finalResult.saleTitle = {};

            finalResult.saleTitle.name = '所有商品';
            finalResult.saleTitle.count = finalResult.totalCount;
        }

        if (result[2].code === 200) {
            finalResult.topBanner = saleHandler.handleSaleBannerData(result[2].data);
        }

        if (result[3].code === 200) {
            finalResult.leftContent = saleHandler.handleSaleSortData(result[3].data.filter.group_sort, params);
        }

        return finalResult;
    });
};