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

'use strict';

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

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

const api = new API('http://testservice.yoho.cn:28077');

const getConfig = () => ({
    title: 'home',
    devEnv: true,
    version: '0.0.1'
});


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



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

const getBrandItems = (data) => {
    let brandItems = [];

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

        obj.link = item.sort_url;
        obj.hot = item.is_hot;
        obj.brandName = item.sort_name;

        brandItems.push(obj);
    });


    return brandItems;
};

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

const getSubNav = (data) => {
    let subNav = [];

    _.forEach(data, 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);
    });


    console.log(subNav);

    return subNav;
};


exports.getAllHeaderData = function(resData) {
    let config = getConfig();
    let data = {
        headerData: {
            header: true,
            type: 'boy',
            yohoGroup: getMenuData(),
            navbars: getNavBar(resData),
            subNav: getSubNav(resData)
        }
    };

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

exports.getHeaderData = function() {
    let data = sign.apiSign({

            /* eslint-disable */
            client_type: 'web',

            /* eslint-enable */
    });

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