using System; using System.Security.Cryptography; using System.IO; using System.Text; using System.Web; using System.Data; using System.Data.SqlClient; namespace Mtxfw.DAL { //公共函数 public class ShoppingCart1 { static user_Goods daoGoods = new user_Goods(); static order_info daoorder = new order_info(); public static int TotalNum(HttpContext context) { int intTotalNum = 0; if(context.Request.Cookies["0x120xxx1"]!=null) { HttpCookie myCookie = context.Request.Cookies["0x120xxx1"]; string strCookie=myCookie.Value; string[] strACookie; strACookie=strCookie.Split('|'); intTotalNum = 0; for(int i=0;i 0) { for (int i = 0; i < strACookie.Length - 1; i++) { //逐行赋值 string[] strA = strACookie[i].Split(','); if (strA.Length == 2) { dr = dt.NewRow(); //新建1行,注意和新建列的区别 if (strA[0] != "") { dr["id"] = strA[0]; //商品编号 //context.Response.Write(strA[0]); Mtxfw.Model.user_Goods ModelGoods = daoGoods.GetModel(int.Parse(strA[0])); if (ModelGoods != null) { dr["prodName"] = ModelGoods.GoodsName; //返回商品名称 dr["prodimg"] = ModelGoods.GoodsPic; //返回商品名称 dr["prodid"] = ModelGoods.Id; //返回商品名称 dr["SUserID"] = 0; //返回商品所有者 dr["TotalNum"] = 10000; //返回商品剩余数量 dr["prodPrice"] = ModelGoods.GoodsPrices;//返回商品折扣价格 dr["prodPrice2"] = ModelGoods.GoodsPrices2;//返回商品折扣价格 string[] strAuthor = "1|0|0|0".Split('|');; //返回商品运费 dr["IFMJCDYH"] = strAuthor[0]; dr["SPYH1"] = strAuthor[1]; dr["SPYH2"] = strAuthor[2]; dr["SPYH3"] = strAuthor[3]; } //dv.Table.Clear(); } dr["prodNum"] = 0; if (strA[1] != "")//商品数量 { dr["prodNum"] = Convert.ToInt32(strA[1]); } dt.Rows.Add(dr); //将该行加到表dt中 } } } } //返回函数值,注意返回的是DataView } return dt.DefaultView; } //该函数用来判断该商品是否已经在购物车中 public static bool IsExist(HttpContext context,string strProdId) { //从Cookie中获取购物车中的货物栏目信息 string strCookie = ""; bool b = false; if (context.Request.Cookies["0x120xxx1"] != null) { HttpCookie myCookie = context.Request.Cookies["0x120xxx1"]; strCookie = myCookie.Value; } string[] strACookie = strCookie.Split('|'); if (strACookie.LongLength > 0) { for (int i = 0; i < strACookie.Length - 1; i++) { string[] strA = strACookie[i].Split(','); if (strA.Length == 2) { if (strA[0] != "") { if (strA[0] == strProdId) { b = true; //表示该商品已经存在 } } } } } return b; } //向购物车中添加一商品,如果该商品已经存在,只要修改数量即可,如果不存在,则需要添加一项 public static void Addprod(HttpContext context, string strProdId, int num) { //从Cookie中获取购物车中的货物栏目信息 string[] AProdId = strProdId.Split(','); for (int j = 0; j < AProdId.Length; j++) { string strCookie = ""; if (AProdId.Length > 0) { if (AProdId[j] != "") { HttpCookie myCookie = new HttpCookie("0x120xxx1"); if (IsExist(context, AProdId[j])) { if (context.Request.Cookies["0x120xxx1"] != null) { myCookie = context.Request.Cookies["0x120xxx1"]; strCookie = myCookie.Value; string[] strACookie; strACookie = strCookie.Split('|'); //下面根据HashTable中的商品栏目信息添加每一行 for (int i = 0; i < strACookie.Length - 1; i++) { string[] strA = strACookie[i].Split(','); if (strA.Length == 2) { if (strA[0] != "") { if (strA[0] == AProdId[j]) { strCookie = strCookie.Replace(strA[0] + "," + strA[1], strA[0] + "," + (Convert.ToInt32(strA[1]) + num).ToString()); } } } } } } else { if (context.Request.Cookies["0x120xxx1"] != null) { myCookie = context.Request.Cookies["0x120xxx1"]; strCookie = myCookie.Value; } strCookie += AProdId[j] + ",1|"; } //最后将新的购物栏目信息保存到Cookie中 myCookie.Value = strCookie; myCookie.Expires = DateTime.Now.AddYears(1); context.Response.Cookies.Add(myCookie); } } } } //更改购物车中的货物数量 public static void UpdateProd(HttpContext context, string strProdId, string intprodNum) { HttpCookie myCookie = new HttpCookie("0x120xxx1"); //从Cookie中获取购物车中的货物栏目信息 string[] AProdId = strProdId.Split(','); for (int j = 0; j < AProdId.Length; j++) { string strCookie = ""; if (AProdId.Length > 0) { if (AProdId[j] != "") { if (context.Request.Cookies["0x120xxx1"] != null) { myCookie = context.Request.Cookies["0x120xxx1"]; strCookie = myCookie.Value; string[] strACookie; strACookie = strCookie.Split('|'); for (int i = 0; i < strACookie.Length - 1; i++) { //下面更改数量 string[] strA = strACookie[i].Split(','); if (strA.Length == 2) { if (strA[0] != "") { if (strA[0] == AProdId[j]) { if (intprodNum == "" || intprodNum == "0") { strCookie = strCookie.Replace(strACookie[i] + "|", ""); } else { strCookie = strCookie.Replace(strA[0] + "," + strA[1] + "|", strA[0] + "," + intprodNum + "|"); } } } } } } //最后将新的购物栏目信息保存到Cookie中 myCookie.Value = strCookie; myCookie.Expires = DateTime.Now.AddYears(1); context.Response.Cookies.Add(myCookie); } } } } } }