channel.js 6.61 KB
/**
 * 频道页面 model
 * @author: Bi Kai<kai.bi@yoho.cn>
 * @date: 2016/05/09
 */
'use strict';
const utils = '../../../utils';
const contentCodeConfig = require('../../../config/content-code');
const _ = require('lodash');
const api = global.yoho.ServiceAPI;
const camelCase = global.yoho.camelCase;
const logger = global.yoho.logger;
const resourcesProcess = require(`${utils}/resources-process`);


/**
 * 性别数据
 * @type {Object}
 */
const genderData = {
    boys: '1,3',
    girls: '2,3'
};

/**
 * 楼层资源的位置码
 * @type {Object}
 */
const contentCode = contentCodeConfig.channel;

/**
 * 频道底部 Banner 位置码
 * @type {Object}
 */
const bottomBannerCode = contentCodeConfig.bottom;

/**
 * 频道选择页 默认数据
 * @type {Object}
 */
const channelList = [
    {
        href: '/boys',
        title: '男生',
        entitle: 'BOYS'
    }, {
        href: '/girls',
        title: '女生',
        entitle: 'GIRLS'
    }, {
        href: '/kids',
        title: '潮童',
        entitle: 'KIDS'
    }, {
        href: '/lifestyle',
        title: '创意生活',
        entitle: 'LIFESTYLE'
    }
];

/**
 * 获取二级菜单顶部颜色
 * @param  {[string]} choosed
 * @return {[string]}
 */
const _getSidebarColor = (choosed) => {
    let color = false;

    if (choosed === 'girls') {
        color = '#FF88AE';
    } else if (choosed === 'kids') {
        color = '#7ad9f9';
    } else if (choosed === 'lifestyle') {
        color = '#4f4138';
    }

    return color;
};

/**
 * 处理侧边栏数据
 * @param  {[array]} list
 * @return {[array]}
 */
const _processSideBar = (list, choosed) => {
    const formatData = [];
    let offset = 0; // 分割数组用到的游标

    list = list || [];
    list = camelCase(list);

    _.forEach(list, (item, i) => {
        if (item.sub) {
            item.sub.unshift({
                sortName: item.sortName,
                sortNameEn: item.sortNameEn,
                back: true,
                isSelect: false,
                bgColor: _getSidebarColor(choosed)
            });
        }

        // 如果有分隔符,分割数组
        if (item.separativeSign === 'Y') {
            formatData.push(list.slice(offset, i));
            offset = i;
        }
    });

    // 数组被分割剩余的部分
    formatData.push(list.slice(offset));
    return formatData;
};

/**
 * 获取频道页面资源位
 * @param  {[object]} gender
 * @return {[type]}
 */
const _getChannelResource = (params) => {
    params.gender = params.gender || 'boys';

    params = Object.assign(params, {
        gender: genderData[params.gender] || '1,2,3',
        content_code: contentCode[params.gender], // eslint-disable-line
        page: 1,
        limit: 30
    });
    if (!params.uid) {
        params.new_device = true; // eslint-disable-line
    }

    return api.get('operations/api/v5/resource/home', params, {
        cache: true
    }).then(result => {
        if (result && result.code === 200) {
            return resourcesProcess(result.data.list);
        } else {
            logger.error('index resouce is not 200');
            return result;
        }
    });
};

/**
 * 获取左侧边栏数据
 * @param  {[string]} choosed
 * @return {[object]}
 */
const _getLeftNav = (choosed) => {
    choosed = choosed || 'all';

    return api.get('operations/api/v6/category/getCategory', {}, {
        cache: true
    }).then(result => {
        if (result && result.code === 200) {
            return _processSideBar(result.data, choosed);
        } else {
            logger.error('sidebar code is not 200');
            return result;
        }
    });
};

/**
 * 获取频道选择页数据
 * @return {[type]}
 */
const _getChannelList = () => {
    return api.get('operations/api/v5/entrance/getEntrance', {}, {
        cache: true
    }).then((result) => {
        if (result && result.code === 200) {
            const list = {};

            list.channelList = [];
            list.yohood = {};
            result.data.list = camelCase(result.data.list || []);

            _.forEach(result.data.list, function(item) {
                let id = _.toNumber(item.id);
                const channel = channelList[id - 1];

                if (channel) {
                    list.channelList.push(channel);
                }

                if (id === 5) {
                    list.yohood.showYohood = true;
                    list.yohood.yohoodHref = 'http://www.yohood.cn';
                }

                if (id === 6) {
                    list.channelList.push({
                        href: item.url,
                        title: item.title || '双 11',
                        entitle: item.title
                    });
                }
            });

            return Object.keys(list).length ? list : channelList;
        } else {
            logger.error('channel select code is not  200');
            return channelList;
        }
    });
};

/**
 * 获取频道选择页 背景
 * @return {[type]}
 */
const _getChannelBg = () => {
    return api.get('operations/api/v5/resource/get', {
        content_code: contentCode.index
    }, {
        cache: true
    }).then(result => {
        if (result && result.code === 200) {
            return result.data.length && result.data[0] && result.data[0].data && result.data[0].data.list[0];
        } else {
            logger.error('channel select background code is not 200');
            return {
                src: ''
            };
        }
    });
};

/**
 * 获取频道选择页面数据
 * @param  {[string]} gender
 * @return {[type]}
 */
let getChannelSwitchData = () => {
    return Promise.all([_getChannelList(), _getChannelBg()]);
};

/**
 * 获取频道页面数据
 * @param  {[object]} params
 * @return {[object]}
 */
let getChannelData = (params) => {
    var channelData = {};
    let navGender = _.cloneDeep(params.gender);

    return Promise.all([_getChannelResource(params), _getLeftNav(navGender)]).then((data) => {
        channelData.content = data[0]; // 资源位数据
        channelData.sideNav = data[1]; // 侧边栏数据

        return channelData;
    });
};

/**
 * 获取频道页底部 bannel 数据
 * @param  {[string]} gender
 * @return {[type]}
 */
let getBottomBannerData = (gender) => {
    gender = gender || 'boys';

    if (gender === 'boys' || gender === 'girls') {
        return api.get('operations/api/v5/resource/get', {
            content_code: bottomBannerCode[gender] // eslint-disable-line
        }, true);
    }
    return Promise.resolve({
        code: 400,
        data: '',
        message: '参数错误'
    });
};

module.exports = {
    getChannelData,
    getChannelSwitchData,
    getBottomBannerData
};