simple-header.js 2.84 KB
var $ = require('yoho-jquery');

var $apiDom = $('#api-domain');

require('./common');

// var apiDomain = $apiDom.val(); // 获取数据的地址

$apiDom.remove(); // 删除API信息

if ($('.simple-header').size() > 0) {
    $('.tool-options').on('mouseenter', function() {
        $(this).find('.tool-select').fadeIn();
    }).on('mouseleave', function() {
        $(this).find('.tool-select').fadeOut();
    });
}

/**
 * 格式化用户名
 * @return {[type]} [description]
 */
function formatUsernName(userName) {
    var name,
        char,
        _num = 0,
        _length = 0,
        t;

    for (t = 0; t < userName.length; t++) {
        char = userName.substr(t, 1);
        if (/.*[\u4e00-\u9fa5]+.*$/.test(char)) {
            _length += 2;
        } else {
            _length += 1;
        }
    }
    if (_length <= 10) {
        name = userName;
    } else {
        _num = 0;
        name = '';
        for (t = 0; t < userName.length; t++) {
            if (_num < 10) {
                char = userName.substr(t, 1);
                if (char !== '*') {
                    if (/.*[\u4e00-\u9fa5]+.*$/.test(char)) {
                        _num += 2;
                    } else {
                        _num += 1;
                    }
                }
                name += char;
            }
        }
        if (name.length < userName.length) {
            name += '...';
        }
    }
    return name;
}

/**
 * 设置简单头部登陆状态
 * @return {[type]} [description]
 */
function setLoginStatus(data) {
    var info = data,
        loginHtml,
        name;

    if (data.result !== -1) {
        name = formatUsernName(info.profileName);
        loginHtml = '<span>Hi~</span>' +
            '<a href="//www.yohobuy.com/home?t=' + info.random + '">' + name + '</a>&nbsp;' +
            '<a href="//www.yohobuy.com/logout.html">[退出]</a>';
    } else {
        loginHtml = '<span>Hi~</span>&nbsp;' +
            '<a href="//www.yohobuy.com/signin.html?refer="' + window.location.href + '>[请登录]</a>&nbsp;' +
            '<a href="//www.yohobuy.com/reg.html"' + window.location.href + '>[免费注册]</a>';
    }
    $('.header-tool li').eq(0).html(loginHtml);
}

/**
 * 获取登录信息
 * @return {[type]} [description]
 */
function actionLoginInfo() {
    var param = {
        return_type: 'jsonp'
    };

    $.getJSON('//www.yohobuy.com/common/passport/?callback=?', param, function(_data) {
        if (_data && _data.data) {
            setLoginStatus(_data.data);
        }
    });
}

// 获取头部登陆信息
(function() {
    var uid = getUid(),   //eslint-disable-line
        profileName = getProfileName();  // eslint-disable-line

    var info = {
        random: $.now(),
        profileName: profileName
    };

    if (uid !== 0) {
        info.result = 1;
    } else {
        info.result = -1;
    }

    setLoginStatus(info);
}());