Authored by yangyang

Merge branch 'develop' of http://git.dev.yoho.cn/web/yohobuy into feature/web-list

@@ -7,3 +7,95 @@ if ($('.simple-header').size() > 0) { @@ -7,3 +7,95 @@ if ($('.simple-header').size() > 0) {
7 $(this).find('.tool-select').fadeOut(); 7 $(this).find('.tool-select').fadeOut();
8 }); 8 });
9 } 9 }
  10 +
  11 +/**
  12 + * 格式化用户名
  13 + * @return {[type]} [description]
  14 + */
  15 +function formatUsernName(userName) {
  16 + var name,
  17 + char,
  18 + _num = 0,
  19 + _length = 0,
  20 + t;
  21 +
  22 + for (t = 0; t < userName.length; t++) {
  23 + char = userName.substr(t, 1);
  24 + if (/.*[\u4e00-\u9fa5]+.*$/.test(char)) {
  25 + _length += 2;
  26 + } else {
  27 + _length += 1;
  28 + }
  29 + }
  30 + if (_length <= 10) {
  31 + name = userName;
  32 + } else {
  33 + _num = 0;
  34 + name = '';
  35 + for (t = 0; t < userName.length; t++) {
  36 + if (_num < 10) {
  37 + char = userName.substr(t, 1);
  38 + if (char !== '*') {
  39 + if (/.*[\u4e00-\u9fa5]+.*$/.test(char)) {
  40 + _num += 2;
  41 + } else {
  42 + _num += 1;
  43 + }
  44 + }
  45 + name += char;
  46 + }
  47 + }
  48 + if (name.length < userName.length) {
  49 + name += '...';
  50 + }
  51 + }
  52 + return name;
  53 +}
  54 +
  55 +/**
  56 + * 设置简单头部登陆状态
  57 + * @return {[type]} [description]
  58 + */
  59 +function setLoginStatus(_data) {
  60 + var loginHtml,
  61 + name;
  62 +
  63 + if (_data && _data.href && _data.href.user) {
  64 + name = formatUsernName(_data.href.user);
  65 + loginHtml = '<span>Hi~</span><a href="">' +
  66 + name + '</a>&nbsp;<a href="' + _data.href.logout + '">[退出]</a>';
  67 + } else {
  68 + name = formatUsernName(_data.user);
  69 + loginHtml = '<span>Hi~</span><a href="">' +
  70 + name + '</a>&nbsp;<a href="http://www.yohobuy.com/signin.html">[请登录]</a>' +
  71 + '<a href="http://www.yohobuy.com/reg.html">[免费注册]</a>';
  72 + }
  73 + $('.header-tool li').eq(0).html(loginHtml);
  74 +}
  75 +
  76 +/**
  77 + * 获取登录信息
  78 + * @return {[type]} [description]
  79 + */
  80 +function actionLoginInfo() {
  81 + $.ajax({
  82 + type: 'GET',
  83 + url: '/common/getSimpleHeader',
  84 + data: {},
  85 + success: function(data) {
  86 + if (data.code === 200) {
  87 + setLoginStatus(data.data);
  88 + }
  89 + }
  90 + });
  91 +}
  92 +
  93 +/**
  94 + * 初始化函数
  95 + * @return {[type]} [description]
  96 + */
  97 +function init() {
  98 + actionLoginInfo(); //获取登录信息
  99 +}
  100 +
  101 +init();