public async Task <ActionResult> Create([Bind(Include = "Id,openid,nickname,sex,province,city,country,headimgurl,unionid,phone")] WeiXinUser weiXinUser) { if (ModelState.IsValid) { db.WeiXinUsers.Add(weiXinUser); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(weiXinUser)); }
public async Task <ActionResult> Create([Bind(Include = "Id,LotteryId,QiHao,OpenNumber,OpenTime,CreateTime,UpdateTime,Source,Forecast1,Forecast2,Forecast3,Used,CurrentForecastNumber,ForecastFrom,UsedBy")] LotteryOpenHistory lotteryOpenHistory) { if (ModelState.IsValid) { lotteryOpenHistory.Id = Guid.NewGuid(); db.LotteryOpenHistories.Add(lotteryOpenHistory); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } return(View(lotteryOpenHistory)); }
public async Task <ActionResult> GenerateQrCodeUrl(bool newAuthCode = false) { AuthCode authCode = null; if (!newAuthCode) { authCode = await db.AuthCodes.Where(w => w.IsExpired == false).OrderByDescending(w => w.Id).FirstOrDefaultAsync(); if (authCode != null) { return(Json(new { errno = 0, QrCodeUrl = authCode.QrCodeUrl, msg = "" }, JsonRequestBehavior.AllowGet)); } } var olds = await db.AuthCodes.Where(w => w.IsExpired == false).ToListAsync(); foreach (var item in olds) { item.IsExpired = true; } await db.SaveChangesAsync(); authCode = new AuthCode { Code = Guid.NewGuid().ToString("N"), CreateTime = DateTime.Now, IsExpired = false, QrCodeUrl = null, }; QRCodeGenerator qrGenerator = new QRCodeGenerator(); var plainText = WebConfigs.WebSiteBaseUrl + "/Login/Index?authCode=" + authCode.Code; QRCodeData qrCodeData = qrGenerator.CreateQrCode(plainText, QRCodeGenerator.ECCLevel.Q); QRCode qrCode = new QRCode(qrCodeData); Bitmap qrCodeImage = qrCode.GetGraphic(20); var baseDir = AppDomain.CurrentDomain.BaseDirectory; var dir = "/Content/QRCodes/" + DateTime.Now.ToString("yyyyMMdd"); if (!Directory.Exists(baseDir + dir)) { Directory.CreateDirectory(baseDir + dir); } var fileName = "/" + authCode.Code + ".jpg"; qrCodeImage.Save(baseDir + dir + fileName, ImageFormat.Jpeg); authCode.QrCodeUrl = dir + fileName; db.AuthCodes.Add(authCode); await db.SaveChangesAsync(); return(Json(new { errno = 0, QrCodeUrl = authCode.QrCodeUrl, msg = "" }, JsonRequestBehavior.AllowGet)); }
public async Task <ActionResult> Register(RegisterModel model) { if (string.IsNullOrEmpty(model.AuthCode)) { return(Json("请从官方渠道进入", JsonRequestBehavior.AllowGet)); } //SuperAuthCode var superAuthCode = ConfigurationManager.AppSettings["SuperAuthCode"].ToString(); if (superAuthCode.ToLower() != model.AuthCode.ToLower()) { using (var db = new OlaEntities()) { var ac = db.AuthCodes.FirstOrDefaultAsync(w => w.Code == model.AuthCode); if (ac == null) { return(Json("请从官方渠道进入,没有邀请码无法注册", JsonRequestBehavior.AllowGet)); } } } // 验证短信验证码 var key = SMS_Prefix + model.Mobile; if (Session[key] == null) { return(Json("验证码已过期", JsonRequestBehavior.AllowGet)); } if (Session[key].ToString() != model.Vcode) { return(Json("验证码错误", JsonRequestBehavior.AllowGet)); } using (var db = new OlaEntities()) { var wxUser = await db.WeiXinUsers.FirstOrDefaultAsync(w => w.openid == model.OpenId); if (wxUser == null) { return(Json("账户信息不存在,请重新进入", JsonRequestBehavior.AllowGet)); } wxUser.phone = model.Mobile; await db.SaveChangesAsync(); WriteCookie(wxUser.Id); } return(Json("success", JsonRequestBehavior.AllowGet)); }
public async Task <ActionResult> BaseCallback(string code, string state, string authCode, string returnUrl) { //log.Info("code:" + code + " state:" + state + " authCode:" + authCode + " returnUrl:" + returnUrl); if (string.IsNullOrEmpty(code)) { ViewBag.Info = "您拒绝了授权!"; return(View("Info")); } OAuthAccessTokenResult result = null; //通过,用code换取access_token try { result = OAuthApi.GetAccessToken(WeiXinConfig.appId, WeiXinConfig.appSecret, code); if (result.errcode != ReturnCode.请求成功) { ViewBag.Info = "错误:" + result.errmsg; return(View("Info")); } //下面2个数据也可以自己封装成一个类,储存在数据库中(建议结合缓存) //如果可以确保安全,可以将access_token存入用户的cookie中,每一个人的access_token是不一样的 //Session["OAuthAccessTokenStartTime"] = DateTime.Now; //Session["OAuthAccessToken"] = result; //因为第一步选择的是OAuthScope.snsapi_userinfo,这里可以进一步获取用户详细信息 using (var db = new OlaEntities()) { var wxUser = await db.WeiXinUsers.FirstOrDefaultAsync(w => w.openid == result.openid); if (wxUser == null) { //log.Info(JsonConvert.SerializeObject(result)); OAuthUserInfo userInfo = OAuthApi.GetUserInfo(result.access_token, result.openid); //log.Info(JsonConvert.SerializeObject(userInfo)); if (string.IsNullOrEmpty(authCode)) { ViewBag.Info = "验证失败!请从正规途径进入!" + result.errmsg; return(View("Info")); } wxUser = new WeiXinUser { city = userInfo.city, country = userInfo.country, headimgurl = userInfo.headimgurl, nickname = userInfo.nickname, openid = userInfo.openid, province = userInfo.province, sex = userInfo.sex, unionid = userInfo.unionid, CreateTime = DateTime.Now, phone = null, AuthCode = authCode, state = (int)WeiXinUserStateEnum.Normal, }; db.WeiXinUsers.Add(wxUser); await db.SaveChangesAsync(); } if (string.IsNullOrEmpty(wxUser.phone)) { if (!string.IsNullOrEmpty(authCode)) { return(RedirectToAction("Register", new { authCode, wxUser.openid })); } else { ViewBag.Info = "验证失败!请从正规途径进入!"; return(View("Info")); } } if (wxUser.state != (int)WeiXinUserStateEnum.Normal) { ViewBag.Info = "您已经被限制访问该网站,请联系网站负责人!"; return(View("Info")); } WriteCookie(wxUser.Id); } //log.Info("login:"******"Index", "L")); } catch (Exception ex) { log.Error(ex.Message, ex); ViewBag.Info = ex.Message; return(View("Info")); } }