shopcar.js 7.8 KB
/*获取*/
//$(document).ready(function(){			
new Vue({
	el: '#shopcar',
	data: {
		carlist: [],
		checktotalmoney:[],		
		cartnum:{
			num:0
		},
		//总价钱
		totalmoney:{
			money:0
		},
		checkgood:false,
		checklist:[],
        cartnum:0

	},
	created: function() {
		this.details();
		this.num();
	},

	methods: {
		//获取信息
		details: function() {
			var that = this;
			var url = '/Cart/index'
			var params = {
				unique_id: localStorage.getItem('unique_id')
			}
			ajaxsend(url, params, function(res) {
				/*console.log(JSON.stringify(res))*/
				console.log(res);
				var data=res.data;
				var carlist=data.cartList;
				that.carlist=carlist;
				console.log(that.carlist);
				for(obj of that.carlist){
					obj.checked=true;
				}
				that.checklist.length=that.carlist.length;
				console.log(that.checklist)
				//全选按钮
				that.checkgood=true;
				that.countPrice();

			})
		},
		addnum:function(obj,index){
		var that=this;
		var adnum="";
		console.log(obj);
		var cartid=obj.cart_id;
		adnum=parseInt(that.carlist[index].goods_num);
		that.carlist[index].goods_num++
		adnum+=1;
		var itemmoney=adnum*obj.goods_price;
		itemmoney=Math.floor(itemmoney * 100) / 100;		
		console.log(itemmoney);
		//更改购物车里的数量
		var url = '/Cart/editNum';
		var params = {
				cart_id:cartid,
				goods_num:adnum
			}
		ajaxsend(url, params, function(res) {
				/*console.log(JSON.stringify(res))*/
				console.log(res);
				if(res.status==true){
					//alert("更改成功");
                    //window.location.reload();
                    that.details();
                    that.num();
                    load();
				}else if(res.status==false){
                    unstock()
				}					
			})

	},		
	reducenum:function(obj,index){		
		var that=this;
		console.log(obj);
		var rednum="";
		var cartid=obj.cart_id;
		
		rednum=parseInt(that.carlist[index].goods_num);

        //剩一个商品的时候
        if(rednum==1){
            var that=this;
            var con=confirm("是否删除该商品");

            if(con==true){
                //var carid=cart_id;
                var url='/Cart/del';
                var params = {
                    cart_id:cartid
                }
                ajaxsend(url, params, function(res) {
                    /*console.log(JSON.stringify(res))*/
                    console.log(res);
                    if(res.status==true){
                        alert("删除成功");
                        window.location.reload();
                    }

                })
            }
        }else{
            that.carlist[index].goods_num--;
            rednum-=1;
            console.log(rednum)
            var itemmoney=rednum*obj.goods_price;
            itemmoney=Math.floor(itemmoney * 100) / 100;
            console.log(itemmoney);
            var url = '/Cart/editNum';
            var params = {
                cart_id:cartid,
                goods_num:rednum
            }
            ajaxsend(url, params, function(res) {
                /*console.log(JSON.stringify(res))*/
                console.log(res);
                if(res.status==true){
                    //alert("更改成功");
                   //window.location.reload();
                    that.details();
                    that.num();
                    load();
                }else if(res.status==false){
                    //alert("库存不足")
                    unstock();
                }
            })
        }
	},
		check:function(obj,index){	
			var that=this;
//			console.log(obj);
//			var checkprice=obj.goods_price;
//			var checknum=obj.goods_num;
//			var checkmoney=checkprice*checknum;
//			checkmoney=Math.floor(checkmoney * 100) / 100;
//			console.log(checkmoney);

			if(that.carlist[index].checked==false){
				that.carlist[index].checked=true;
				that.checklist.push(1);
				console.log(that.checklist)
			}else if(that.carlist[index].checked==true){
				that.carlist[index].checked=false;

				that.checklist.pop(1);
				console.log(that.checklist)

			}

			console.log(that.checklist);
            console.log(that.carlist);
            //var j=0;
            //for(var obj of that.carlist){
            //    //console.log(obj.goods_num)
            //    if(obj.goods_num>0){
            //        j++
            //    }
            //}
			if(that.checklist.length==that.carlist.length){
				that.checkgood=true;
			}else{
				that.checkgood=false;
			}
			that.countPrice();		
//			if(that.carlist[index].checked==true){
//				that.checktotalmoney.push(checkmoney);
//				
//			}else if(that.carlist[index].checked==false){
//				that.checktotalmoney.pop(checkmoney);
//				
//			}
			
//			console.log(that.checktotalmoney);
//			var s=0;
//			for(var i=0;i<that.checktotalmoney.length;i++){
//				s=s+that.checktotalmoney[i]
//			}
//			console.log(s);
//			that.totalmoney.money=s;
		},
		
		choseall:function(){
			var that=this;

			that.checklist.length=that.carlist.length;
			console.log(that.checklist.length)

			if(that.checkgood==false){
				that.checkgood=true;
				var cartList=that.carlist;
			for(obj of cartList){
				obj.checked=true;				
				}
			that.countPrice();
			}else if(that.checkgood==true){
				that.checkgood=false;
				var cartList=that.carlist;
				for(obj of cartList){
					obj.checked=false;
				}
				that.countPrice();
				that.checklist.length=0;
				//window.location.reload();
				
			}
		},
        //购物车数量
        num:function(){
            var that=this;
            var url='/CountNum/countCart';
            var params = {
                unique_id: localStorage.getItem('unique_id')

            }
            ajaxsend(url, params, function(res) {
                /*console.log(JSON.stringify(res))*/
                console.log(res);
                var data=res.data;
                console.log(data[0].count)
                that.cartnum=data[0].count;
                console.log(that.cartnum)

            })
        },
		//删除购物车
		delcart:function(cart_id){
			var that=this;
            var con=confirm("是否删除该商品");
            if(con==true){
                var carid=cart_id;
                var url='/Cart/del';
                var params = {
                    cart_id: carid
                }
                ajaxsend(url, params, function(res) {
                    /*console.log(JSON.stringify(res))*/
                    console.log(res);
                    if(res.status==true){
                        alert("删除成功");
                        window.location.reload();
                    }

                })
            }

		},
		//计算价钱的回调函数
		countPrice(){
			var that=this;
			let countPrice = 0
			let cartList = that.carlist;
			console.log(cartList);
			for(obj of cartList){
				console.log(obj)
				if(obj.checked==true){		
					var checkmoney=obj.goods_num*parseInt(obj.goods_price*1000);
					countPrice +=checkmoney/1000;					
					console.log(countPrice);
				}
			}			
			that.totalmoney.money=countPrice;
		},
		//结算
		accounts:function(){
            var that=this;
			console.log(that.checklist.length)
            if(that.checklist.length==0){
                alert("请选择商品");
                return false;
            }else{
                var cartList=that.carlist;
                var cartdid=[];
                console.log(cartList);
                for(obj of cartList){
                    console.log(obj);
                    if(obj.checked==true){
                        var cartid=obj.cart_id;
                        cartdid.push(cartid);
                        console.log(cartdid);
                        window.location.href="/index.php?g=&m=template&a=order&id="+cartdid;
                    }

                    //else if(that.checklist.length==0){
                    //	alert("请选择商品");
                    //   return false;
                    //}
                }
            }

		}
		
	}
})

//});