Authored by wangqing

增加登录状态

@@ -9,7 +9,9 @@ var $ = require('yoho.jquery'); @@ -9,7 +9,9 @@ var $ = require('yoho.jquery');
9 var handlebars = require('yoho.handlebars'); 9 var handlebars = require('yoho.handlebars');
10 var json2 = require('json2'); 10 var json2 = require('json2');
11 var noticeSuccess = false; 11 var noticeSuccess = false;
12 - 12 +var apiDomain = 'http://api.open.yohobuy.com';
  13 +var vipInfoCombine = null;
  14 +var loginInfoCombine = null;
13 /** 15 /**
14 * 判断为1的helper 16 * 判断为1的helper
15 * @param {[type]} v1 [description] 17 * @param {[type]} v1 [description]
@@ -24,6 +26,37 @@ handlebars.registerHelper('equalone', function(v1, options) { @@ -24,6 +26,37 @@ handlebars.registerHelper('equalone', function(v1, options) {
24 return options.inverse(this); 26 return options.inverse(this);
25 } 27 }
26 }); 28 });
  29 +/**
  30 + * 判断等级为3的helper
  31 + * @param {[type]} v1 [description]
  32 + * @param {[type]} options) { if (v1 [description]
  33 + * @return {[type]} [description]
  34 + */
  35 +
  36 +handlebars.registerHelper('equallevelthree', function(v1, options) {
  37 +
  38 + if (v1 === '3') {
  39 + return options.fn(this);
  40 + } else {
  41 + return options.inverse(this);
  42 + }
  43 +});
  44 +
  45 +/**
  46 + * 判断为0的helper
  47 + * @param {[type]} v1 [description]
  48 + * @param {[type]} options) { if (v1 [description]
  49 + * @return {[type]} [description]
  50 + */
  51 +
  52 +handlebars.registerHelper('equalzero', function(v1, options) {
  53 +
  54 + if (v1 === '0') {
  55 + return options.fn(this);
  56 + } else {
  57 + return options.inverse(this);
  58 + }
  59 +});
27 require('../plugin/yohocookie'); 60 require('../plugin/yohocookie');
28 require('../plugin/yohocart'); 61 require('../plugin/yohocart');
29 /** 62 /**
@@ -817,6 +850,7 @@ function actionExeTemplate() { @@ -817,6 +850,7 @@ function actionExeTemplate() {
817 var resulthtml = $('#goodcartempwarpper').html().replace(/\\/g, ''); 850 var resulthtml = $('#goodcartempwarpper').html().replace(/\\/g, '');
818 851
819 $('#goodcartempwarpper').html(resulthtml); 852 $('#goodcartempwarpper').html(resulthtml);
  853 + vipInfoCombine = handlebars.compile($('#tmpl-my-login-new').html())
820 } 854 }
821 /** 855 /**
822 * 执行搜索 856 * 执行搜索
@@ -828,6 +862,138 @@ function actionSearch() { @@ -828,6 +862,138 @@ function actionSearch() {
828 $('#query_key').search(searchDomain); 862 $('#query_key').search(searchDomain);
829 } 863 }
830 /** 864 /**
  865 + * 获取登录状态
  866 + * @return {[type]} [description]
  867 + */
  868 +function actionLoginState(_data) {
  869 + var timestamp = new Date().getTime();
  870 + var noLoginHtml = '<span>Hi~</span>[<a href="http://www.yohobuy.com/signin.html" class="list-a login-out ">请登录</a>]&nbsp;[<a href="http://www.yohobuy.com/reg.html" class="list-a login-out" onclick="">免费注册</a>]'
  871 + var loginHtml = '<span>Hi~<a href="http://www.yohobuy.com/home?t=' + timestamp + '">{{user_name}}</a></span>&nbsp;'+
  872 + '{{#equalone is_login}}'+
  873 + '[<a href="{{logout}}" class="list-a login-out">退出</a>]'+
  874 + '{{else}}'+
  875 + '[<a href="http://www.yohobuy.com/signin.html" class="list-a login-out">请登录</a>]&nbsp;'+
  876 + '[<a href="http://www.yohobuy.com/reg.html" onclick="" class="list-a login-out">免费注册</a>]'+
  877 + '{{/equalone}}';
  878 + var boxObj = $('#loginBox');
  879 + var info = $.cookie('_UID');
  880 + loginInfoCombine = handlebars.compile(loginHtml);
  881 +
  882 + if (typeof info == 'undefined' || info == null) {
  883 + boxObj.html(noLoginHtml);
  884 + return false;
  885 + }
  886 + var user = info.split('::');
  887 + if (typeof user == 'undefined' || user.length < 4) {
  888 + boxObj.html(noLoginHtml);
  889 + return false;
  890 + }
  891 + var userName = user[0] || ' ';
  892 + var name = '';
  893 + var _length = 0;
  894 + for (var t = 0; t < userName.length; t++) {
  895 + var char = userName.substr(t, 1);
  896 + if (/.*[\u4e00-\u9fa5]+.*$/.test(char)) {
  897 + _length += 2;
  898 + } else {
  899 + _length += 1;
  900 + }
  901 + }
  902 + if (_length <= 10) {
  903 + name = userName;
  904 + } else {
  905 + _num = 0;
  906 + for (var t = 0; t < userName.length; t++) {
  907 + if (_num < 10) {
  908 + var char = userName.substr(t, 1);
  909 + if (char != '*') {
  910 + if (/.*[\u4e00-\u9fa5]+.*$/.test(char)) {
  911 + _num += 2;
  912 + } else {
  913 + _num += 1;
  914 + }
  915 + }
  916 + name += char;
  917 + }
  918 + }
  919 + if (name.length < userName.length) {
  920 + name += '...';
  921 + }
  922 + }
  923 + var _logout = '';
  924 + if (/http:\/\//.test(user[3])) {
  925 + _logout = user[3].replace('www.yohobuy.com', 'www.yohobuy.com');
  926 + } else {
  927 + _logout = 'http://www.yohobuy.com/logout_' + user[3] + '.html';
  928 + }
  929 + var data = {
  930 + "user_name": name,
  931 + "logout": _logout,
  932 + "random": Math.random()
  933 + };
  934 + var is_login = '1';
  935 + if (_data.result === -1) {
  936 + is_login = '-1';
  937 + }
  938 + data['is_login'] = is_login;
  939 + boxObj.html(loginInfoCombine(data));
  940 +}
  941 +/**
  942 + * 获得vip用户信息
  943 + * @return {[type]} [description]
  944 + */
  945 +function actionVipInfo(_data) {
  946 + if (_data.result == 1) {
  947 + var vipInfo = _data.data;
  948 +
  949 + if (typeof vipInfo == undefined || vipInfo.length < 1) {
  950 + return false;
  951 + }
  952 + $('#myYohoBox').mouseenter(function() {
  953 + $(this).addClass('acttags');
  954 + $('#myYohoBox .myyoho-info').show().html(vipInfoCombine(vipInfo));
  955 + var headUrl = $('.myyoho-photo img').attr('data-url');
  956 + if ($._checkUrlState == 1) {
  957 + $('.myyoho-photo').show().find('img').attr('src', headUrl);
  958 + return;
  959 + }
  960 + if (headUrl == '' || $._checkUrl != '') {
  961 + return;
  962 + }
  963 + $.ajax({
  964 + url: headUrl,
  965 + type: 'GET',
  966 + complete: function(response) {
  967 + if (response.status == 200 || response.status == 0) {
  968 + $('.myyoho-photo').show().find('img').attr('src', headUrl);
  969 + $._checkUrlState = 1;
  970 + }
  971 + $._checkUrl = headUrl;
  972 + }
  973 + });
  974 +
  975 + ;
  976 + });
  977 + $('#myYohoBox').mouseleave(function() {
  978 + $(this).removeClass('acttags');
  979 + $('#myYohoBox .myyoho-info').hide()
  980 + });
  981 + } else {}
  982 +}
  983 +/**
  984 + * 获取登录信息
  985 + * @return {[type]} [description]
  986 + */
  987 +function actionLoginInfo() {
  988 + var param = {
  989 + method: 'open.passport.get'
  990 + };
  991 + $.getData(apiDomain, param, function(_data) {
  992 + actionLoginState(_data); // 更改登录状态
  993 + actionVipInfo(_data);//获得vip;
  994 + });
  995 +}
  996 +/**
831 * 初始化函数 997 * 初始化函数
832 * @return {[type]} [description] 998 * @return {[type]} [description]
833 */ 999 */
@@ -849,6 +1015,7 @@ function init() { @@ -849,6 +1015,7 @@ function init() {
849 actionClickMiniCartBox(); //点击购物车跳转 1015 actionClickMiniCartBox(); //点击购物车跳转
850 actionListenDelCarGoods(); //监听购物车删除 1016 actionListenDelCarGoods(); //监听购物车删除
851 actionListenCartMore(); // 1017 actionListenCartMore(); //
  1018 + actionLoginInfo();//获取登录信息
852 } 1019 }
853 1020
854 init(); 1021 init();