.Net用Cookie做购物车

 

public class ShoppingCart
    {
       //保存购物车ID和数量到Cookie中

//保存ID的格式:"1,32,43

//下面是对应数量:"2,3,2"

如ID=1,对应的数量就是2
       public void SaveCookieCart(int productId, int amount)
       {
        
           if (HttpContext.Current.Request["ShoppingCart"]== null)
           {
               HttpCookie cookie = new HttpCookie("ShoppingCart");
               DateTime dt = DateTime.Now;
               TimeSpan ts = new TimeSpan(7, 0, 0, 0, 0);

               cookie.Values.Add("ProductId",productId.ToString());
               cookie.Values.Add("Amount", amount.ToString());
               HttpContext.Current.Response.AppendCookie(cookie); cookie.Expires = dt.Add(ts);
           }
           else
           {
               //用逗号隔开
               HttpCookie cookie = HttpContext.Current.Request.Cookies["ShoppingCart"];
               string[] ProductIdArray = cookie["ProductId"].ToString().Split(',');
               string[] Amount = HttpContext.Current.Request.Cookies["ShoppingCart"]["Amount"].ToString().Split(',');
               if (!ProductIdArray.Contains(productId.ToString()))
               {
                   cookie.Values["ProductId"] = HttpContext.Current.Request.Cookies["ShoppingCart"]["ProductId"] + "," + productId;
                   cookie.Values["Amount"] = HttpContext.Current.Request.Cookies["ShoppingCart"]["Amount"] + "," + amount;
                }  
                 HttpContext.Current.Response.AppendCookie(cookie);
                   DateTime dt = DateTime.Now;
                   TimeSpan ts = new TimeSpan(7, 0, 0, 0, 0);
                   cookie.Expires = dt.Add(ts);
             
           }
       }
       //购物车数据源,也可以作为订单数据源
       public IList<Model.ShoppingCartProduct> GetIListShoppingCartFromCookie()
       {
           HttpCookie cookie = HttpContext.Current.Request.Cookies["ShoppingCart"];
           if (cookie != null)
           {
               string[] ProductIdArray = cookie["ProductId"].ToString().Split(',');
               string[] AmountArray = cookie["Amount"].ToString().Split(',');
               int productId;
               int amount;
               IList<Model.ShoppingCartProduct> iList = new List<Model.ShoppingCartProduct>();
               for (int i = ProductIdArray.Length-1; i >= 0; i--)
               {
                   productId = Convert.ToInt32(ProductIdArray[i]);
                   amount = Convert.ToInt32(AmountArray[i]);
                   Model.ShoppingCartProduct shoppingCart = new SqlDAL.ShoppingCart().GetShoppingCartItem(productId,amount);
                   iList.Add(shoppingCart);           
               }
               return iList;
           }
           else{
               return null;          
           }
       }
       //删除购物车中的某一项
       public void DeleteShoppingCartCookieItem(int productId)
       {
           HttpCookie cookie = HttpContext.Current.Request.Cookies["ShoppingCart"];
         
           if (cookie != null)
           {
               string[] ProductIdArray = cookie["ProductId"].ToString().Split(',');
               string[] AmountArray = cookie["Amount"].ToString().Split(',');
                             StringBuilder productIdCookie=new StringBuilder();
               StringBuilder amountCookie = new StringBuilder();
               for (int i =0; i<ProductIdArray.Length; i++)
               {
                   string productIdItem=ProductIdArray[i].Trim();
                   string amountItem=AmountArray[i].Trim();
                   //如果不相等就保存
                   if(productId.ToString()!=productIdItem){  
                      
                 
                   productIdCookie.Append(productIdItem+",");//追加完成
                   amountCookie.Append(amountItem+",");//追加完成
                   }
               }
                 //更新删除Id后的COOKIE
                   cookie.Values["ProductId"] = productIdCookie.ToString().Substring(0, productIdCookie.Length-1);
                   cookie.Values["Amount"] =amountCookie.ToString().Substring(0,amountCookie.Length-1);
                   HttpContext.Current.Response.AppendCookie(cookie);
                   DateTime dt = DateTime.Now;
                   TimeSpan ts = new TimeSpan(7, 0, 0, 0, 0);
                   cookie.Expires = dt.Add(ts);

           }
       }
       //清空购物车
       public void ClearCookie()
       {

           if (HttpContext.Current.Request["ShoppingCart"] != null)
           {
               HttpCookie cookie = new HttpCookie("ShoppingCart");
               DateTime dt = DateTime.Now;
               TimeSpan ts = new TimeSpan(0, 0, 0, 0, 0);
               cookie.Values.Add("ProductId", "");
               cookie.Values.Add("Amount", "");
               HttpContext.Current.Response.AppendCookie(cookie); cookie.Expires = dt.Add(ts);
           }

       }
       //修改购物车某商品的数量
       public void ModifyAmount(int productId,int amount)
       {
           HttpCookie cookie = HttpContext.Current.Request.Cookies["ShoppingCart"];
           if (cookie != null)
           {
               string[] ProductIdArray = cookie["ProductId"].ToString().Split(',');
               string[] AmountArray = cookie["Amount"].ToString().Split(',');
             
               StringBuilder productIdCookie = new StringBuilder();
               StringBuilder amountCookie = new StringBuilder();
               for (int i = 0; i < ProductIdArray.Length; i++)
               {
                 
                   //如果不相等就保存
                   if (productId.ToString()==ProductIdArray[i].ToString())
                   {


                       productIdCookie.Append(ProductIdArray[i]+ ",");
                       amountCookie.Append(amount + ",");//修改操作
                   }
                   else
                   {
                       productIdCookie.Append(ProductIdArray[i] + ",");//追加完成
                       amountCookie.Append(AmountArray[i] + ",");//追加完成
                   }
               }
               //更新删除Id后的COOKIE
               cookie.Values["ProductId"] = productIdCookie.ToString().Substring(0, productIdCookie.Length - 1);
               cookie.Values["Amount"] = amountCookie.ToString().Substring(0, amountCookie.Length - 1);//删除最后的逗号
               HttpContext.Current.Response.AppendCookie(cookie);
               DateTime dt = DateTime.Now;
               TimeSpan ts = new TimeSpan(7, 0, 0, 0, 0);
               cookie.Expires = dt.Add(ts);
         
           }
          
       }


    }
}
这个类写得有点繁琐,有什么好的见解,请评论 我是菜鸟一个

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值