outlets.js 8.68 KB
/*
 * @Author: Targaryen
 * @Date:   2016-05-30 16:20:03
 * @Last Modified by:   Targaryen
 * @Last Modified time: 2016-06-08 19:56:39
 */

'use strict';
const library = '../../../library';
const utils = '../../../utils';
const API = require(`${library}/api`).API;
const api = new API();
const list = require('./list');
const outletsProcess = require('./outlets-handler');
const outletsApi = require('./outlets-api');
const productProcess = require(`${utils}/product-process`);
const headerModel = require('../../../doraemon/models/header');
const _ = require('lodash');

const publicHandler = require('./public-handler');

// 奥莱频道资源码
const channelCode = {
    index: 'e0565dad65fb8da1f39bc1ac83fc8346',
    boys: '2af513637dc5feeec7f6f8b52989b24b',
    girls: 'd1b56b56c3a12dc1f2f83958680a0911',
    kids: '0fd7ad594940f9ec5a03697317cf6521',
    lifestyle: 'b3d3f55a26f130ac2b516b9fb3823711'
};

/**
 * 获取奥莱首页数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
exports.getOutletsIndexData = (params, channel) => {

    return api.all([
        headerModel.requestHeaderData('outlets'),
        outletsApi.getChannelResouceData({content_code: channelCode.index}),
        outletsApi.getOutletsActivityOrigin({type: '1', channel: channel}), // 获取限时活动列表
        outletsApi.getOutletsActivityOrigin({type: '2', channel: channel}), // 获取即将结束列表
        outletsApi.getOutletsTrendData({limit: '30'}), // 获取潮流速递商品数据
        outletsApi.getOutletsGoodsList(Object.assign(params, {limit: params.limit || '100'})), // 获取底部商品数据
        outletsApi.getOutletsActivityOrigin({type: '3', channel: channel}) // tar add 16171552 即将上线数据
    ]).then(result => {

        let finalResult = {
            headerData: Object.assign(result[0].headerData, {
                header: true,
                headType: 'outlets'
            })
        };

        // 处理资源位数据
        if (result[1].code === 200) {
            finalResult = Object.assign(finalResult,
                outletsProcess.handleOutletsBannersData(result[1].data, params));
        }

        // 处理限时嗨购列表数据
        if (result[2].code === 200) {
            finalResult.limitedBuy = Object.assign(finalResult.limitedBuy,
                outletsProcess.handleOutletsActivityData(result[2].data, '限时嗨购'));
        }

        // 处理即将结束列表数据
        if (result[3].code === 200) {
            finalResult.nearOver = outletsProcess.handleOutletsActivityData(result[3].data, '即将结束');
        }

        // 处理潮品推荐数据
        if (result[4].code === 200) {
            finalResult.limitedBuy.extra.trendGood = outletsProcess.handleOutletstrendGoodData(result[4].data);
        }

        // 处理底部商品数据
        if (result[5].code === 200) {
            finalResult.goodsBoard = {
                sort: Object.assign(publicHandler.handleSaleOptsData(params, result[5].data.total), {newPage: true}),
                list: productProcess.processProductList(result[5].data.product_list)
            };

            // 添加锚点
            if (finalResult.goodsBoard.sort.sortType) {
                _.forEach(finalResult.goodsBoard.sort.sortType, (value, key) => {
                    finalResult.goodsBoard.sort.sortType[key].href += '#otspool';
                });
                if (finalResult.goodsBoard.sort.preHref) {
                    finalResult.goodsBoard.sort.preHref += '#otspool';
                }
                if (finalResult.goodsBoard.sort.nextHref) {
                    finalResult.goodsBoard.sort.nextHref += '#otspool';
                }
            }

            finalResult.goodsBoard.pager = publicHandler.handleSalePagerData(result[5].data.total, params);
        }

        // 处理即将上架品牌数据
        if (result[6].code === 200 && !_.isEmpty(result[6].data)) {
            finalResult.limitedBuy.extra.comeSoon = outletsProcess.handleComeSoonData(result[6].data);
        }

        return finalResult;
    });
};

/**
 * 获取奥莱频道页面数据
 * @param  {[object]} gender
 * @return {[type]}
 */
exports.getOutletsChannelData = (params, channel) => {
    // 频道资源位不存在
    if (!channelCode[channel]) {
        throw new Error('outlets channel resource not found');
    }

    let channelData = {};

    let apiArr = [
        // 获取头部数据
        headerModel.requestHeaderData('outlets'),

        // 获取频道资源位
        outletsApi.getChannelResouceData({content_code: channelCode[channel]}),

        // 获取奥莱活动
        outletsApi.getOutletsActivityOrigin({
            platform: 1,
            size: 0,
            channel: channel,
            type: 1
        }),

        // 获取奥莱商品列表
        outletsApi.getOutletsGoodsList(Object.assign(params, {channel: channel}))
    ];

    return api.all(apiArr).then(result => {
        // 返回页面的数据
        let finalResult = {
            headerData: Object.assign(result[0].headerData, {
                header: true,
                headType: 'outlets'
            })
        };

        Object.assign(channelData, finalResult);

        // 资源楼层
        if (result[1].code === 200) {
            Object.assign(channelData, outletsProcess.processFloor(result[1].data.list, params));
        }

        // 限时嗨购
        if (result[2].code === 200) {
            channelData.nearOver = outletsProcess.handleOutletsActivityData(result[2].data, '限时嗨购');
        }

        // 商品列表
        if (result[3].code === 200) {
            channelData.goodsBoard = {};
            channelData.goodsBoard.sort = publicHandler.handleSaleOptsData(params, result[3].data.total);
            channelData.goodsBoard.sort.newPage = true;
            channelData.goodsBoard.list = productProcess.processProductList(result[3].data.product_list);
            channelData.goodsBoard.pager = publicHandler.handleSalePagerData(result[3].data.total, params);

            // 添加锚点
            if (channelData.goodsBoard.sort.sortType) {
                _.forEach(channelData.goodsBoard.sort.sortType, (value, key) => {
                    channelData.goodsBoard.sort.sortType[key].href += '#otspool';
                });
                if (channelData.goodsBoard.sort.preHref) {
                    channelData.goodsBoard.sort.preHref += '#otspool';
                }
                if (channelData.goodsBoard.sort.nextHref) {
                    channelData.goodsBoard.sort.nextHref += '#otspool';
                }
            }
        }

        return channelData;
    });
};

/**
 * 获取奥莱活动页面数据
 * @param  {[object]}
 * @return {[type]}
 */
exports.getOutletsSpecialData = (params, channel) => {
    return api.all([
        headerModel.requestHeaderData('outlets'),
        outletsApi.getOutletsActivityOrigin({ // 获取活动信息
            id: params.id
        })
    ]).then(result => {
        let specialData = {};

        Object.assign(specialData, result[0]);
        specialData.headerData.headType = 'outlets';

        if (result[1].code !== 200) {
            // 活动信息获取异常
            throw new Error('fail_to_get_special_info_with_ID:' + params.id);
        }

        Object.assign(specialData,
            outletsProcess.handleOutletsSpecilData(result[1].data[0]));

        return api.all([
            list.getListData(Object.assign({
                productPool: result[1].data[0].productPoolId,
                saleType: 4
            }, params), channel)
        ]).then(data => {
            specialData.specialHead.count = data[0].totalCount;
            Object.assign(specialData, data[0]);
            return specialData;
        });
    });
};

/**
 * 获取奥莱分类页面数据
 * @param  {[type]} params  [description]
 * @param  {[type]} channel [description]
 * @return {[type]}         [description]
 */
exports.getOutletsCategoryData = (params, channel) => {
    return api.all([
        headerModel.requestHeaderData('outlets')
    ]).then(result => {
        let finalResult = {
            headerData: Object.assign(result[0].headerData, {
                header: true,
                headType: 'outlets'
            })
        };

        return api.all([
            list.getListData(params, channel)
        ]).then(listResult => {
            finalResult.saleList = listResult[0];

            if (!_.isEmpty(params.bannerImage)) {
                finalResult.saleList.topBanner = {};
                finalResult.saleList.topBanner.list = [];
                finalResult.saleList.topBanner.list[0] = {
                    img: params.bannerImage
                };
            }
            return finalResult;
        });
    });
};