public async Task <IActionResult> PutUser([FromRoute] string id, [FromBody] User user) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != user.OpenID) { return(BadRequest()); } _context.Entry(user).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!UserExists(id)) { return(NotFound()); } else { throw; } } return(NoContent()); }
public async Task <IActionResult> CreateOrder([FromBody] NewOrder order) { if (!ModelState.IsValid) { return(Ok(new { code = 1004, msg = "CreateOrder ModelState invalid " })); } IActionResult actionResult = NoContent(); try { var userid = Request.GetJwtSecurityToken()?.GetUserId(); if (string.IsNullOrEmpty(userid) || !await _context.User.AnyAsync(u => u.UserID == userid)) { actionResult = Ok(new { code = 1003, msg = $"用户[{userid}]未找到" }); } else { order.OrderID = Guid.NewGuid().ToString().Replace("-", ""); order.Create = DateTime.Now; order.UserID = userid; var seat = await _context.Seat.FirstOrDefaultAsync(s => s.SeatNumber == order.SeatNumber); order.SeatID = seat?.SeatId; order.BuyItems.ForEach(async a => { a.BuyItemID = Guid.NewGuid().ToString().Replace("-", ""); a.OrderID = order.OrderID; var goods = await _context.Goods.FindAsync(a.GoodsID); a.UnitPrice = goods.SellingPrice; a.Total = a.UnitPrice * a.Amount; a.GoodsName = goods.Name; a.Icon = goods.Icon; a.Image = goods.Image; _context.BuyItem.Add(a); order.TotalPrice += a.Total; }); order.Payable = order.TotalPrice;//这里减去平台红包 int sellerordercount = await _context.Order.CountAsync(o => o.SellerID == order.SellerID && o.Create.Date.Equals(DateTime.Now.Date)); order.OrderIndex = sellerordercount + 1; _context.Order.Add(order); int result = await _context.SaveChangesAsync(); if (result > 0) { actionResult = Ok(new { code = 0, msg = "OK", order }); } else { actionResult = Ok(new { code = 1001, msg = "订单未能保存" }); } } } catch (Exception ex) { actionResult = Ok(new { code = 1002, msg = ex.Message });; } return(actionResult); }
public async Task <IActionResult> Login([FromBody] LoginModel loginMode) { try { var jsonResult = SnsApi.JsCode2Json(WxOpenAppId, WxOpenAppSecret, loginMode.code); if (jsonResult.errcode == ReturnCode.请求成功) { //Session["WxOpenUser"] = jsonResult;//使用Session保存登陆信息(不推荐) //使用SessionContainer管理登录信息(推荐) if (!_context.User.Any(u => u.OpenID == jsonResult.openid)) { var userinfo = Senparc.Weixin.MP.AdvancedAPIs.UserApi.Info(Senparc.Weixin.MP.Containers.AccessTokenContainer.GetAccessToken(WxOpenAppId), jsonResult.openid); var adduser = _context.User.Add(new WeWaiter.Data.User() { UserID = Guid.NewGuid().ToString().Replace("-", ""), JoinIn = DateTime.Now, LastActive = DateTime.Now, OpenID = jsonResult.openid, NickName = userinfo.nickname, Sex = userinfo.sex, City = userinfo.city, Country = userinfo.country, Language = userinfo.language, Province = userinfo.province, Subscribe = userinfo.subscribe, SubscribeScene = userinfo.subscribe_scene, SubscribeTime = userinfo.subscribe_time, UnionId = userinfo.unionid, Remark = userinfo.remark }); await _context.SaveChangesAsync(); } var usr = _context.User.FirstOrDefault(u => u.OpenID == jsonResult.openid); if (usr != null) { //https://github.com/aspnet/Home/issues/2193 var token = usr.CreateJsonWebToken(_appSettings); var sessionBag = SessionContainer.UpdateSession(usr.UserID, jsonResult.openid, jsonResult.session_key, jsonResult.unionid); return(Ok(new { code = 0, msg = "OK", token, ImageHost = Utils.Server.ImageHost })); } else { return(Ok(new { code = 1007, msg = "未能正确获取到用户数据" })); } } else { return(Ok(new { code = 1006, msg = jsonResult.errmsg })); } } catch (Exception ex) { return(Ok(new { code = 1008, msg = ex.Message })); } }