header.js 4.48 KB
/**
 * header model
 * @author: yyq<yanqing.yang@yoho.cn>
 * @date: 2016/06/30
 */
'use strict';

const _ = require('lodash');

const serviceApi = global.yoho.ServiceAPI;

/**
 * 获取菜单
 * @param undefined
 * @return {array} 菜单数组
 */
const getMenuData = () => (
    {
        yohoGroup: [{
            link: '/',
            cn: '有货BLACK',
            en: 'YOHO!BLK'
        }, {
            link: 'http://www.yohoboys.com',
            cn: '男生潮流',
            en: 'YOHO!BOYS'
        }, {
            link: 'http://www.yohogirls.com',
            cn: '女生潮流',
            en: 'YOHO!GIRLS'
        }, {
            link: 'http://app.yohoshow.com',
            cn: '物趣分享',
            en: 'YOHO!SHOW'
        }, {
            link: 'http://www.yohood.cn',
            cn: '潮流嘉年华',
            en: 'YO\'HOOD'
        }, {
            link: '//www.yohobuy.com',
            cn: '有货',
            en: 'YOHO!BUY'
        }]
    }
);

/**
 * 获取导航
 * @param {Object} data 要处理的数据
 * @param {String} type 频道类型
 * @return {array} 导航数组
 */
const getNavBar = (data, type) => {
    let navBars = [];

    _.forEach(data, item => {
        let obj = {},
            lowEn = _.camelCase(item.sort_name_en).toLowerCase();

        obj.link = item.sort_url;
        obj.cn = item.sort_name;
        obj.en = item.sort_name_en;
        obj.isNewPage = item.is_new_page === 'Y' ? true : false;

        if (type === lowEn) {
            obj.cur = true;
        }

        navBars.push(obj);
    });

    return navBars;
};


/**
 * 获取品牌名字
 * @param {Object} data 要处理数据
 * @return {array} 品牌数组
 */
const getBrandItems = (data) => {
    let brandItems = [];

    _.forEach(data, item => {
        brandItems.push({
            link: item.sort_url,
            hot: item.is_hot === 'Y' ? true : false,
            name: item.sort_name
        });
    });

    return brandItems;
};

/**
 * 获取三级菜单
 * @param {Object} data 要处理数据
 * @return {array} 三级菜单数组
 */
const getThirdNav = (data) => {
    let thirdNav = [];

    _.forEach(data, item => {
        let obj = {
            link: item.sort_url,
            name: item.sort_name
        };

        thirdNav.push(obj);

        if (item.sub) {
            thirdNav = _.concat(thirdNav, getBrandItems(item.sub));
            obj.category = true;

            // obj.brandItems = getBrandItems(item.sub);
        }

    });

    return _.chunk(thirdNav, 10);
};

/**
 * 获取子菜单
 * @param {Object} data 要处理数据
 * @param {String} type 频道类型
 * @return {array} 子菜单数组
 */
const getSubNav = (data, type) => {
    let subNav = [];

    _.forEach(data, it => {
        if (type === _.camelCase(it.sort_name_en).toLowerCase()) {
            _.forEach(it.sub, item => {
                let obj = {};

                obj.link = item.sort_url;
                obj.cn = item.sort_name;
                obj.en = item.sort_name_en;
                obj.isHot = item.is_hot === 'Y' ? true : false;
                obj.isNew = item.is_new === 'Y' ? true : false;

                if (item.sub) {
                    obj.thirdNav = getThirdNav(item.sub);
                    obj.imgCode = item.content_code;
                }

                subNav.push(obj);
            });
        }
    });

    return subNav;
};




/**
 * 处理接口返回的数据
 * @param {object} 接口返回的对象
 * @param {String} 指定页面类型
 * @return {object} 头部数据
 */
const setHeaderData = (resData, type) => (
    {
        navMenu: {
            type: type,
            navbars: resData ? getNavBar(resData, type) : [],
            subNav: resData ? getSubNav(resData, type) : []
        }
    }
);

/**
 * 请求头部菜单数据
 * @param {String} 频道类型
 * @return {promise}
 */
const requestNavBar = (type) => {
    return serviceApi.get('operations/api/v6/category/getCategory', {
        client_type: 'web',
        parent_id: 1155 // 接口写死blk菜单pid为1155
    }, {
        cache: true,
        code: 200
    }).then(res => {
        return setHeaderData(res.data, type);
    });
};



/**
 * 请求头部数据
 * @param {String} 频道类型
 * @return {promise}
 */
exports.requestHeaderData = (type) => {
    let arr = [
        getMenuData()
    ];

    if (type) {
        arr.push(requestNavBar(type));
    }

    return Promise.all(arr).then(result => {
        return Object.assign({
            pageHeader: result[0]
        }, result[1]);
    });
};