public void StartWork() { //time.Marke("1.启动锚点"); int lastExportID = 0; string actStr = _redisHelper.StringGet(SUCCESSACTIDQUEUEKEY); if (!string.IsNullOrEmpty(actStr)) { lastExportID = Convert.ToInt32(actStr); } //time.Marke("2.redis读取完毕"); IEnumerable <int> actIdList = _signPointActivityRepository.Query <int>("select distinct id from SignPointActivity where id>@rid and id<=@maxID and IsDelete=@isDelate", new { rid = lastExportID, isDelate = 0, maxID = 1400 }); //time.Marke("3.redis 执行 select distinct id from SignPointActivity where id>@rid 查询完毕"); //time.Marke("4.执行工作任务"); //执行任务 ExcuteWorker(actIdList); //time.Marke("5.工作任务执行完毕"); Console.WriteLine("运行完毕, 请清理活动表(SignActivity)下的RowId 。。。"); time.SaveData(); }
public ActionResult ActiveEmail(string t, string u) { string msg = ""; if (t.IsNullOrEmpty() || u.IsNullOrEmpty()) { msg = "参数为空,你是要搞事情啊"; } else { string emailByToken = _iredisHelper.StringGet(t); int? userId = _iredisHelper.StringGet <int?>(u); if (emailByToken.IsNullOrEmpty() || userId == null) { msg = "你这token都已经过期了,我可是给了你1个小时的时间。。。再去拿token吧。"; } else { _appUserRepository.Update(_appUserRepository.Queryable(r => r.Email == emailByToken && r.Id == userId), r => new AppUser { Email = emailByToken, EmailIsValid = true }); msg = "您已成功验证该邮箱,你可以用这个邮箱登录系统了!"; _iredisHelper.KeyDeleteAsync(t); _iredisHelper.KeyDeleteAsync(u); } } ViewBag.Msg = msg; return(View()); }
public JsonResult TestRedis() { var guid = Guid.NewGuid().ToString(); _redisHelper.StringSet("guid:" + guid, guid, 1000); _redisHelper.StringGet("guid:" + guid); _redisHelper.KeyDelete("guid:" + guid); return(Json(guid)); }
public AjaxResult SmsCode(string email) { AjaxResult amm; int limitcount = 10; int limitMinutes = 10; if (!Validate.IsEmail(email)) { return(AjaxResult.Error("邮箱格式不正确")); } AppUser userEntity = this.Queryable(u => u.Email == email && u.DeleteMark == false).FirstOrDefault(); if (userEntity == null) { amm = AjaxResult.Error("此邮箱尚未注册!"); } else { string count = _iRedisHelper.StringGet <string>(email); //缓存十分钟,如果缓存中存在,且请求次数超过10次,则返回 if (!string.IsNullOrEmpty(count) && Convert.ToInt32(count) >= limitcount) { amm = AjaxResult.Error("没收到邮箱:请联系[email protected]"); } else { #region 发送邮箱,并写入缓存,更新登录信息表的token,date,code int num = 0; if (!string.IsNullOrEmpty(count)) { num = Convert.ToInt32(count); } string countplus1 = num + 1 + ""; _iRedisHelper.StringSet <string>(email, countplus1, new TimeSpan(0, limitMinutes, 0)); ResetPwd resetpwdEntry = new ResetPwd { UserId = (int)userEntity.Id, ModifyPwdToken = Utils.GuId(), PwdCodeTme = DateTime.Now, ModfiyPwdCode = Utils.RndNum(6), LoginName = userEntity.LoginName, NickName = userEntity.NickName }; //将发送验证码的数据存入redis缓存中 _iRedisHelper.StringSet(email + "sendcodekey", resetpwdEntry, new TimeSpan(0, limitMinutes, 0)); //发送找回密码的邮件 string body = UiHelper.FormatEmail(resetpwdEntry, "PwdReSetTemplate"); _imailHelper.SendByThread(email, "[、天上有木月博客] 密码找回", body); #endregion //将修改密码的token返回给前端 amm = AjaxResult.Info("验证码已发送至你的邮箱!", resetpwdEntry.ModifyPwdToken, ResultType.Success.ToString()); } } return(amm); }
/// <summary> /// 根据邮箱发送四位验证码,并将token返回给前端 /// </summary> /// <param name="email"></param> /// <returns></returns> public ActionResult GetActiveCode(string email) { if (email.IsNullOrEmpty()) { return(Error("Email不能为空!!!")); } if (!Validate.IsEmail(email)) { return(Error("Email格式不正确!!!")); } int id = Op.CurrentUser.UserId; //bool EmailIsValid = _appUserRepository.IQueryable(u => u.Id == Id).Select(r => r.EmailIsValid).FirstOrDefault(); //if (EmailIsValid) //{ // return Error("您已经绑定成功了,请不要重复绑定!!!"); //} //为了安全,一个用户IP10分钟内只能请求此接口3次 string ip = Net.Ip; int count = _redisHelper.StringGet <int>(ip); if (count >= 3) { return(Error("请求过于频繁,请稍后再试!")); } count += 1; _redisHelper.StringSet(ip, count, new TimeSpan(0, 10, 0)); var amm = _appUserRepository.IsRepeat(new AppUser { Id = id, LoginName = Utils.GuId(), Email = email }); if (amm.State.Equals(ResultType.Error.ToString())) { return(Error("该邮箱已被绑定其他用户绑定!")); } //缓存1小时 TimeSpan saveTime = new TimeSpan(1, 0, 0); //生成token string token = Utils.GuId(); string rand4Num = Utils.RndNum(4); //redis缓存绑定邮箱随机token作为键,email作为值,随机u为键,当前登录id为值 _redisHelper.StringSet(token, email, saveTime); _redisHelper.StringSet(token + email, rand4Num, saveTime); EmailViewModel emailViewModel = new EmailViewModel { ToUserName = email, Code = rand4Num }; string body = UiHelper.FormatEmail(emailViewModel, "SendCodeTemplate"); _imailHelper.SendByThread(email, "[、天上有木月博客] 邮箱激活通知", body); return(Success("邮箱发送成功,请查收", token)); }
//Intercept方法是拦截的关键所在,也是IInterceptor接口中的唯一定义 public void Intercept(IInvocation invocation) { var method = invocation.MethodInvocationTarget ?? invocation.Method; //对当前方法的特性验证 var qCachingAttribute = method.GetCustomAttributes(true).FirstOrDefault(x => x.GetType() == typeof(CachingAttribute)) as CachingAttribute; if (qCachingAttribute != null) { //获取自定义缓存键 var cacheKey = CustomCacheKey(invocation); //注意是 string 类型,方法GetValue var cacheValue = _cache.StringGet(cacheKey); if (cacheValue != null) { //将当前获取到的缓存值,赋值给当前执行方法 var type = invocation.Method.ReturnType; var resultTypes = type.GenericTypeArguments; if (type.FullName == "System.Void") { return; } object response; if (typeof(Task).IsAssignableFrom(type)) { //返回Task<T> if (resultTypes.Any()) { var resultType = resultTypes.FirstOrDefault(); // 核心1,直接获取 dynamic 类型 dynamic temp = Newtonsoft.Json.JsonConvert.DeserializeObject(cacheValue, resultType); //dynamic temp = System.Convert.ChangeType(cacheValue, resultType); // System.Convert.ChangeType(Task.FromResult(temp), type); response = Task.FromResult(temp); } else { //Task 无返回方法 指定时间内不允许重新运行 response = Task.Yield(); } } else { // 核心2,要进行 ChangeType response = Convert.ChangeType(_cache.StringGet <object>(cacheKey), type); } invocation.ReturnValue = response; return; } //去执行当前的方法 invocation.Proceed(); //存入缓存 if (!string.IsNullOrWhiteSpace(cacheKey)) { object response; //Type type = invocation.ReturnValue?.GetType(); var type = invocation.Method.ReturnType; if (typeof(Task).IsAssignableFrom(type)) { var resultProperty = type.GetProperty("Result"); response = resultProperty.GetValue(invocation.ReturnValue); } else { response = invocation.ReturnValue; } if (response == null) { response = string.Empty; } _cache.StringSet(cacheKey, response, TimeSpan.FromMinutes(qCachingAttribute.AbsoluteExpiration)); } } else { invocation.Proceed();//直接执行被拦截方法 } }
public void IsSuccessfullyInsertData() { var actIdList = _signPointActivityRepository.Query <int>("select distinct top 1 id from SignPointActivity"); var watch = new Stopwatch(); watch.Start(); //数据库应该有数据 actIdList.ShouldNotBeNull(); //条目不为空 actIdList.Any().ShouldBe(true); var node = actIdList.FirstOrDefault(); var boo = _actuatorAppService.ExportAct(node); boo.ShouldBe(true); //检测mongodb的字段 List <IEnumerable <object> > queryList = _signPointActivityRepository.QueryMultiple( "select top 1 * from SignPointActivity where ID=@id;select * from SignPointSet where ActivityID=@id", new { id = node }, typeof(SignPointActivityDto), typeof(SignPointSetDto)); SignPointActivityDto signAct = (SignPointActivityDto)queryList[0].FirstOrDefault(); signAct.ShouldNotBeNull(); List <SignPointSetDto> actSetList = new List <SignPointSetDto>(); foreach (var item in queryList[1]) { actSetList.Add((SignPointSetDto)item); } actSetList.Any().ShouldBe(true); SignActivity signMongo = _signActivityRepository.FirstOrDefault(x => x.Id == signAct.ActID.ToString()); signMongo.ShouldNotBeNull(); signMongo.Title.ShouldBe(signAct.Title); signMongo.BackGroundImg.ShouldBe(signAct.BackGroundImg); signMongo.BtnName.ShouldBe(signAct.BtnName); signMongo.BtnUrl.ShouldBe(signAct.BtnUrl); signMongo.BtnUrlName.ShouldBe(signAct.BtnUrlName); signMongo.CssStyle.ShouldBe(signAct.CssStyle); signMongo.FreeChance.ShouldBe(signAct.FreeChance); signMongo.RegistrationRemark.ShouldBe(signAct.RegistrationRemark); signMongo.Memberlogin.ShouldBe(signAct.Memberlogin); signMongo.SignPointSet.Count().ShouldBe(actSetList.Count()); string timePattern = "yyyyMMdd"; signMongo.BeginTime.ToString(timePattern).ShouldBe(signAct.BeginTime.ToString(timePattern)); signMongo.EndTime.ToString(timePattern).ShouldBe(signAct.EndTime.ToString(timePattern)); //检查奖项库存 foreach (var item in signMongo.SignPointSet) { if (item.WxHbID > 0)//红包 { int prizeType = 0; int activityType = item.IsSpecial == 0 ? 2 : 3; string hbStockCountKey = string.Format(SignKeys.signactprizegrantnum_key, item.ID, prizeType); hbStockCountKey = "B200016:" + hbStockCountKey; int hbGrantCount = _eventAwardRecordsRepository.Count(" where MemberLogin=@Memberlogin and ActivityID=@ActID and ActivityType=@ActivityType and (PrizeType=@PrizeType or PrizeType=@PrizeType2 ) and AwardSetId = @AwardSetId", new { Memberlogin = item.Memberlogin, ActID = node, ActivityType = activityType, PrizeType = 5, PrizeType2 = 6, AwardSetId = item.ID }); int count = Convert.ToInt32(_redisHelper.StringGet(hbStockCountKey)); count.ShouldBe(hbGrantCount); } if (item.ProductID > 0)//产品 { int prizeType = 1; int activityType = item.IsSpecial == 0 ? 2 : 3; string productStockCountKey = string.Format(SignKeys.signactprizegrantnum_key, item.ID, prizeType); productStockCountKey = "B200016:" + productStockCountKey; int prizeTypeDB = 0; int productStockCount = _eventAwardRecordsRepository.Count("where MemberLogin=@Memberlogin and ActivityID=@ActID and ActivityType=@ActivityType and PrizeType=@PrizeType and AwardSetId = @AwardSetId", new { Memberlogin = item.Memberlogin, ActID = node, ActivityType = activityType, PrizeType = prizeTypeDB, AwardSetId = item.ID }); int count = Convert.ToInt32(_redisHelper.StringGet(productStockCountKey)); count.ShouldBe(productStockCount); } } //检查用户数据 IEnumerable <string> openIdList = _signRecordsRepository .Query <string>("select distinct openid from SignRecords where ActivityID=@actID ", new { actID = node }); if (openIdList == null || !openIdList.Any()) { return; //无人参加 } string openid = openIdList.FirstOrDefault(); List <IEnumerable <object> > queryList2 = _signPointActivityRepository.QueryMultiple( "select * from SignRecords where ActivityID=@actID and Openid=@openId order by ID desc; " + "select * from SignSpecialRecords where ActivityID=@actID and Openid=@openId; " + "select * from MemberSignatureCard where ActivityID=@actID and Openid=@openId; " + //"select * from EventAwardRecords where ActivityID=@actID and Openid=@openId and (ActivityType=@actType or ActivityType=@actSpecialType ", "select * from EventAwardRecords where ActivityID=@actID and Openid=@openId and ActivityType=@actType union select * from EventAwardRecords where ActivityID=@actID and Openid=@openId and ActivityType=@actSpecialType ", new { actID = node, openId = openid, actType = 2, actSpecialType = 3 }, new Type[] { typeof(SignRecordsDto), typeof(SignSpecialRecordsDto), typeof(MemberSignatureCardDto), typeof(EventAwardRecordsDto) }); //签到记录 UserSignActivity userSignMongo = _userSignActRepositoryMongo.FirstOrDefault(UserSignActivity.GetId(openid, signMongo.Id)); userSignMongo.ShouldNotBeNull(); if (queryList2[0].Any()) { List <SignRecordsDto> records = new List <SignRecordsDto>(); foreach (var item in queryList2[0]) { records.Add((SignRecordsDto)item); } userSignMongo.SignRecords.Count().ShouldBe(records.Count()); var recordDB = records.OrderBy(m => m.Addtime).FirstOrDefault(); var recordMongo = userSignMongo.SignRecords.OrderBy(t => t.Addtime).FirstOrDefault(); userSignMongo.Id.ShouldBe(UserSignActivity.GetId(openid, signMongo.Id)); recordMongo.CustomerID.ShouldBe(recordDB.CustomerID); recordMongo.Day.ShouldBe(recordDB.Day); recordMongo.IsContinue.ShouldBe(recordDB.IsContinue); recordMongo.IsSignature.ShouldBe(recordDB.IsSignature); recordMongo.memberlogin.ShouldBe(recordDB.memberlogin); } //特殊 if (queryList2[1].Any()) { List <SignSpecialRecordsDto> specials = new List <SignSpecialRecordsDto>(); foreach (var item in queryList2[1]) { specials.Add((SignSpecialRecordsDto)item); } userSignMongo.SignSpecialRecords.Count().ShouldBe(specials.Count); var recordDB = specials.OrderBy(m => m.Addtime).FirstOrDefault(); var recordMongo = userSignMongo.SignSpecialRecords.OrderBy(t => t.Addtime).FirstOrDefault(); recordMongo.SpecialSDate.ShouldBe(recordDB.SpecialSDate); recordMongo.CustomerID.ShouldBe(recordDB.CustomerID); } //补签卡 if (queryList2[2].Any()) { List <MemberSignatureCardDto> cards = new List <MemberSignatureCardDto>(); foreach (var item in queryList2[2]) { cards.Add((MemberSignatureCardDto)item); } userSignMongo.MemberSignatureCard.Count().ShouldBe(cards.Count()); userSignMongo.MemberSignatureCard.Count(m => m.IsUse == 1).ShouldBe(cards.Count(m => m.IsUse == 1)); } //奖项 if (queryList2[3].Any()) { List <EventAwardRecordsDto> awards = new List <EventAwardRecordsDto>(); foreach (var item in queryList2[3]) { awards.Add((EventAwardRecordsDto)item); } userSignMongo.EventAwardRecords.Count().ShouldBe(awards.Count()); } //检测完删掉mongodb的测试数据 // _signActivityRepository.Delete foreach (var user in openIdList) { _userSignActRepositoryMongo.Delete(UserSignActivity.GetId(user, signMongo.Id)); } _signActivityRepository.Delete(signMongo.Id); watch.Stop(); watch.ElapsedMilliseconds.ShouldBeLessThan(10000); }