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);
}
}
}
}
这个类写得有点繁琐,有什么好的见解,请评论 我是菜鸟一个