/// <summary> /// 根据用户名和密码,登入验证,并且拿cookie的值 /// </summary> /// <param name="loginId">登录名</param> /// <param name="loginPwd">登入密码</param> /// <param name="cookie">cookie</param> /// <param name="msg">返回信息</param> public static void CheckUserLoginPwdAndGetCartCookie(string loginId, string loginPwd, HttpCookie cookie, out string msg) { string rememberMe = HttpContext.Current.Request["RememberMe"] ?? "false"; BLL.UsersBll bll = new BLL.UsersBll(); Model.Users user = new MyBookShop.BLL.UsersBll().GetModelByLoginId(loginId); msg = string.Empty; if (user != null) { msg = Common.CommonTools.GetMd5String(Common.CommonTools.GetMd5String(loginPwd)).Equals(user.LoginPwd) ? "success" : "failed"; } if (msg == "success") { if (rememberMe != "false")//记住我选项 { HttpCookie cookie1 = new HttpCookie("loginId", loginId); cookie1.Value = loginId; cookie1.Expires = DateTime.Now.AddDays(10); HttpContext.Current.Response.Cookies.Add(cookie1); } else { if (cookie != null) { cookie.Expires = DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookie); } } //Model.Users user = new BLL.UsersBll().GetModelByLoginId(loginId); HttpContext.Current.Session["userInfo"] = user; //同时检测用户cookie中有没有购物车信息,有就将数据写入数据库 HttpCookie cookieCart = HttpContext.Current.Request.Cookies["ShoppingCart"]; if (cookieCart != null) { int userId = bll.GetModelByLoginId(loginId).Id; foreach (string item in cookieCart.Values) { string itemValue = cookieCart[item].ToString(); string[] cartInfo = itemValue.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); BLL.CartBll cartBll = new BLL.CartBll(); Model.Cart cart = cartBll.GetModel(Convert.ToInt32(cartInfo[0]), user.Id); if (cart == null) { cart = new Model.Cart(); cart.Book.Id = Convert.ToInt32(cartInfo[0]); cart.Count = Convert.ToInt32(cartInfo[1]); cart.User.Id = userId; cartBll.Add2(cart);//调用新的增加方法。对象属性变了 } else { cart.Count += Convert.ToInt32(cartInfo[1]); cartBll.Update2(cart); } } //清空cookie cookieCart.Expires = System.DateTime.Now.AddDays(-1); HttpContext.Current.Response.Cookies.Add(cookieCart); } } }
//添加进购物车 private string AddBookIntoCart(int bookId) { //判断用户是否登入 Model.Users user = Common.CommonTools.CheckIsLoginOrNot(); if (user != null)//登入状态下将数据保存到数据库 { BLL.CartBll bll = new BLL.CartBll(); Model.Cart cart = bll.GetModel(bookId, user.Id); if (cart != null)//先检测购物车中是否存在相同的商品 { cart.Count++;//存在则自增1 bll.Update2(cart);//更新 return "已永久更新你的购物信息"; } else//不存在同样的商品则新增 { cart = new Model.Cart();//为空则创建 cart.Book.Id = bookId; cart.Count = 1; cart.User.Id = user.Id; bll.Add2(cart);//新增 return "已永久新增你的购物信息"; } } else//未登入状态下,保存到cookie { //先判断cookie是否有值 HttpCookie cookie = HttpContext.Current.Request.Cookies["ShoppingCart"]; if (cookie == null) { cookie = new HttpCookie("ShoppingCart"); cookie.Values.Add(bookId.ToString(), bookId + "|" + 1); cookie.Expires = System.DateTime.Now.AddDays(2); HttpContext.Current.Response.Cookies.Add(cookie); return "已临时保存你的购物信息";//,<a href='javascript:LoginClick();'>永久保存?</a> } else { bool flag = true; foreach (string item in cookie.Values) { string itemValue = cookie.Values[item]; if (string.IsNullOrEmpty(itemValue)) { break; } string[] itemValues = itemValue.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); if (itemValues[0] == bookId.ToString())//如果cookie中存在,则数量增加 { int num = Convert.ToInt32(itemValues[1]) + 1; cookie.Values.Remove(bookId.ToString()); cookie.Values.Add(bookId.ToString(), bookId + "|" + num); cookie.Expires = System.DateTime.Now.AddDays(2); HttpContext.Current.Response.Cookies.Add(cookie); flag = false; break; } } if (flag)//遍历之后都不存在相同产品,则新增一条 { cookie.Values.Add(bookId.ToString(), bookId + "|" + 1); cookie.Expires = System.DateTime.Now.AddDays(2); HttpContext.Current.Response.Cookies.Add(cookie); } return "已临时更新你的购物信息";//,<a href='javascript:LoginClick();'>永久保存?</a> } } }