main.js 10.3 KB

var cart = 
	{
		//用于购买按钮
		buy : function(callbackFunc){
			var goods_id = $('#goods_id').val();
			var size_id = $('#size_id').val();
			var size_name = $('#size_name').val();
			var num = $('#num').val();
			var promotion_id = $('#promotion_id').val();
			var promotion_type = $('#promotion_type').val();

			if(goods_id == 0){
				QGlobal.UI.alertWindow('请选择颜色','alertDialog','alertMessageBox',401);
				return;
			}
			if(size_id == 0 || size_name==''){
				QGlobal.UI.alertWindow('请选择尺码','alertDialog','alertMessageBox',401);
				return;
			}
			if(num < 1) {
				QGlobal.UI.alertWindow('请至少购买一件','alertDialog','alertMessageBox',401);
				return ;
			}
			if(!this.checkCartLimit(num)){
				QGlobal.UI.alertWindow('您买的商品太多了,请联系我们的客服,专门为您服务','alertDialog','alertMessageBox',401);
				return;
			}
			//window.open('http://www.baidu.com', 'baidu');
			this.AddToCart(goods_id, size_id, num, size_name, promotion_id, promotion_type, callbackFunc);
		},
		checkCartLimit : function(num){
			var cartInfo = eval('(' + $.cookie('_g') + ')');
	        if(cartInfo != null){
	        	if(parseInt(parseInt(cartInfo._nac) + parseInt(cartInfo._ac) + parseInt(num)) > 1000){	        		
	        		return false;
	        	}
	        }
	        return true;
		},
		//向购物车增加商品
		AddToCart : function(goods_id, size_id, num, size_name, promotion_id, promotion_type, callbackFunc){

			$.post('/shopping',{goods_id:goods_id,size_id:size_id,num:num,size_name:size_name,promotion_id:promotion_id,promotion_type:promotion_type}, 
					function(data){
						if(data.code == 200){
						//	window.open('/shopping/cart', 'cartList');
							eval(callbackFunc + '(200,' + data.data.totalNum + ',' + data.data.totalCost + ',' + data.data.goodsInCartId + ',"")');
						} else{
							eval(callbackFunc + '(' + data.code + ',' + num + ',' + 0 + ',0' +  ',"' + data.message +'")');
						}
					}, 'json');
		},
		
		/**
		 * 商品详情页面显示结果
		 */
		productDetailBuyResult : function(resultCode, totalNum, totalCost, goodsInCartId, msg){
			
			if(resultCode == 200) {
				$('#curTotalNum').html(totalNum);
				$('#curTotalCost').html(totalCost);
				$('#shoppingCart').dialog('open');
				$('#shoppingCart').show();
			} else {
				//失败的提示
				QGlobal.UI.alertWindow(msg,{'code':401});
			}			
		},
		
		/**
		 * 购物车购买结果显示
		 */
		cartBuyResult : function(resultCode, totalNum, totalCost, goodsInCartId, msg){
			if(resultCode == 200)
			{
				$('#dialogSellInfo').dialog('close');
				location.reload();
			}
			else
			{
				alert(msg);
			}
		},
		
		closeBuyResult : function(){
			$('#buyResult').hide();
		},		
		//移除商品
		doRemove : function(id, cartId, isPreSell, isGift, isOutlets) {			
			this.remove(id, cartId, isPreSell, isGift, isOutlets);			
		},
		remove : function(id, cartId, isPreSell, isGift, isOutlets) {
			 $.post('/shopping/cart/delone',{id:id,cartId:cartId}, function(data){
				 if(data.code == 200){
                 	cart.setNoGoodsStatus();
                 	if(isPreSell != 1){
                 		PromotionUtil.delGoods(id, isGift, isOutlets);
                     	PromotionUtil.showResultInCart();
                 	}                 	
                 	$('#trDetail' + id).remove();
                 }
			 }, 'json');
		},
		
		onlyRemove : function(id, cartId, isPreSell, isGift, isOutlets){
			$.post('/shopping/cart/delone',{id:id,cartId:cartId}, function(data){
				 if(data.code == 200){
                	cart.setNoGoodsStatus();
                	if(isPreSell != 1){
                		PromotionUtil.delGoods(id, isGift, isOutlets);
                	}                 	
                	$('#trDetail' + id).remove();
                }
			 }, 'json');
		} ,	
		
 		//从购物车移入收藏
		moveToFavorite : function(id, cartId, isPreSell, isGift, isOutlets) {
			$.post('/shopping/cart/movetofavorite', {cartId:cartId,id:id}, function(data){
				if(data.code == 200){
					if(isPreSell != 1){
						cart.setNoGoodsStatus();
	                	PromotionUtil.delGoods(id, isGift, isOutlets);
	                	PromotionUtil.showResultInCart();
					}                	
                	$('#trDetail' + id).remove();
                }else if(data.code == 401){
                	alert('您还没有登录,请先登录,然后再收藏该商品');
                	//QGlobal.UI.alertWindow('您还没有登录,请先登录,然后再收藏该商品','alertDialog','alertMessageBox',401);
                }				
			}, 'json');
		},
		
		/**
		 * 设置是否显示购物车已空
		 */
		setNoGoodsStatus: function(){
			var cartInfo = eval('(' + $.cookie('_g') + ')');
	        if(cartInfo != null){
	        	if(cartInfo._nac == 0){
	        		if(cartInfo._ac > 0){
	        			window.location.reload();
	        		}
	        		//显示购物车已空
	        		$('#noGoodsInCart').show();
	        		$('#commonTotalCost').html(0);
	        	}
	        	if(cartInfo._ac == 0){
	        		$('#divAdvanceTitle').hide();
	        		$('#advanceCartGoodsList').hide();
	        		$('#advanceFeeList').hide();
	        		$('#advanceBottom').hide();
	        	}
	        	
	        	if(cartInfo._nac == 0 && cartInfo._ac == 0)
	        	{
	        		//待优化
	        		window.location.reload();
	        	}
	        }
		},
		
		/**
		 * 清空购物车
		 */
		clearCart : function(cartId) {
			$.post('/shopping/cart/clearcart', {cartId:cartId}, function(data){
				if(data.code == 200){
                	window.location.reload();
                }				
			}, 'json');
		},
		
		/**
		 * 增加数量
		 */
		addNum : function(cartId, detailId, isPreSell, isOutlets) {
			var curNum = $('#curNum' + detailId).val();
			var num = parseInt(curNum, 10) + 1;
			if(!this.checkCartLimit(num)){
				QGlobal.UI.alertWindow('您买的商品太多了,请联系我们的客服,专门为您服务','alertDialog','alertMessageBox',401);
				return;
			}
			this.modifyNum(cartId, detailId, num, isPreSell, isOutlets);
		},
		
		//减少数量
		cutNum: function(cartId, detailId, isPreSell, isOutlets) {
			var curNum = $('#curNum' + detailId).val();
			var num = curNum -1;
			if(num < 1) {
				if(confirm('确定要把该商品从购物车删除吗?')){
					this.doRemove(detailId, cartId, isPreSell, 0, isOutlets);					
				}
			} else {
				this.modifyNum(cartId, detailId, num, isPreSell, isOutlets);
			}
		},
		
		//修改商品到制定数量
		modifyNum: function (cartId, detailId, num, isPreSell, isOutlets){
			$.post('/shopping/cart/changenum', {cartId:cartId,id:detailId,num:num},function(data){
				 if(data.code == 200){
                 	$('#curNum' + detailId).val(num); 
                 	//更新小计价格
                 	var gPrice = $('#gPrice' + detailId).val();
                 	$('#gTotalCost' + detailId).html(QGlobal.Viewhelper.formatMoney(num * gPrice));
                 	
                 	cart.setNoGoodsStatus();
                 	if(isPreSell != 1) {
                 		//更新购物车数组里产品的数量
                     	PromotionUtil.updateGoodsNum(detailId, num, isOutlets);
                     	//修改成功,重新计算促销方案
                     	PromotionUtil.showResultInCart();
                 	}
                 	
                 }				
			});
		},
		
		expandSwitch : function(btnId, expandId){
			if($('#' + expandId).is(":hidden") == false) {
				//当前为显示
				$('#' + expandId).hide();
			} else {
				$('#' + expandId).show();
			}
		},
		
		showSellInfo : function(productId, promotion_id, promotion_type){
			$.post('/shopping/cart/saleinfo', {product_id:productId,promotion_id:promotion_id,promotion_type:promotion_type},function(data){				
				$('#dialogSellInfo').html(data);
				$('#dialogSellInfo').dialog({
					autoOpen: false,
					width:640,
	                height:420,
					modal: true,
					resizable: false
				});
				$('#dialogSellInfo').dialog('open');
				initMiniProduct();
			});	
			
		},
		showCommonSellInfo : function(productId){
			$.post('/shopping/cart/commonsaleinfo', {product_id:productId},function(data){
				$('#dialogSellInfo').html(data);
				$('#dialogSellInfo').dialog({
					autoOpen: false,
					width:640,
	                height:420,
					modal: true,
					resizable: false
				});
				$('#dialogSellInfo').dialog('open');
				initMiniProduct();
			});
		},
		
		checkCartGift: function(isPreSell){
			if(!PromotionUtil.checkSelGift()){
				$('#selGiftNotice').dialog({
					autoOpen: false,
					width:360,
	                height:220,
					modal: false,
					resizable: false
				});
				$('#selGiftNotice').dialog('open');
				return false;
			}
			this.checkStorage(isPreSell);
		},
		
		/**
		 * 关闭选择赠品的提示
		 */
		closeGiftNotice : function(){
			$('#selGiftNotice').dialog('close');
		},
		
		clickFlag : 1,	 //防止多次点击	
		checkStorage : function(isPreSell){			
			if(cart.clickFlag == 0){
				return;
			}
			cart.clickFlag = 0;
			$.getJSON('/shopping/cart/checkstorage?isPreSell=' + isPreSell, function(jsonData){
				cart.clickFlag = 1;
				if(jsonData.code == 200){
					$('#submitOrder' + isPreSell).submit();
				}else{					
					QGlobal.UI.alertWindow("您要购买的“" + jsonData.data + "“库存不足,您可以进行缺货登记或选择其他的商品,感谢您的支持!",{boxWidth:420,boxHeight:160}); 
				}
			});
		},
		recommendsProduct : function(page, obj)
		{
			QGlobal.Page.QSend_Url = '/shopping/cart/recommendsproduct';
			QGlobal.Page.QItem = 'recommendsProduct';
			var str = page;
			QGlobal.Page.getList(str, obj);
		}
	
	};

/**
 * 收藏使用的js
 */
FavoriteUtil = {		
	boxList : function(page){
		QGlobal.Page.QSend_Url = '/home/favorite/boxlist';
		QGlobal.Page.QItem = 'favoriteList';
		QGlobal.Page.getList(page); 
	}	
};

/**
 * 购物流程的支付页面所用的功能
 */
cart.pay = {
	
		/**
		 * 去支付
		 */
		goPay : function()
		{
		/*	var order_code = $('#order_code').val();
			var payment = $(":radio[name='payment'][checked]").val();
			window.loc	ation = 'http://www.yohobuy.com/pay?order_code=' + order_code + "&payment_type=" + payment; */
			$('#payForm').submit();
			return false;
		},
		
		/**
		 * 选择一个支付方式
		 */
		selPayment : function(paymentId)
		{
			$('#payment_' + paymentId).attr('checked', 'true');
			var paymentName = $('#payimg' + paymentId).attr('alt');
			$('#spanBtnPay').html('使用' + paymentName + '付款');
		}
};