index.js 8.37 KB
/**
 * girls model
 * @author: 赵彪<bill.zhao@yoho.cn>
 * @date: 2016/05/17
 */
'use strict';

const _ = require('lodash');

const ServiceAPI = require(`${global.library}/api`).ServiceAPI;
const sign = require(`${global.library}/sign`);
const helpers = require(`${global.library}/helpers`);


const serviceApi = new ServiceAPI();

const headerModel = require('../../../doraemon/models/header');

// 创意生活
// const CODE_LIFESTYLE_CHANNEL_1 = '380c38155fd8beee10913a3f5b462da6';

// const CODE_LIFESTYLE_CHANNEL_2 = '665f7c2fb9d037ee820766953ee34bf7';

const channelMap = {
    boys: {
        code: '79372627eee75d73afe7f9bac91e5ce6',
        gender: '1,3'
    },
    girls: {
        code: '75215008957605c05e8cd375eac4f817',
        gender: '2,3'
    },
    kids: {
        code: 'd71f4b27f2a7229fbb31a4bc490a6f36',
        gender: 'kids'
    },
    lifestyle: {
        code: '8a341ca7eacc069ba80f02dec80eaf34',
        gender: 'lifestyle'
    }
};

const getBannerList = data => {
    let list = [];

    _.forEach(data, (bannerData) => {
        let obj = {};

        obj.href = bannerData.url;
        obj.img = bannerData.src;
        obj.name = bannerData.title;

        list.push(obj);
    });

    return list;
};

const getadbannerData = data => {
    const obj = {
        adbanner: {
            href: '',
            img: '',
            name: ''
        }
    };

    obj.adbanner.href = data.url;
    obj.adbanner.img = data.src;
    obj.adbanner.name = data.title;

    return obj;
};

const getSlideData = srcData => {
    const slideData = {
        slide: {
            list: [],
            pagination: []
        }
    };

    slideData.slide.list = getBannerList(srcData.big_image);
    slideData.slide.pagination = getBannerList(srcData.list);

    return slideData;
};


const getNewReportFloorData = args => {
    let item = args[0].data;
    let secondItem = args[1].data;
    let thirdItem = args[2].data;
    let forthItem = args[3];

    let list = [];
    let obj = {};

    const data = {
        newReport: {
            name: '最新速报',
            list: []
        }
    };


    let adData;
    let floorDatas = [];


    obj.href = item[0].url;
    obj.img = item[0].src;

    list.push(obj);

    _.forEach(secondItem, (floorData) => {
        let o = {};

        o.href = floorData.url;
        o.img = floorData.src;

        list.push(o);
    });

    obj.href = thirdItem[0].url;
    obj.img = thirdItem[0].src;

    list.push(obj);

    data.newReport.list = list;

    floorDatas.push(data);

    if (forthItem.template_name === 'single_image') {
        adData = getadbannerData(forthItem.data[0]);
        floorDatas.push(adData);
    }

    return floorDatas;
};

// 优选品牌
const getPreBrandTopData = args => {
    let item = args[0].data;

    const data = {
        preferenceBrands: {
            name: '优选品牌',
            imgBrand: [],
            brandUrl:helpers.urlFormat('getbrandFloorDataAjax')
        }
    };

    _.forEach(item, (topData) => {
        let o = {};

        o.href = topData.url;
        o.img = topData.src;

        data.preferenceBrands.imgBrand.push(o);
    });

    return data;
};

// 热门品类
const getHotGoodsFloorData = (args) => {
    let item = args[0];
    let nextItem = args[1];

    let list = [];

    let object = {},
        keyword = [],
        category = [],
        brands = [],
        types = [],
        navs = {},
        products = [];

    const data = {
        recommend: {
            tplrecommend: []
        }
    };


    _.forEach(item.data.menuNav.list, (it) => {
        let obj = {};

        obj.name = it.name;
        obj.href = it.url;
        category.push(obj);
    });

    _.forEach(item.data.menuNav.blocks, (it) => {
        let obj = {};

        obj.name = it.title;
        obj.href = it.url;
        obj.img = it.img;
        keyword.push(obj);
    });

    _.forEach(item.data.imgs, (it, idx) => {
        let obj = {};

        obj.name = it.title;
        obj.href = it.url;
        obj.img = it.img;

        if (idx === 0 || idx === 4) {
            brands.push(obj);
        } else {
            types.push(obj);
        }
    });

    _.forEach(nextItem.data, (it) => {
        let obj = {};

        obj.name = it.title;
        obj.href = it.url;
        obj.img = it.src;

        products.push(obj);
    });

    navs.name = item.data.navs.list[0].name;
    navs.href = item.data.navs.list[0].url;
    object.name = item.data.name;
    object.keyword = keyword;
    object.category = category;
    object.brands = brands;
    object.types = types;
    object.navs = navs;
    object.products = products;
    list.push(object);

    data.recommend.tplrecommend = list;

    return data;
};

// 人气单品
const getSingleHotFloorData = args => {
    const len = 10;
    const data = {
        singlehot: {
            name: '人气单品',
            imgHot: []
        }
    };

    let list = [];
    let adData;
    let floorDatas = [];

    for (let i = 0; i < len; i++) {
        let pos = i;
        let val = {};
        let obj = {};

        if (i === 1) {
            val = args[0].data[0]; // 第二个是大图
        } else if (i === len - 1) {
            val = args[0].data[1]; // 最后一个是大图
        } else {
            if (pos > 1) { // 小图
                pos = pos - 1;
            }
            val = args[1].data[pos];
        }
        obj.href = val.url;
        obj.img = val.src;
        list.push(obj);
    }

    data.singlehot.imgHot = list;
    floorDatas.push(data);

    if (args[2].template_name === 'single_image') {
        adData = getadbannerData(args[2].data[0]);
        floorDatas.push(adData);
    }

    return floorDatas;
};



const requestContent = type => {

    let data = sign.apiSign({
            /* eslint-disable */
            client_type: 'web',
            /* eslint-enable */
        content_code: channelMap[type || 'boys'].code,
        gender: channelMap[type || 'boys'].gender,
        page: 1,
        limit: 1000
    });

    return serviceApi.get('/operations/api/v5/resource/home', data);
};

const floorMap = {
    slide: getSlideData,
    hot: getHotGoodsFloorData,
    最新速报: getNewReportFloorData,
    人气单品: getSingleHotFloorData,
    优选品牌: getPreBrandTopData,
    ad: getadbannerData
};

const processFloorData = rawData => {
    let floorList = [];

    _.forEach(rawData, (data, index) => {
        let floorData = null;

        if (data.template_name === 'recommend_content_three') {
            floorData = floorMap.slide.call(null, data.data);
        } else if (data.template_intro === '热门品类') {
            floorData = floorMap.hot.call(null, rawData.slice(index, index + 2));
        } else if (data.data.text) {
            floorData = floorMap[data.data.text] &&
                        floorMap[data.data.text].call(null, rawData.slice(index + 1, index + 5));
        }

        if (!_.isNil(floorData)) {
            _.isArray(floorData) ?
                floorList = floorList.concat(floorData) :
                floorList.push(floorData);
        }
    });

    return floorList;
};

/**
 * 获取频道页数据
 * @param  {string} type 传入频道页类型,值可以是: boys, girls, kids, lifestyle
 * @return {object}
 */
exports.getContent = type => {
    return Promise.all([headerModel.requestHeaderData(), requestContent(type)]).then(res => {

        if (res[0].code === 200 && res[1].code === 200) {
            let headerData = res[0].data,
                contentData = res[1].data.list;

            let data = {};

            data = headerModel.setHeaderData(headerData, type);
            data.module = 'channel';
            data.page = type;
            data.footerTop = true;
            data.channel = processFloorData(contentData);

            return data;
        }
    });
};


// 优选品牌楼层floorData-ajax
exports.getbrandFloorDataAjax = type => {
    return requestContent(type).then(res => {
        if (res.code === 200) {
            let contentData = res.data.list;

            let data = {
                'logoBrand':[],
                'moreBrand':''
            };

            _.forEach(contentData[6].data, (floorData) => {
                let o = {};

                o.href = floorData.url;
                o.img = helpers.image(floorData.src, 185, 86, 2);

                data.logoBrand.push(o);
            });

            data.moreBrand = contentData[7].data[0].url;

            return data;
        }
    });
};