javascript 面向对象编程

1.http://www.ibm.com/developerworks/cn/web/1304_zengyz_jsoo/


/*普通商品操作*/
function Cart_Model() {
	this.cart_url = "/cart";
	this.cart_ids = [];
	this.is_init = false;
	this.add = function(goods_id, num, success, fail){
		var _this = this;
		var data = {goods_id : goods_id, num : num};
		$.post(this.cart_url + "/add", data, function(result){
			if(result.s == 10) {
				goto_login(function(){
					window.location.reload();
				});
			}
			else if(result.s == 1) {
				if(result.d.one_step_buy == 1) {
					window.location.href = "/checkout";
					return;
				}else{
					_this.is_init = false;
					success?success(result):"";
					if($('.top_cart_list').length>0) {
						$.get("/cart/top_cartlist", function(result){
							$('.top_cart_list').html(result);
						}, "html");
					}
				}
			}
			else {
				fail?fail(result):"";
			}
		}, "json");
	};
	
	this.del = function(cart_id, success, fail) {
		var _this = this;
		$.post(this.cart_url + "/del", {cart_id:cart_id}, function(result){
			if(result.s == 1) {
				_this.is_init = false;
				success?success(result):"";
			}
			else {
				fail?fail(result):"";
			}
		}, "json");
	};

	this.delall = function(success, fail) {
		var _this = this;
		var chk_value = [];    
		$('.cart_list .selectAll:checked').each(function(){    
			chk_value.push($(this).val());
		});
		if(chk_value.length <= 0) {
			alert("未选中任何商品");
		}
		$.post(this.cart_url + "/delall", {cart_ids:chk_value}, function(result){
			if(result.s == 1) {
				_this.is_init = false;
				success?success(result):"";
			} else {
				fail?fail(result):"";
			}
		}, "json");
	}

	this.change = function(cart_id, num, success, fail){
		var _this = this;
		var data = {cart_id : cart_id, num : num};
		$.post(this.cart_url + "/change", data, function(result){
			if(result.s == 1) {
				_this.is_init = false;
				success?success(result):"";
			} else {
				fail?fail(result):"";
			}
		}, "json");
	};

	this.select = function(cart_ids, success) {
		var is_min = 0;
		var cart_class = ".cart_list";
		if(typeof(is_cart) == "undefined") {
			is_min = 1;
			cart_class = ".mini-cart";
		}
		this.cart_ids = cart_ids;
		this.is_init = false;
		this.init();
	};
	
	this.count = function() {
		$.get(this.cart_url + "/count", {is_min:is_min}, function(result){
			$(cart_class).html(result);
		}, "html");
	};
	
	this.init = function() {
		var _this = this;
		if(this.is_init) return;
		var is_min = 0;
		var cart_class = ".cart_list";
		if(typeof(is_cart) == "undefined") {
			is_min = 1;
			cart_class = ".mini-cart";
		}
		if($(cart_class).length>0) {
			$.get(this.cart_url + "/cartlist", {is_min:is_min, cart_ids:this.cart_ids}, function(result){
                          console.log("********"+this.cart_ids);
				$(cart_class).html(result);
				if(is_min) {
					setHeight();
				}
				_this.is_init = true;
				$(".cart_goods_num").html($("#cart_count").text());

				if($('.top_cart_list').length>0) {
					$.get("/cart/top_cartlist", function(result){
						$('.top_cart_list').html(result);
					}, "html");
				}
			}, "html");
		}
		else if($(".cart_goods_num").length>0){
			$.get(this.cart_url + "/cartcount", function(result){
				$(".cart_goods_num").html(result.d.cartcount);
			}, "json");
		}
	};
}


$(document).ready(function(){
	setTimeout(function(){
    	cart.init();
    }, 500);

	$(".buy_one").bind("click",function(e){
		var goods_id = parseInt($(this).attr("this_id"));
		if(goods_id <=0) {
			return false;
		}
		cart.add(goods_id, 1, function(result){
			$(".cart_msg").show();
			$(".cart_msg").find(".fr").html("<strong>" + result.m + "</strong><p>您可以<a href='"+cart.cart_url+"'>去购物车结算</a></p>");
			$(".cart_goods_num").html(result.d.count);
			setTimeout(function(){
				$(".cart_msg .ico_close").click();
			}, 3000);
		}, function(result){
			$(".cart_msg").show();
			$(".cart_msg").find(".fr").html(result.m);
			setTimeout(function(){
				$(".cart_msg .ico_close").click();
			}, 3000);
		});
	});
	$(".buy_btn").bind("click",function(e){
		var goods_id = parseInt($(".buy_value").attr("this_id"));
		var num = parseInt($(".buy_value").val());
		if(goods_id <=0) {
			return false;
		}
		if(num <= 0) {
			return false;
		}
		cart.add(goods_id,num, function(result){
			/*$("#flyItem").css({left:e.pageX, top:e.pageY-$("body").scrollTop() });
			$("#flyItem").css("visibility", "visible");
			$("#flyItem").find("img").attr("src", $("#goods_imgs").find("img").attr("src"));
			// 需要重定位
			myParabola.position().move();*/
			/*..$(".cart_msg").show();
			$(".cart_msg").find(".fr").html("<strong>" + result.m + "</strong><p>您可以<a href='"+cart.cart_url+"'>去购物车结算</a></p>");
			$(".cart_goods_num").html(result.d.count);
			setTimeout(function(){
				$(".cart_msg .ico_close").click();
			}, 3000);*/ 
			window.location.href='/cart/success/item_id/'+cart_add_item_id+'/'+goods_id;
		}, function(result){
			$(".cart_msg").show();
			$(".cart_msg").find(".fr").html(result.m);
			setTimeout(function(){
				$(".cart_msg .ico_close").click();
			}, 3000);
		});
	});
	$(".cart_list").on("click", ".delall", function(){
		cart.delall(function(){cart.init();});
	});
	$(".cart_list").on("click", ".del_cart", function(e) {

		var cart_id = $(this).attr("this_id");
		var top = $(this).offset().top - $(".del_cart_tip").height() - parseInt($(".del_cart_tip").css("padding-top")) - parseInt($(".del_cart_tip").css("padding-bottom")) - 5; 
		var left = $(this).offset().left - $(".del_cart_tip").width()/2 + $(this).width()/2;

		$(".del_cart_tip").show().offset({left:left,top:top}).find(".confirm").attr("this_id", cart_id);
	});

	$(".del_cart_tip .confirm").click(function(){
		var cart_id = $(this).attr("this_id");
		cart.del(cart_id, function(result) {
			cart.init();
			$(".del_cart_tip .cancel").click();
		}, function(result){
			alert(result.m);
			$(".del_cart_tip .cancel").click();
		});
	});
	$(".cart_panel").on("click", ".del_cart", function(){
		var cart_id = $(this).parent().find(".tm-mcElect").val();
		cart.del(cart_id, function(result) {
			cart.init();
		}, function(result){
			alert(result.m);
		});
	});
	$(".del_cart_tip .cancel").click(function(){
		$(".del_cart_tip").hide();
	});
	var change_timeout = null;

	$(".cart_list").on("change", ".buy_value", function(e) {
		$(this).val(parseInt($(this).val()));
		var cart_id = $(this).attr("this_id");
		var num = $(this).val();
		var _this = $(this);
		clearTimeout(change_timeout);
		change_timeout = setTimeout(function(){
			cart.change(cart_id, num, function(result) {
			cart.init();
			}, function(result){
				_this.parent().next().html(result.m);
				_this.parent().next().show();
				setTimeout(function(){
					_this.parent().next().stop();
					_this.parent().next().fadeOut(500);
				}, 3000)
				_this.val(result.d);
			});
		},300);
	});
	$(".cart_list").on("click", ".buy_value_min", function(e) {
		var val = parseInt($(this).next().val());
		$(this).next().val((val-1)<1?1:(val-1));
		$(this).next().change();
	});
	$(".cart_list").on("click", ".buy_value_add", function(e) {
		var val = parseInt($(this).prev().val());
		$(this).prev().val(val+1);
		$(this).prev().change();
	});
	$(".cart_list").on("change", ".selectAll", function(){
		$(".choice").prop("checked", $(".selectAll:not(:checked)").length<=0);
		var chk_value =[];    
		$('.cart_list .selectAll:not(:checked)').each(function(){    
			chk_value.push($(this).val());
		});
		cart.select(chk_value);
	});
	$(".cart_list").on("change", ".choice", function(){
		$(".selectAll").prop("checked", $(this).prop("checked"));
		$(".selectAll").eq(0).change();
		$(".choice").not($(this)).prop("checked", $(this).prop("checked"));
	});
	$(".cart_panel").on("change", ".tm-mcElect", function(){
		$(".tm-mcElectCart").prop("checked", $(".tm-mcElect:not(:checked)").length<=0);
		var chk_value =[];    
		$('.cart_panel .tm-mcElect:not(:checked)').each(function(){    
			chk_value.push($(this).val());
		});
		cart.select(chk_value);
	});
	$(".cart_panel").on("change", ".tm-mcElectCart", function(){
		$(".tm-mcElect").prop("checked", $(this).prop("checked"));
		$(".tm-mcElect").change();
	});
	$(".cart_msg .ico_close").click(function(){
		$(".cart_msg").hide();
		return false;
	});
	$(".cart_msg .ico_close").mouseenter(function(){return false;});

	$(".cart_list").on("click", ".goto_login", function(){
		goto_login(function(){
			is_login = 1;
			cart.select([]);
		});
	});

	$(".cart_list").on("click", "#checkout", function(){
		if(!is_login) {
			goto_login(function(){
				is_login = 1;
				cart.select([]);
			});
			return false;
		}
		var chk_value =[];
		$('.cart_list .selectAll:not(:checked)').each(function(){    
			chk_value.push($(this).val());
		});
		if($('.cart_list .selectAll:checked').length <= 0) {
			alert("未选中任何商品");
			return false;
		}
		post('checkout', {cart_ids:chk_value});
	});

	$('.goods_box').on("click", ".add_favorites", function(){
		if(!is_login) {
			location.href="/login";
		}
		var id = $(this).attr("this_id");
		$.post("user/addFavorites", {id:id}, function(result){
			if(result.s == 1) {
				alert(result.m);
			} else {
				alert(result.m);
			}
		}, "json");
	});


	$('.cart_list').on("click", ".add_favorites", function(){
		if(!is_login) {
			goto_login(function(){
				is_login = 1;
				cart.select([]);
			});
			return false;
		}
		var id = $(this).attr("this_id");
		var item_id = $(this).attr("this_item_id");
		$.post("/favorites", {'goods_id':id,'item_id':item_id}, function(data){
			if(data.ret > 0) {
				$(".cart_add_favorites_suc").show();
				setTimeout("$('.cart_add_favorites_suc').hide()",2000);
			} else {
				$(".cart_add_favorites_suc").hide();
				alert('收藏失败!');
			}
		}, "json");
	});

	$('.top_cart,.top_cart_list').hover(function(){
        $('.top_cart').addClass('on').find('i').removeClass('icon_arrow').addClass('icon_up');
        $('.top_cart_list ').show();

    },function(){
         $('.top_cart_list ').hide();
        $('.top_cart').removeClass('on').find('i').removeClass('icon_up').addClass('icon_arrow');
    });
});
function post(URL, PARAMS) {        
    var temp = document.createElement("form");        
    temp.action = URL;        
    temp.method = "post";        
    temp.style.display = "none";        
    for (var x in PARAMS) {
        if(typeof(PARAMS[x]) == "object") {
        	for(var i in PARAMS[x]) {
        		var opt = document.createElement("textarea");   
        		opt.name = x + "[]";  
        		opt.value = PARAMS[x][i]; 
        		temp.appendChild(opt);
        	}
        } else {
        	var opt = document.createElement("textarea");   
        	opt.name = x;        
        	opt.value = PARAMS[x];  
        	temp.appendChild(opt);          
        }
    }        
    document.body.appendChild(temp);        
    temp.submit();        
    return temp;        
} 
var cart = new Cart_Model();

var _login_success = function() {};
function goto_login(success) {
	$.get("/login?is_small=1",function(data){
		_login_success = success;
		$("#dialog").html(data);
		$("#login_panel").show();
		$("#register_panel").hide();
		var dialogW=$('#dialog').width();
		var dialogH=$('#dialog').height();
		$('#dialog').css({'marginLeft':-dialogW/2,'marginTop':-dialogH/2});
		$('#dialog,#fullbg').fadeIn();
	});
}

/*反向团购购买操作*/
function Group_rev_Model() {
	this.add = function(id, goods_id, num, success, fail){
		var _this = this;
		$.post("/cart/group_rev_add", {id: id, goods_id: goods_id, num: num}, function(ret){
			if(ret.s == 10) {
				goto_login(function(){
					window.location.reload();
				});
			}
			else if(ret.s == 1) {
				if(success) {
					success(ret);
				}else{
					window.location.href = "/checkout/group_rev";
				}
			}
			else {
				alert(ret.m);
				fail?fail(ret):"";
			}
		}, "json");
	};
}
var group_rev = new Group_rev_Model();

/*秒杀购买操作*/
function Seckill_Model() {
	this.add = function(id, goods_id, num, success, fail){
		var _this = this;
		$.post("/cart/seckill_add", {id: id, goods_id: goods_id, num: num}, function(ret){
			if(ret.s == 10) {
				goto_login(function(){
					window.location.reload();
				});
			}
			else if(ret.s == 5) {
                $.simple_dialog.show({
                    message: ret.m,
                    messageStyle:{fontSize:'16px', color:'#f60', textAlign:'center'},
                    dialogStyle:{width:'380px'},
                    overlayer: 1,
                    button_ok: {
                    	name:'去订单列表',
                    	visiable:1,
                    	callback:function(){
                    		window.location.href = "/user/orderList";
                    	}
                    },
                    button_cancel: {visiable: 0}
                });
			}
			else if(ret.s == 1) {
				if(success) {
					success(ret);
				}else{
					window.location.href = "/checkout/seckill";
				}
			}
			else {
				alert(ret.m);
				fail?fail(ret):"";
			}
		}, "json");
	};
}
var seckill = new Seckill_Model();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值