head_top.js 8.71 KB
$(function(){
	//渲染hello头
	jQuery.ajax({
		type : "GET",
		url : "https://passport.nong12.com/uc/ws/helloService",
		dataType : "jsonp",
		success : function(data) {
			if (data) {
				if(data.info.indexOf("logout") > 0) {
					$(".top .left").remove();
					$(".top .right").before(data.info);
					if(window.pageConfig){
						if (window.pageConfig.navId && window.pageConfig.navId == "home"){
							$(".pack-mo-tr1").find(".btn-big-y").each(function(){
								$(this).remove();
							});
							$(".pack-mo-tr1").prepend(
									'<a class="btn-big-w" href="http://user.nong12.com/purchase/order_pay.html"><img width="14" height="14" src="http://static.nong12.com/static/common/images/i/obligation.png"> 待付款</a>'
									+ '<a class="btn-big-w" href="http://user.nong12.com/purchase/order_delivery.html"><img width="16" height="15" src="http://static.nong12.com/static/common/images/i/picking.png"> 待提货</a>').addClass("p-l1");
						}
					}
				}else {
					$(".top .left a").first().attr("href",DiliPath.loginPath + "?ReturnUrl=" + window.location.href);
					$(".top .left a").last().attr("href",DiliPath.registPath);
				}
			}
		},
		error : function() {
			return false;
		}
	});
	
	//渲染footer
	jQuery.ajax({
		type : "GET",
		url : DiliPath.homePath + "common/footerJson.html",
		dataType : "jsonp",
		jsonp : "jsonpCallback",
		success : function(data) {
			var html = [];
			for ( var i = 0; i < data.length; i++) {
				html.push("<li><i class='m-icon icon-a" + (i+1) + "'></i> <span>" + data[i].title + "</span><br>");
				var vc = eval(data[i].extendsOne);
				for ( var j = 0; j < vc.length; j++) {
					if(j == vc.length - 1){
						html.push("<a href='" + vc[j].link + "'>" + vc[j].text + "</a></li>");
					}else{
						html.push("<a href='" + vc[j].link + "'>" + vc[j].text + "</a><br>");
					}
				}
			}
			$(".m-left ul").empty().append(html.join(""));
		}
	});
	
	//迷你购物车加载
	cartTools.showCart();
	$(".mo-fn3 a").hover(function() {
		cartTools.showCart();
	}, function() {});
	
	if($("head").find("script[src*='jquery.lazyload']").size()>0){
		$("body").find("img").each(function(){
			$(this).lazyload({
				effect : "fadeIn",
				skip_invisible : false
			});	
		});
	}
	
});
//公用系统域名
window.DiliPath = {
		homePath : "http://www.nong12.com/",
		userPath : "http://user.nong12.com/",
		staticPath : "http://static.nong12.com/",
		shopPath : "http://shop.nong12.com/",
		loginPath : "https://passport.nong12.com/uc/login.aspx",
		registPath : "https://passport.nong12.com/regedit/regedit.aspx",
		packageProductPath : function(productId) {
			return DiliPath.homePath + "product/" + productId + ".html";
		}
};

window.Money = {
	centToYuan : function(cent) {
		cent = parseInt(cent);
		if (cent <= 0) {
            return "0.00";
        }
		yuan = (cent / 100).toFixed(2);
		var result = yuan.toString();
		if(result.indexOf(".") <= 0) {
			result = result + ".00";
		}
        return result;
	}
};

var cartTools = {
	/**
	 * 加入购物车addrId:交割地 amount:数量 price:单价 sku:skuId
	 * @param addrId
	 * @param amount
	 * @param price
	 * @param sku
	 */
	addCart : function(addrId, amount, price, sku,fn) {
		jQuery.ajax({
			url : DiliPath.homePath + "cart/addCart.html",
			type : "POST",
			data : {addrId:addrId,amount:amount,price:price,sku:sku,userId:0},
			dataType : "jsonp",
			jsonp: "jsonpCallback",
			cache : false,
			success : function(result) {
				if(fn && typeof(fn) == 'function'){
					fn(result);
				}
				cartTools.showCart();
			}
		});
	},
	/**
	 *  渲染迷你购物车
	 */
	showCart :  function () {
	    jQuery.ajax({
	        url: DiliPath.homePath + "cart/getMiniCart.html",
	        type: "POST",
	        dataType : "jsonp",//数据类型为jsonp  
	        jsonp: "jsonpCallback",//服务端用于接收callback调用的function名的参数  
	        cache: true,
	        success: function (miniCart) {
	            if (miniCart) {
	                $(".receipt-icon").html(miniCart.length);
	                if($(".hover-bill").length > 0){
	                	 $(".a-tit").siblings("b").html("(" + miniCart.length + ")").attr(
	                             "count", miniCart.length);
	                         var miniCartHtml = [];
	                         var totalPrice = 0;
	                         if (miniCart.length == 0) {
	                             $(".hover-bill .u-con-no").remove();
	                             $(".hover-bill").append('<div class="u-con-no">购物车中还没有商品,赶紧选购吧!</div>');
	                             $(".hover-bill ul, .tab-xd, #near").hide();
	                         } else {
	                             $(".hover-bill ul, .tab-xd, #near").show();
	                             for (var i = 0; i < miniCart.length; i++) {
	                                 miniCartHtml[i] = "<li price='"
	                                     + miniCart[i].price
	                                     + "' amount='"
	                                     + miniCart[i].amount
	                                     + "'><a href='" + DiliPath.homePath + "product/" + miniCart[i].pid + ".html' class='a-img'><img data-original='"
	                                     + miniCart[i].productImage
	                                     + "' src='" + DiliPath.staticPath + "static/common/images/gg/loading.gif' width='40' height='40' /></a><dl><dt><a href='" + DiliPath.homePath + "product/" + miniCart[i].pid + ".html'>"
	                                     + miniCart[i].productName
	                                     + " </a></dt><dd>¥"
	                                     + Money.centToYuan(miniCart[i].price)
	                                     + " * "
	                                     + miniCart[i].amount
	                                     + "</dd></dl><a href='#' class='a-del' id='"
	                                     + miniCart[i].sku + "' addrId='" + miniCart[i].addrId + "'></a></li>";
	                                 totalPrice += miniCart[i].price
	                                     * miniCart[i].amount;
	                             }
	                             $(".hover-bill .u-con-no").remove();
	                             $(".hover-bill ul").empty();
	                             $(".hover-bill ul").append(miniCartHtml.join(""));
	                             $(".hover-bill ul .a-del").each(function () {
	                                 $(this).click(function () {
	                                	 cartTools.delMiniCart($(this).attr("id"), $(this).attr("addrId"));
	                                     return false;
	                                 });
	                             });
	                             $(".hover-bill ul").find("img").each(function () {
	                                 $(this).lazyload();
	                             });
	                             $(".tab-xd b").empty();
	                             $(".tab-xd b").html("¥" + Money.centToYuan(totalPrice));
	                             overflow_y($('.hover-bill ul'), miniCart.length * 71, 355);
	                         }
	                }
	            }
	        }
	    });
	},
	/**
	 * 删除迷你购物车中的sku
	 * @param skuId
	 * @param addrId
	 */
	delMiniCart : function(skuId,addrId) {
		jQuery.ajax({
			url : window.DiliPath.homePath+"cart/delSkuFromCart.html",
			type : "POST",
			dataType : "jsonp",//数据类型为jsonp  
		    jsonp: "jsonpCallback",//服务端用于接收callback调用的function名的参数 
			data : {sku:skuId,addrId:addrId},
			async : false,
			success : function(result) {
				if (result) {
					if (result.flag) {
						$("#" + skuId).parents("li").slideUp("fast",function(){
	                    	$("#" + skuId).parents("li").remove();
	                    });
	                    $(".tab-xd b").html("¥" + cartTools.compute());
	                    var cartCount = parseInt($(".a-tit").siblings("b").attr("count")) - 1;
	                    $(".a-tit").siblings("b").html("(" + cartCount + ")").attr(
	                        "count", cartCount);
						Tools.amHover($(".receipt-icon"), 'flipInH');
	                    $(".receipt-icon").html(cartCount);
	                    if(cartCount == 0){
	                    	$(".hover-bill .u-con-no").remove();
	                        $(".hover-bill").append('<div class="u-con-no">购物车中还没有商品,赶紧选购吧!</div>');
	                        $(".hover-bill ul, .tab-xd, #near").hide();
	                    }
	                    overflow_y($('.hover-bill ul'), cartCount*71, 355);
					}
				}
			}
		});
	},
	/**
	 * 计算购物车价格
	 */
	compute : function(){
		var totalPrice = 0;
		$(".hover-bill ul li").each(function() {
			totalPrice += $(this).attr("price") * $(this).attr("amount");
		});
		return Money.centToYuan(totalPrice);
	}
};