header.js 4.01 KB
/**
 * header model
 * @author: 赵彪<bill.zhao@yoho.cn>
 * @date: 2016/05/03
 */

'use strict';

const _ = require('lodash');
const lRoot = '../../library/';

const ServiceAPI = require(`${lRoot}/api`).ServiceAPI;
const sign = require(`${lRoot}/sign`);

const serviceApi = new ServiceAPI();

/**
 * 获取公共配置
 * @param undefined
 * @return {Object} 配置对象
 */
const commonConfig = () => ({
    title: 'home',
    devEnv: true,
    version: '0.0.1'
});

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

/**
 * 获取导航
 * @param undefined
 * @return {array} 导航数组
 */
const getNavBar = (data) => {
    let navBars = [];

    _.forEach(data, function(item) {
        let obj = {};

        obj.link = item.sort_url;
        obj.cn = item.sort_name;
        obj.en = item.sort_name_en;

        navBars.push(obj);
    });

    return navBars;
};


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

    _.forEach(data, function(item) {
        let obj = {};

        obj.link = item.sort_url;
        obj.hot = item.is_hot === 'Y' ? true : false;
        obj.brandName = item.sort_name;

        brandItems.push(obj);
    });


    return brandItems;
};

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

    _.forEach(data, function(item) {
        let obj = {};

        obj.link = item.sort_url;
        obj.title = item.sort_name;
        obj.imgCode = item.content_code;

        if (item.sub) {
            obj.brandItems = getBrandItems(item.sub);
        }

        thirdNav.push(obj);
    });


    return thirdNav;
};

/**
 * 获取子菜单
 * @param undefined
 * @return {array} 子菜单数组
 */
const getSubNav = (data, type) => {
    let subNav = [];

    let index;

    switch (type) {
        case 'boy':
            index = 0;
            break;
        case 'girl':
            index = 1;
            break;
        case 'kids':
            index = 2;
            break;
        case 'lifestyle':
            index = 3;
            break;
        default:
            index = 0;
    }

    _.forEach(data[index].sub, function(item) {
        let obj = {};

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

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

        subNav.push(obj);
    });


    return subNav;
};


/**
 * 处理接口返回的数据
 * @param {object} 接口返回的对象
 * @return {object} 头部数据
 */
exports.getAllHeaderData = function(resData, type) {
    let config = commonConfig();
    let data = {
        headerData: {
            header: true,
            type: type,
            yohoGroup: getMenuData(),
            navbars: getNavBar(resData),
            subNav: getSubNav(resData, type)
        }
    };

    return _.merge(config, data);
};

/**
 * 请求头部数据
 * @param undefined
 * @return {promise}
 */
exports.getHeaderData = function() {
    let data = sign.apiSign({

            /* eslint-disable */
            client_type: 'web',
            /* eslint-enable */
    });

    return serviceApi.get('/operations/api/v6/category/getCategory', data, true);
};