前后端分离 ---购物车

购物车不仅仅可以存在服务端的rredis中和MySQL中

也可以存在前端的cookie中

 

//购物车

    var Cart = function () {

        this.Count = 0;

        this.Total = 0;

        this.Items = new Array();

    };

    //购物车集合对象

    var CartItem = function () {

        this.Id = 0;

        this.Name = "";

        this.Count = 0;

        this.Price = 0;

    };

 

    //购物车操作

    var CartHelper = function () {

        this.cookieName = "{$member_id}_repast_cart";

        this.Clear = function () {

            var cart = new Cart();

            this.Save(cart);

            return cart;

        };

 

        //向购物车添加

        this.Add = function (id, name, count, price) {

            var cart = this.Read();

            var index = this.Find(id);

 

            if(count==0){

                this.Del(id);

 

            }else{

                //如果ID已存在,覆盖数量

                if (index > -1) {

                    cart.Total -= (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000);

                    cart.Items[index].Count = count;

                    cart.Total += (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000);

 

                } else {

                    var item = new CartItem();

                    item.Id = id;

                    item.Name = name;

                    item.Count = count;

                    item.Price = price;

                    cart.Items.push(item);

                    cart.Count++;

                    cart.Total += (((item.Count * 100) * (item.Price * 100)) / 10000);

                    // console.log(cart);

                    // cart.Total += (((cart.Items[index].Count * 100) * (cart.Items[index].Price * 100)) / 10000);

                }

                cart.Total=Math.round(cart.Total * 100) / 100;

                this.Save(cart);

            }

 

            return cart;

        };

        //改变数量

        this.Change = function (id, count) {

            var cart = this.Read();

            var index = this.Find(id);

            cart.Items[index].Count = count;

            this.Save(cart);

            return cart;

        };

        //移出购物车

        this.Del = function (id) {

            var cart = this.Read();

            var index = this.Find(id);

            if (index > -1) {

                var item = cart.Items[index];

                cart.Count--;

                cart.Total = cart.Total - (((item.Count * 100) * (item.Price * 100)) / 10000);

                cart.Items.splice(index, 1);

                this.Save(cart);

            }

 

            return cart;

 

        };

        //根据ID查找

        this.Find = function (id) {

            var cart = this.Read();

            var index = -1;

            for (var i = 0; i < cart.Items.length; i++) {

                if (cart.Items[i].Id == id) {

                    index = i;

                }

            }

            return index;

        };

        //COOKIE操作

        this.Save = function (cart) {

            var source = "";

            for (var i = 0; i < cart.Items.length; i++) {

                if (source != "") { source += "|$|"; }

                source += this.ItemToString(cart.Items[i]);

            }

            $.cookie(this.cookieName, source);

        };

        this.Read = function () {

            //读取COOKIE中的集合

            var source = $.cookie(this.cookieName);

            var cart = new Cart();

            if (source == null || source == "") {

                return cart;

            }

            var arr = source.split("|$|");

            cart.Count = arr.length;

            for (var i = 0; i < arr.length; i++) {

                var item = this.ItemToObject(arr[i]);

                cart.Items.push(item);

                cart.Total += (((item.Count * 100) * (item.Price * 100)) / 10000);

            }

            return cart;

        };

        this.ItemToString = function (item) {

            return item.Id + "||" + escape(item.Name) + "||" + item.Count + "||" + item.Price;

        };

        this.ItemToObject = function (str) {

            var arr = str.split('||');

            var item = new CartItem();

            item.Id = arr[0];

            item.Name = unescape(arr[1]);

            item.Count = arr[2];

            item.Price = arr[3];

            return item;

        };

    };

    //调用

    var xc=new CartHelper();

    //id,名称,数量,价格    数量为0的话直接删除

    xc.Add(id,c_name,num,price);

 

这个方法会用的话非常简单~·

转载于:https://www.cnblogs.com/weifeng-888/p/10686433.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值