general-tabs-service.js 5.66 KB
'use strict';

const _ = require('lodash');

const userApi = require('./user-api');
const headerModel = require('../../../doraemon/models/header');
const msgModel = require('./message');
const helpers = global.yoho.helpers;

const defaultAvatar = '//img10.static.yhbimg.com/headimg/' +
    '2013/11/28/09/01cae078abe5fe320c88cdf4c220212688.gif?imageView/2/w/100/h/100';

const _homeNav = (switcher) => {
    return [
        {
            title: '交易管理',
            subNav: [
                {name: '我的订单', href: '/home/orders', catchs: ['/home/orders', '/home/index', '/home/orders/detail']},
                {name: '我的收藏', href: '/home/favorite', catchs: ['/home/favorite/reduction']},
                {name: '我的有货币', href: '/home/currency'},
                {name: '我的红包', href: '/home/redenvelopes'},
                {name: '我的优惠券', href: '/home/coupons'},
                {name: '我的VIP', href: '/home/vip'}
            ]
        },
        {
            title: '服务中心',
            subNav: [
                {name: '我的退/换货', href: '/home/returns'},
                {name: '我的咨询', href: '/home/consult'},
                {name: '我的评论', href: '/home/comment'},

                // {name: '我的投诉', href: '/home/complaints'},
                {name: '我的信息', href: '/home/message', count: 0},
                {
                    name: '在线客服',
                    href: switcher ?
                        '/service/client' : 'http://chat8.live800.com/live800/chatClient/chatbox.jsp?companyID=620092&configID=149091&jid=8732423409',
                    isBlank: true
                }
            ]
        },
        {
            title: '个人信息管理',
            subNav: [
                {name: '编辑个人资料', href: '/home/user'},
                {name: '账号安全', href: '/home/account', catchs: [
                    '/home/account/userpwd',
                    '/home/account/email',
                    '/home/account/mobile',
                    '/home/account/checkverifycode',
                    '/home/account/checkpassword',
                    '/home/account/verifypassword',
                    '/home/account/modifypwd',
                    '/home/account/sendemail',
                    '/home/account/checkemail',
                    '/home/account/modifyemail',
                    '/home/account/sendemailsuccess',
                    '/home/account/mailresult',
                    '/home/account/checkmobile',
                    '/home/account/checkmobilemsg',
                    '/home/account/sendmobilemsg',
                    '/home/account/modifymobile'
                ]},
                {name: '地址管理', href: '/home/address'},
                {name: '兑换礼品卡', href: '/home/gift'}
            ]
        }
    ];
};

const _getActiveNav = (url, switcher, count) =>{
    let mHomeNav = _.cloneDeep(_homeNav(switcher));
    let activeNav = null;

    mHomeNav = mHomeNav.map((item) => {
        item.subNav = item.subNav.map((nav) => {

            let curMatchPath = url;

            if (!nav.matchQuery && curMatchPath.indexOf('?') >= 0) { // 严格的路径匹配,包含后面的参数
                curMatchPath = curMatchPath.substr(0, curMatchPath.indexOf('?'));
            }

            if (curMatchPath === nav.href) {
                nav.active = true;
            }

            if (nav.name === '我的信息') {
                nav.count = +count;
            }

            if (nav.catchs) {
                let index = nav.catchs.indexOf(curMatchPath);

                if (index > -1) {
                    nav.active = true;
                    nav.curHref = nav.catchs[index];
                }
            }

            if (nav.active) {
                activeNav = nav;
            }

            nav.href = nav.href.indexOf('http://') > -1 ? nav.href : helpers.urlFormat(nav.href);
            return nav;
        });
        return item;
    });

    return {
        homeNav: mHomeNav,
        activeNav: activeNav
    };
};

const _getAvatar = (uid) => {
    return userApi.getUserInfo(uid).then((result) => {
        return _.get(result, 'data.head_ico', '') || defaultAvatar;
    });
};

const _msgCount = (uid) => {
    return msgModel.unreadTotal(uid).then(result => _.get(result, 'data.total', 0));
};

const _getTabsData = (uid, channel) => {
    return Promise.props({
        msg: _msgCount(uid),
        header: headerModel.requestHeaderData(channel),
        avatar: _getAvatar(uid)
    });
};

const getHomeNav = (uid, channel, url, clientSwitcher) => {
    return _getTabsData(uid, channel).then((result) => {
        let navs = _getActiveNav(url, clientSwitcher, result.msg);

        let activeNav = navs.activeNav;
        let bread = [{href: helpers.urlFormat('/'), name: 'YOHO!BUY 有货首页'}];

        if (activeNav) {
            bread.push({
                name: '个人中心',
                href: helpers.urlFormat('/home')
            });

            bread.push({
                name: activeNav.name
            });

            // 订单详情
            if (activeNav.curHref === '/home/orders/detail') {
                Object.assign(_.last(bread), {
                    href: helpers.urlFormat('/home/orders')
                });
                bread.push({
                    name: '订单详情'
                });
            }
        } else {
            bread.push({
                name: '个人中心'
            });
        }

        return Object.assign({
            path: bread,
            homeNav: navs.homeNav,
            userThumb: result.avatar
        }, result.header);
    });
};

module.exports = {
    getHomeNav
};