outlets.js 4.63 KB
/*
 * @Author: Targaryen
 * @Date:   2016-05-30 16:20:03
 * @Last Modified by:   Targaryen
 * @Last Modified time: 2016-06-02 10:26:33
 */

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

/**
 * 处理奥莱活动数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
const handleOutletsActivityData = (origin) => {
    let dest = {};

    dest.name = '限时嗨购'; // 需要根据 origin 传值修改
    dest.topic = [];

    // 处理奥莱活动列表数据
    _.forEach(origin, value => {
        let activity = {};

        activity.href = helpers.urlFormat('/outlets/special/detail', { id: value.id });
        activity.img = value.coverUrl;
        activity.logo = value.logoUrl;
        activity.discount = value.promotionName;
        activity.title = value.title;
        activity.limit = value.productPoolId; // 不清楚什么数据 待处理

        dest.topic.push(activity);
    });

    let nowTime = Math.round(new Date().getTime() / 1000);

    dest.specialHead = {
        logo: origin.logoUrl,
        special: origin.title,
        discount: '8',
        title: '全部商品',
        limit: (origin.endTime - nowTime) * 1000
    };

    return dest;
};

/**
 * 处理奥莱资源位数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
const handleOutletsBannersData = (origin) => {
    var dest = {};

    dest.mainBanner = {};

    _.forEach(origin.list, value => {
        if (value.template_name === 'focus') {
            dest.mainBanner.list = value.data;
        }

        if (value.template_name === 'threePicture') {
            dest.column = value.data;
        }
    });

    return dest;
};

/**
 * 获取奥莱首页数据
 * @param  {[type]} origin [description]
 * @return {[type]}        [description]
 */
exports.getOutletsIndexData = (params) => {
    return api.all([
        outletsApi.getChannelResouceData({ content_code: 'e0565dad65fb8da1f39bc1ac83fc8346' }),
        outletsApi.getOutletsActivityOrigin(params), // 获取限时活动列表
        outletsApi.getOutletsActivityOrigin(params) // 获取即将结束列表
    ]).then(result => {
        let finalResult = {};

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

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

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

        return finalResult;
    });
};


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

/**
 * 获取奥莱频道页面数据
 * @param  {[object]} gender
 * @return {[type]}
 */
exports.getOutletsChannelData = (params) => {
    let channelData = {};

    return api.all([
        outletsApi.getChannelResouceData({ content_code: channelCode[params.channel] }),
        outletsApi.getOutletsActivityOrigin({ platform: 2, size: 0, type: 1 }),
        saleApi.getSaleGoodsList(params)
    ]).then(result => {
        // 资源楼层
        if (result[0].code === 200) {
            Object.assign(channelData, outletsProcess.processFloor(result[0].data.list));
        }

        // 限时嗨购
        if (result[1].code === 200) {
            Object.assign(channelData, { flashSale: outletsProcess.formatActivityData(result[1].data) });
        }

        // 商品列表
        if (result[2].code === 200) {
            channelData.goodsBoard.list = productProcess.processProductList(result[2].data.product_list);
        }
        return channelData;
    });
};

/**
 * 获取奥莱活动页面数据
 * @param  {[object]}
 * @return {[type]}
 */
exports.getOutletsSpecialData = (params) => {
    return api.all([
        outletsApi.getOutletsActivityOrigin({
            id: params.id
        })
    ]).then(result => {
        if (result[0].code === 200) {
            return handleOutletsActivityData(result[0].data[0]);
        }
    });
};