menu-crumb-handler.js 2.25 KB
/**
 * 个人中心布局(顶部面包屑和左侧导航)
 * @author: xuqi<qi.xu@yoho.cn>
 * @date: 2016/7/5
 */

'use strict';

const _ = require('lodash');

const blk = global.yoho;

const url = blk.helpers.urlFormat;

const meCode = require('../../../config/channel-type').meCode;
const api = global.yoho.ServiceAPI;

// 获取个人中心资源位
const getMeThumb = () => {
    const data = {
        client_type: 'web',
        content_code: meCode
    };

    return api.get('operations/api/v5/resource/home', data, {
        cache: true,
        code: 200
    }).then(result => {
        const obj = _.get(result, 'data.list[0].data[0]', {});

        if (result.data) {
            return {
                banner: result.data.list ? result.data.list[0].data[0].src : '',
                url: result.data.list ? result.data.list[0].data[0].url : ''
            };
        }
        return {
            banner: obj.src || '',
            url: obj.url || ''
        };
    });
};

// 左侧菜单
const navigation = [
    {
        link: url('/me/order'),
        name: '我的订单'
    },
    {
        link: url('/me/return'),
        name: '我的退/换货'
    },
    {
        link: url('/me/collection'),
        name: '我的收藏'
    },
    {
        link: url('/me/currency'),
        name: '我的有货币'
    },
    {
        link: url('/me/setting'),
        name: '个人设置'
    },
    {
        link: url('/me/address'),
        name: '收货地址'
    }
];

// 头部导航面包屑
const getMeCrumb = name => {
    return [
        {
            link: blk.config.siteUrl,
            name: 'YOHO!BLK首页'
        },
        {
            link: url('/me'),
            name: '个人中心'
        },
        {
            name: name ? name : '我的订单'
        }
    ];
};

// 侧栏菜单
const getSideMenu = focus => {
    let copiedNav = _.cloneDeep(navigation);

    if (focus && _.some(copiedNav, {name: focus})) {
        Object.assign(_.find(copiedNav, {name: focus}), {
            focus: true
        });
    } else {

        // 若没有传focus || focus不在navigation中,则默认focus第1个菜单
        copiedNav[0].focus = true;
    }

    return copiedNav;
};

module.exports = {
    getMeCrumb,
    getMeThumb,
    getSideMenu
};