simple-header.js
2.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
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> ' +
'<a href="//www.yohobuy.com/logout.html">[退出]</a>';
} else {
loginHtml = '<span>Hi~</span> ' +
'<a href="//www.yohobuy.com/signin.html?refer="' + window.location.href + '>[请登录]</a> ' +
'<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);
}());