index.js 2.97 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 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 getNewReportFloorData = data => {
    let list = [];

    _.forEach(data, (item, index) => {
        if (item.data.text === '最新速报') {
            let obj = {};

            obj.href = data[index + 1].data[0].url;
            obj.img = data[index + 1].data[0].src;

            list.push(obj);

            _.forEach(data[index + 2].data, (floorData) => {
                let o = {};

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

                list.push(o);
            });

            obj.href = data[index + 3].data[0].url;
            obj.img = data[index + 3].data[0].src;

            list.push(obj);
        }
    });

    return list;
};

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);
};

// boys, girls, kids, lifestyle
exports.getContent = type => {
    return Promise.all([headerModel.requestHeaderData(), requestContent(type)]).then(res => {
        let headerData = res[0].data,
            contentData = res[1].data;

        let data = {};


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

        data.slide = {
            list: getBannerList(contentData.list[0].data.big_image),
            pagination: getBannerList(contentData.list[0].data.list)
        };

        data.newReport = {
            name: '最新速报',
            list: getNewReportFloorData(contentData.list)
        };


        return data;
    });
};