public ActionResult Shopcart(string code) { #region 用户信息部分 string userOpenId = string.Empty; Tuple <string, string> result = base.getUserOpenId(code); if (!string.IsNullOrEmpty(result.Item1)) { userOpenId = result.Item1; } else if (!string.IsNullOrEmpty(result.Item2)) { return(Redirect(result.Item2)); } ViewBag.userOpenId = userOpenId; #endregion List <UserShopcartsInfo> userShopcartsInfoList = null; if (!string.IsNullOrEmpty(userOpenId)) { userShopcartsInfoList = ShopCartBiz.CreateNew().getUserShopcartsInfo(userOpenId); } ViewBag.PageName = "购物车"; ViewBag.ProjectUrl = base.projectURL; return(View(userShopcartsInfoList)); }
public JsonResult AddShopcart(string prodId, int num, string prodPrice) { string res = string.Empty; string userOpenId = base.getUserOpenIdFromCookie(); if (string.IsNullOrEmpty(userOpenId)) { res = "false";//这里可以扩展,可以换成枚举,目前功能不需要 } else { try { ShopCartBiz shopcartBiz = ShopCartBiz.CreateNew(); shoppingcart userShopcart = shopcartBiz.checkProdExistInCarts(userOpenId, prodId); if (userShopcart != null) { int addNum = userShopcart.num + num; shopcartBiz.UpdateProdInCarts(userOpenId, prodId, addNum, prodPrice); } else { shopcartBiz.AddProdInCarts(userOpenId, prodId, num, prodPrice); } res = shopcartBiz.getUserCartsNum(userOpenId).ToString(); } catch (Exception e) { _Apilog.WriteLog("添加购物车异常: " + e.Message); res = "false"; } } return(Json(res)); }
// GET: Products public ActionResult Index(int id, string code) { #region 用户信息部分 string userOpenId = string.Empty; Tuple <string, string> result = base.getUserOpenId(code); if (!string.IsNullOrEmpty(result.Item1)) { userOpenId = result.Item1; } else if (!string.IsNullOrEmpty(result.Item2)) { return(Redirect(result.Item2)); } ViewBag.userOpenId = userOpenId; ViewBag.userShopcartNum = ShopCartBiz.CreateNew().getUserCartsNum(userOpenId).ToString();//获取用户购物车数量 #endregion #region 产品信息部分 products products = new products(); var resCache = CacheHelper.GetCache("product_" + id.ToString()); if (resCache != null) { products = (products)resCache; } else { products = ProductsBiz.CreateNew().getProductById(id); if (products != null) { var start = DateTime.Now; var expiredDate = start.AddDays(1); TimeSpan ts = expiredDate - start; CacheHelper.SetCache("product_" + id.ToString(), products, ts); } else { return(RedirectToAction("Category")); } } ViewBag.PageName = "商品详情"; ViewBag.ProjectUrl = base.projectURL; #endregion //微信分享 ViewBag.appId = base.wechatAppid; ViewBag.appSecret = base.wechatAppSecret; ViewBag.timestamp = DateTime.Now.Ticks.ToString().Substring(0, 10); ViewBag.nonceStr = Guid.NewGuid().ToString().Substring(0, 16); ViewBag.url = Request.Url.ToString(); string jsapi_ticket = WeiXinSDK.WeiXinJSSDK.GetJsapiTicket(ViewBag.appId, ViewBag.appSecret); ViewBag.signature = WeiXinSDK.WeiXinJSSDK.GetJsSdkSignature(ViewBag.nonceStr, jsapi_ticket, ViewBag.timestamp.ToString(), ViewBag.url); ViewBag.shareImg = string.Concat(base.projectURL, products.pic1).Replace("/img/", "/Thumbnail/"); return(View(products)); }
public JsonResult CreateOrderFromShopcart(string prodIds) { string userOpenId = base.getUserOpenIdFromCookie(); string res = "fail"; if (!string.IsNullOrEmpty(userOpenId)) { try { //订单付款页面用户回退购物车页面,然后再创单时实际没有商品,规避此类垃圾数据 string[] prods = prodIds.Split(','); bool prodExist = false; for (int i = 0; i < prods.Length; i++) { shoppingcart checkResult = ShopCartBiz.CreateNew().checkProdExistInCarts(userOpenId, prods[i]); if (checkResult != null) { prodExist = true; break; } } if (prodExist) { string salesNo = WxPayApi.GenerateOutTradeNo();// Guid.NewGuid().ToString(); OrderBiz.CreateNew().createOrderFromShopcart(prodIds, userOpenId, salesNo); res = salesNo; } else { res = "noProds"; } } catch (Exception e) { res = "fail"; _Apilog.WriteLog("ProductsController/CreateOrderFromShopcart 异常: " + e.Message); } } else { _Apilog.WriteLog("ProductsController/CreateOrderFromShopcart 用户userOpenId 为空: " + prodIds); } return(Json(res)); }
public JsonResult UpdateShopcartById(string num, string productId, string userOpenId) { string res = "fail"; if (string.IsNullOrEmpty(userOpenId)) { userOpenId = base.getUserOpenIdFromCookie(); } if (!string.IsNullOrEmpty(userOpenId)) { try { res = "success"; ShopCartBiz.CreateNew().updateUserShopcartsByProductId(userOpenId, productId, num); } catch (Exception e) { res = "fail"; _Apilog.WriteLog("ProductsController/updateUserShopcartsByProductId 异常: " + e.Message); } } return(Json(res)); }