/// <summary> /// 添加收货地址 /// </summary> /// <param name="vModel"></param> /// <returns></returns> public ActionResult ConfirmAddAddress(AddressVModel vModel) { if (ModelState.IsValid) { using (LsBuyEntities db = new LsBuyEntities()) { User user = db.Users.Where(t => t.UserName == User.Identity.Name).FirstOrDefault(); Address address = new Address() { Content = vModel.Content, Phone = vModel.Phone, Receiver = vModel.Reveiver, UserId = user.Id, CreatedTime = DateTime.Now, UpdatedTime = DateTime.Now }; db.Addresses.Add(address); db.SaveChanges(); } return(Redirect("Addresses")); } else { return(View("AddAddress", vModel)); } }
/// <summary> /// 注册页面 /// </summary> /// <returns></returns> public ActionResult Register(RegistVModel registVModel) { if (ModelState.IsValid) { using (LsBuyEntities db = new LsBuyEntities()) { if (db.Users.Where(t => t.Email == registVModel.Email).Count() > 0) { ModelState.AddModelError("Email", "邮箱已注册!"); return(View("Regist", registVModel)); } else { Db.User user = new Db.User { Email = registVModel.Email, Phone = registVModel.Phone, UserType = 0, Password = registVModel.Password, UserName = registVModel.Email, Sex = registVModel.Sex }; db.Users.Add(user); db.SaveChanges(); } return(Redirect("Index")); } } else { return(View("Regist", registVModel)); } }
/// <summary> /// 个人中心主页 /// </summary> /// <returns></returns> public ActionResult Index() { using (LsBuyEntities db = new LsBuyEntities()) { #region 用户信息 PersonalCenterVModel vm = new PersonalCenterVModel(); User user = db.Users.Where(t => t.UserName == User.Identity.Name).FirstOrDefault(); vm.User = new UserVModel { Sex = (int)user.Sex, Email = user.Email, Phone = user.Phone }; #endregion #region 订单列表 List <OrderVModel> orders = new List <OrderVModel>(); //根据用户Id获取订单列表 orders = db.Database.SqlQuery <OrderVModel>(@"select o.OrderNo,p.ImageUrl,ad.Receiver,ad.Content Address,o.OrderStatus,p.Price,p.ProductName from orders o left join OrderDetails d on o.OrderNo = d.OrderNo left join Addresses ad on ad.Id = o.AddressId left join CustomerProducts cp on cp.Id = d.ProductId left join Products p on cp.ProductId = p.Id where o.UserId = @userId", new SqlParameter("userId", user.Id)).ToList(); vm.Orders = orders; #endregion return(View(vm)); } }
/// <summary> /// 添加到购物车-首页添加 /// </summary> /// <param name="cvm"></param> /// <returns></returns> public ActionResult AddWithDefaultProduct(int productId) { try { using (LsBuyEntities db = new LsBuyEntities()) { string defaultColor = "red", defaultImage = "img1", defaultText = ""; //获取当前登录用户信息 User user = db.Users.Where(t => t.UserName == User.Identity.Name).FirstOrDefault(); CustomerProduct cutomerPro = db.CustomerProducts.Where(t => t.Color == defaultColor && t.Text == defaultText && t.Image == defaultImage && t.ProductId == productId).FirstOrDefault(); if (cutomerPro == null) { CustomerProduct customerPro = new CustomerProduct { Color = defaultColor, Image = defaultImage, Text = defaultText, ProductId = productId, }; //添加定制产品 db.CustomerProducts.Add(customerPro); db.SaveChanges(); //查询定制产品 cutomerPro = db.CustomerProducts.Where(t => t.Color == defaultColor && t.Text == defaultText && t.Image == defaultImage && t.ProductId == productId).FirstOrDefault(); } //加入购物车 ShoppingChart shoppingChart = new ShoppingChart { ProductId = cutomerPro.Id, UserId = user.Id, CreatedTime = DateTime.Now }; //加入购物车 db.ShoppingCharts.Add(shoppingChart); //保存到数据库 db.SaveChanges(); return(Redirect("AddSuccess")); } } catch (Exception ex) { throw ex; } }
/// <summary> /// 删除 /// </summary> /// <param name="id"></param> /// <returns></returns> public ActionResult Remove(int id) { using (LsBuyEntities db = new LsBuyEntities()) { var shopchart = db.ShoppingCharts.Where(t => t.Id == id).FirstOrDefault(); db.ShoppingCharts.Remove(shopchart); db.SaveChanges(); return(RedirectToAction("ShoppingCharts", "PersonalCenter")); } }
/// <summary> /// 删除收货地址 /// </summary> /// <param name="id">收货地址Id</param> /// <returns></returns> public ActionResult DeleteAddress(int id) { using (LsBuyEntities db = new LsBuyEntities()) { var address = db.Addresses.Where(t => t.Id == id).FirstOrDefault(); db.Addresses.Remove(address); db.SaveChanges(); } return(Redirect("Addresses")); }
/// <summary> /// 提交订单 /// </summary> /// <returns></returns> public ActionResult ConfirmOrder(int[] id, string AddressId) { if (id == null || id.Length < 1 || string.IsNullOrEmpty(AddressId)) { using (LsBuyEntities db = new LsBuyEntities()) { User user = db.Users.Where(t => t.UserName == User.Identity.Name).FirstOrDefault(); List <ShoppingChartVModel> spcharts = db.Database.SqlQuery <ShoppingChartVModel>(@"select t1.Id,t1.ProductId CustomerProductId,t2.Image, t2.Color,t2.Text,t2.ProductId, t3.Price,t3.ProductName,t3.ModelNumber,t3.ImageUrl from ShoppingCharts t1 left join CustomerProducts t2 on t1.ProductId= t2.Id left join Products t3 on t2.ProductId = t3.Id where t1.UserId = @userId", new SqlParameter("userId", user.Id)).ToList(); List <AddressVModel> addresses = db.Database.SqlQuery <AddressVModel>(@"select * from Addresses where UserId=@userId", new SqlParameter("userId", user.Id)).ToList(); ModelState.AddModelError("ValidMessage", "请选择收货地址或商品!"); return(View("ShoppingCharts", new ShoppingChartsVModel { Addresses = addresses, ShoppingCharts = spcharts })); } } using (LsBuyEntities db = new LsBuyEntities()) { User user = db.Users.Where(t => t.UserName == User.Identity.Name).FirstOrDefault(); #region 生成订单 string orderNo = DateTime.Now.ToString("yyyyMMddHHmmss") + user.Id.ToString().PadLeft(5, '0'); Order order = new Order { AddressId = AddressId, OrderStatus = 0, UserId = user.Id, CreatedTime = DateTime.Now, OrderNo = orderNo }; db.Orders.Add(order); //订单详情 db.ShoppingCharts.Where(t => id.Contains(t.Id)).ToList() .ForEach(t => { OrderDetail detail = new OrderDetail { OrderNo = orderNo, ProductId = t.ProductId.ToString() }; db.OrderDetails.Add(detail); }); //保存 db.SaveChanges(); #endregion return(RedirectToAction("ConfirmOrderSuccess")); } }
/// <summary> /// 修改收货地址 /// </summary> /// <returns></returns> public ActionResult ModifyAddress(int id) { using (LsBuyEntities db = new LsBuyEntities()) { Address ads = db.Addresses.Where(t => t.Id == id).FirstOrDefault(); AddressVModel vm = new AddressVModel { Id = ads.Id, Content = ads.Content, Phone = ads.Phone, Reveiver = ads.Receiver }; return(View(vm)); } }
/// <summary> /// 主页 /// </summary> /// <returns></returns> public ActionResult Index() { using (LsBuyEntities db = new LsBuyEntities()) { IndexVModel indexVm = new IndexVModel(); List <Product> products = db.Products.ToList(); products.ForEach(t => indexVm.ProductList.Add(new ProductVModel { Id = t.Id, ProductName = t.ProductName, ModelNumber = t.ModelNumber, ImageUrl = t.ImageUrl, Price = t.Price, Description = t.Description })); return(View(indexVm)); } }
/// <summary> /// 订单管理首页 /// </summary> /// <returns></returns> public ActionResult Index() { using (LsBuyEntities db = new LsBuyEntities()) { #region 订单列表 List <OrderVModel> orders = new List <OrderVModel>(); //根据用户Id获取订单列表 orders = db.Database.SqlQuery <OrderVModel>(@"select o.OrderNo,p.ImageUrl,ad.Receiver,ad.Content Address,o.OrderStatus,p.Price,p.ProductName from orders o left join OrderDetails d on o.OrderNo = d.OrderNo left join Addresses ad on ad.Id = o.AddressId left join CustomerProducts cp on cp.Id = d.ProductId left join Products p on cp.ProductId = p.Id ").ToList(); //vm.Orders = orders; #endregion return(View(orders)); } }
/// <summary> /// 购物车 /// </summary> /// <returns></returns> public ActionResult ShoppingCharts() { using (LsBuyEntities db = new LsBuyEntities()) { User user = db.Users.Where(t => t.UserName == User.Identity.Name).FirstOrDefault(); List <ShoppingChartVModel> spcharts = db.Database.SqlQuery <ShoppingChartVModel>(@"select t1.Id,t1.ProductId CustomerProductId,t2.Image, t2.Color,t2.Text,t2.ProductId, t3.Price,t3.ProductName,t3.ModelNumber,t3.ImageUrl from ShoppingCharts t1 left join CustomerProducts t2 on t1.ProductId= t2.Id left join Products t3 on t2.ProductId = t3.Id where t1.UserId = @userId", new SqlParameter("userId", user.Id)).ToList(); List <AddressVModel> addresses = db.Database.SqlQuery <AddressVModel>(@"select * from Addresses where UserId=@userId", new SqlParameter("userId", user.Id)).ToList(); return(View(new ShoppingChartsVModel { Addresses = addresses, ShoppingCharts = spcharts })); } }
/// <summary> /// 添加收货地址 /// </summary> /// <param name="vModel"></param> /// <returns></returns> public ActionResult ConfirmModifyAddress(AddressVModel vModel) { if (ModelState.IsValid) { using (LsBuyEntities db = new LsBuyEntities()) { Address address = db.Addresses.Where(t => t.Id == vModel.Id).FirstOrDefault(); address.Content = vModel.Content; address.Phone = vModel.Phone; address.Receiver = vModel.Reveiver; address.UpdatedTime = DateTime.Now; db.SaveChanges(); } return(Redirect("Addresses")); } else { return(View("ModifyAddress", vModel)); } }
/// <summary> /// 收货地址 /// </summary> /// <returns></returns> public ActionResult Addresses() { using (LsBuyEntities db = new LsBuyEntities()) { List <AddressVModel> adds = new List <AddressVModel>(); User user = db.Users.Where(t => t.UserName == User.Identity.Name).FirstOrDefault(); db.Addresses.Where(t => t.UserId == user.Id).ToList() .ForEach(t => { adds.Add(new AddressVModel { Id = t.Id, Content = t.Content, Reveiver = t.Receiver, Phone = t.Phone }); }); return(View(adds)); } }
/// <summary> /// 检查登录 /// </summary> /// <returns></returns> public ActionResult CheckLogin(LoginVModel loginVModel, string ReturnUrl) { using (LsBuyEntities db = new LsBuyEntities()) { if (!ModelState.IsValid) { return(View("Login", loginVModel)); } else { var user = db.Users.Where(t => t.UserName == loginVModel.UserName) .Where(t => t.Password == loginVModel.Password) .FirstOrDefault(); if (user == null) { ModelState.AddModelError("UserName", "用户名或密码错误"); return(View("Login", loginVModel)); } FormsAuthentication.SetAuthCookie(loginVModel.UserName, false); return(Redirect(ReturnUrl ?? "Index")); } } }
/// <summary> /// 定制页面主页 /// </summary> /// <returns></returns> public ActionResult Index(int pid) { using (LsBuyEntities db = new LsBuyEntities()) { CustomerMadeVModel vm = new CustomerMadeVModel(); Product product = db.Products.Where(t => t.Id == pid).FirstOrDefault(); vm.Product = new ProductVModel { Id = product.Id, Price = product.Price, ImageUrl = product.ImageUrl, Description = product.Description, ModelNumber = product.ModelNumber, ProductName = product.ProductName }; vm.CustomerProduct = new CustomerProductVModel { ProductId = product.Id, Color = "red", Image = "img1", Text = "", }; return(View(vm)); } }