userCommon.js 12 KB
/**
 * Created by moli on 15/5/26.
 */
$(function(){
    var DiliPath = $.extend(DiliPath,
		{
			loginPath : "https://passport.nong12.com/uc/login.aspx",
			registPath : "https://passport.nong12.com/regedit/regedit.aspx",
			homePath  : "http://www.nong12.com/",
			logoOut : "https://passport.nong12.com/uc/loginService?ltype=logout",
			userInfo : "http://user.nong12.com/account/security.html"
		}
	);
	var View = {
		info   : {
			'isLogin': '<em>欢迎您来到农丰网!</em><div class="islogin">请<a rel="nofollow" href="https://passport.nong12.com/uc/login.aspx?ReturnUrl=http://www.nong12.com/" class="hover-color-green">登录</a> <a rel="nofollow" href="https://passport.nong12.com/regedit/regedit.aspx" class="hover-color-green">免费注册</a></div>',
			'im': '<div class="im"> <a rel="nofollow" class="h-user-msg" href="http://user.nong12.com/msg/list.html">消息<span id="total">0</span></a> </div>'
		},
		menu   : '<ul><li><a rel="nofollow" href="http://user.nong12.com/account/security.html">账户中心</a></li><li class="more"><a rel="nofollow" href="http://user.nong12.com/purchase/my_buy_center.html">我的农丰<b class="icon-direction"></b></a><div class="menu-list"><div class="menu-list-panel"><a href="http://user.nong12.com/purchase/order.html">已买到的商品</a><a href="http://user.nong12.com/purchase/favorite/product.html">我的收藏</a><a href="http://user.nong12.com/purchase/history.html">我的浏览</a></div></div></li><li class="more"><a rel="nofollow" href="http://shop.nong12.com/order/list.html">销售中心<b class="icon-direction"></b></a><div class="menu-list"><div class="menu-list-panel"><a href="http://shop.nong12.com/open/shop.html">我的店铺</a> <a href="http://shop.nong12.com/order/list.html">已卖出的商品</a></div></div></li><li><a rel="nofollow" href="http://www.nong12.com/purchase_list.html">进货单<span class="color-green">(<span class="cart-num">0</span>)</span></a></li><li><a href="javascript:void(0);" onclick="IM.openWindow(\'农丰客服\',\'农丰客服\', \'\', \'\', \'\' , \'1\');">客服服务</a></li><li><a href="http://www.nong12.com/helpcenter.html">帮助中心</a></li></ul>'
	};
	
    View = {
        // headder : '<div class="info clearfix"><em>欢迎您来到农丰网!</em><div class="islogin">请'
                 // +'<a href="#" class="hover-color-green">登录</a> <a href="#" class="hover-color-green">免费注册</a></div>'
                 // +' <div class="im"> <a class="h-user-msg" href="http://user.nong12.com/msg/list.html">消息<span id="total">0</span></a>'
                 // +' </div> </div> <div class="menu">'
                 // +'<ul><li><a href="http://user.nong12.com/account/security.html">账户中心</a></li><li class="more"><a href="http://user.nong12.com/">采购中心<b class="icon-direction"></b></a>'
                 // +'<div class="menu-list"><div class="menu-list-panel"><a href="http://user.nong12.com/purchase/order.html">已买到的商品</a><a href="http://user.nong12.com/purchase/favorite/product.html">我的收藏</a>'
                 // +'<a href="http://user.nong12.com/purchase/history.html">我的浏览</a></div></div></li><li class="more"><a href="http://shop.nong12.com/seller/setting/main.html">销售中心<b class="icon-direction"></b></a>'
                 // +'<div class="menu-list"><div class="menu-list-panel"><a href="http://shop.nong12.com/open/shop.html">我的店铺</a> <a href="http://shop.nong12.com/order/list.html">已卖出的商品</a>'
                 // +'</div></div></li>'
                 // +'<li><a href="http://www.nong12.com/purchase_list.html">进货单<span class="color-green">(<span class="cart-num">0</span>)</span></a></li>'
                 // +'<li><a href="javascript:void(0);" onclick="IM.openWindow(\'农丰客服\',\'农丰客服\', \'\', \'\', \'\' , \'1\');" >客服服务</a></li>'
                 // +'<li><a href="http://www.nong12.com/helpcenter.html">帮助中心</a></li></ul></div></div>',
		headder: '<div class="info clearfix">' + View.info.isLogin + View.info.im + '</div><div class="menu">' + View.menu + '</div>',
		
        footer : '<div class="footer"><div class="helper-end"><div class="helper clearfix">'
                 +'<div class="help"><ul id="footer_cms"></ul></div><div class="contact"><span class="icon-shield"></span><ul class="clearfix">'
                 +'<li><span class="icon-f-phone"></span><p>服务热线:<span class="text-phone">400-645-5158</span></p></li>'
                 +'<li><span class="icon-f-customerservice"></span><p>服务时间:<span class="text-time">上午9:00 - 晚上21:00</span></p></li>'
                 +'<li><span class="icon-f-im"></span><p class="dili-im" onclick="javascript:IM.openWindow(\'农丰客服\',\'农丰客服\', \'\', \'\', \'\' , \'1\');"><span class="icon-im-call"></span>联系我们</p></li>'
                 +'</ul></div></div></div><div class="dili site-width"><div class="about"><a href="http://www.nong12.com/helpcenter-3-9.html">关于我们</a>'
                 +' | <a href="http://www.nong12.com/helpcenter-3-10.html">联系我们</a> | <a href="http://www.nong12.com/helpcenter-3-11.html">人才招聘</a> | <a href="http://www.nong12.com/helpcenter.html">帮助中心</a></div>'
                 +'<p><span id="friendlinks">友情链接:</span></p>'
				 +'<p>统一社会信用代码:91110105335598454P | 食品经营许可证编号:JY11105010076540 | 版权所有:北京农丰吉瑞网络科技有限公司</p>'
         +'<p> Copyright © 2013-2016 nong12.com, All Rights Reserved 京ICP备15030623号</p>'
         +'</div></div>'
    };


    var Methods = {
		searchval : {type : "STORE"},
		/* 依据data.dtype 发送ajax请求 */
        http : function(url, success, error, data){
            var parm = {
                type : 'GET',
                url : url,
                dataType : "jsonp",
                cache : false,
                success : function(data){
                    success(data);
                },
                error : function(ero){
                    if(error){error(ero)};
                }
            };
            if(data && data.dtype){
                parm.jsonp = data.dtype;
            }
            if(data && data.call){
                parm.jsonpCallback = data.call;
            }
            $.ajax(parm);
        },
		/* 初始化 需要使用的函数*/
        init : function(){
            $(".pnr-top .nav").append($(View.headder));
            $("body").append(View.footer);

            //检查登陆
            Methods.http("https://passport.nong12.com/uc/ws/callHelloJSONP", function(data){
                Methods.isLogin(data);
            }, function(){
                window.loginInfo = false;
                return false;
            });
            //加载底部
            Methods.http("http://www.nong12.com/common/footerJson.html", function(data){
                Methods.setFooter(data);
            }, function(){
                return false;
            }, {dtype : "callFn", call : 'success_jsonpCallback1' + (new Date()).getTime()});
            //友情链接
            Methods.http("http://www.nong12.com/common/fLinkJSONP.html", function(data){
                Methods.setFlink(data);
            }, function(){
                return false;
            }, {dtype : "callFn", call : 'success_jsonpCallback1' + (new Date()).getTime()});
        },
		/* 判断是否登录  javascript:IM.openWindow('农丰客服', '农丰客服', '0', '', '' , '1');$('#total').text(0);$('.noti.new-msg-icon').remove();*/
        isLogin : function(data){
            if(data && data.isLogin){
                //获取信息未读数量
                jQuery.ajax({
                    url : "http://user.nong12.com/msg/unReadCount.do",
                    type : "POST",
                    dataType : "jsonp",
                    jsonp:'callFn',
                    jsonpCallback:'success_jsonpCallback'+(new Date()).getTime(),
                    cache : false,
                    success : function(resp) {
                        if(resp){
                            $('#total').text(resp);
                        }
                    }
                });

                $(".islogin").html("");
                $(".islogin").html('<a href="'+DiliPath.logoOut+'" class="hover-color-green logout">退出</a>');
                $(".nav .info").prepend('<a href="'+DiliPath.userInfo+'"><em>'+data.userName+'</em></a>');
				
				//IM 登陆检测
				$("#right-login .state-success").show();
                $("#userName").html(data.userName);

				if(window.isRight){
					 jQuery.ajax({
						 type: "GET",
						 url: "http://user.nong12.com/api/userservice/getUserName.do",
						 dataType: "jsonp",
						 cache: false,
						 success: function (data) {
							 $("#my-face").attr("src", data.face);
						 }
					 });
					 
					window.loginInfo = true;
				}else{
					
				}
					 IM.notification(function (num) {
                        if (parseInt(num) > 0) {
                           $("#im_bubble").show();
                        }
                     });
				//$(".nav .im").append('<a class="h-user-msg" href="javascript:void(0)" id="notify"></a>');
				//IM.notification($("#notify"));
					 
				window.loginInfo = true;
            }else{
                $(".islogin .hover-color-green").eq(0).attr("href", DiliPath.loginPath + "?ReturnUrl=" + window.location.href);
                $(".islogin .hover-color-green").eq(1).attr("href", DiliPath.registPath);
				window.loginInfo = false;
            }
        },
		/* 动态设置底部HTML片段 */
        setFooter : function(data){
        	if(data){
        		var html = [];
        		var iconArr = ["icon-we", "icon-aftersales", "icon-shopping", "icon-identity", "icon-novice"];
        		for (var i = 0; i < data.length; i++) {
        			var showIcon = data.length <= iconArr.length ? iconArr[i] : iconArr[0];
        			html.push("<li><span class='" + showIcon + "'></span><p>" + data[i].text + "</p><div class='f-list'>");
        			var vc = data[i].subNodes;
        			for (var j = 0; j < vc.length; j++) {
        				if (j == vc.length - 1) {
        					html.push("<a rel='nofollow' href='" + vc[j].link + "'>" + vc[j].text + "</a></div></li>");
        				} else {
        					html.push("<a rel='nofollow' href='" + vc[j].link + "'>" + vc[j].text + "</a>");
        				}
        			}
        		}
        		$("#footer_cms").html(html.join(""));
        	}
        },
        setFlink : function(data){
          if(data && data.length){
            var _friendlinks = $("#friendlinks");

            _friendlinks.append($("<a class='col-999' href='" + data[0].link + "'>" + data[0].text + "</a>"))

            for (var i = 1, l = data.length; i < data.length; i++) {
			  _friendlinks.append(" | ");
              _friendlinks.append($("<a class='col-999' href='" + data[i].link + "'>" + data[i].text + "</a>"))
            };
          }else {
            $("#friendlinks").hide();
          };
        }
    };

    Methods.init();
	
  // 判断是否存在工具条,若没有则添加IM链接。
  if ($(".mui-bar").length == 0) {
    var units = [
      {'tpl': '<div></div>', 'classname': 'mui-bar', 'level': 0},
      {'tpl': '<div></div>', 'classname': 'msgbaby', 'level': 1},
      {'tpl': '<i></i>', 'classname': 'babyicon', 'level': 2},
      {'tpl': '<span></span>', 'classname': 'bubble', 'level': 2}
    ];   

    function _toDom(unitAry) {
      $.each(unitAry, function(i, elem){
        elem['target'] = _makeDom(elem);
      });

      $.each(unitAry, function(i, elem){
        switch (elem.level) {
          case 0:
            $('body').append(elem.target);
            break;
          default:
            $(unitAry[elem.level - 1].target).append(elem.target);
        }
      });

      // 设置 IM 调用点击属性
      $(unitAry[1].target).attr('onclick', "javascript:IM.openWindow('农丰客服', '农丰客服', '0', '', '' , '1');$('#total').text(0);$('.noti.new-msg-icon').remove();");
	  $(unitAry[3].target).attr('id', 'im_bubble');
    };

    function _makeDom(object) {
      return $(object.tpl).attr('class', object.classname)[0];
    };
	
	_toDom(units);
  };
  
});