/// <summary> /// 检查验证码 /// </summary> /// <param name="Mobile"></param> /// <param name="MsgType"></param> /// <param name="Title"></param> /// <returns></returns> public bool CheckVerifyCode(string Mobile, string MsgType, string Title) { var cacheKey = new StringCacheKey(StringCacheKeyType.SYS_SMSVerifyCode, $"{Mobile}/{MsgType}/{Title}"); UserShortMessageLog model = cacheKey.FromCache <UserShortMessageLog>(); return(model != null); }
public ApiResult GetMs(string mobile, string type) { if (string.IsNullOrEmpty(mobile) || string.IsNullOrEmpty(type)) { return(new ApiResult() { Msg = "mobile 或者 type 不能为空" }); } int itype = 0; try { itype = Convert.ToInt32(type); } catch { return(new ApiResult() { Msg = "type 类型错误" }); } var serviceMsgLog = new BLL.Sys.Implements.SysShortMessageService(); UserShortMessageLog msgModel = serviceMsgLog.GetLastSMSLog(mobile, itype); if (msgModel == null) { return(new ApiResult() { Msg = "没找到改手机号,type为" + itype + "的验证码" }); } return(new ApiResult() { Data = msgModel.MsgTitle }); }
/// <summary> /// 根据手机号查询最后一条短信验证码信息 /// </summary> /// <param name="telePhone"></param> public UserShortMessageLog GetLastSMSLog(string telePhone, int type) { UserShortMessageLog model = null; try { var db = new DBEntities(); var msgList = (from p in db.UserShortMessageLogs.Where(m => m.IsDeleted == false && m.TelePhoneNum == telePhone && m.MsgLogType == type). OrderByDescending(m => m.CreateTime) select p).ToList <UserShortMessageLog>(); if (msgList != null && msgList.Count > 0) { model = msgList[0]; } } catch (Exception ex) { model = null; throw ex; } return(model); }
/// <summary> /// 发送短信 /// </summary> /// <param name="evt"></param> /// <returns></returns> public ApiResult SendMsg(RequestSendSMSDTO evt) { string reason = "发送失败"; try { using (var db = new DBEntities()) { UserShortMessageLog model = new UserShortMessageLog() { ShortMessageLogID = Guid.NewGuid().ToString("N"), MsgLogType = evt.MsgType, UserID = evt.UserID ?? "", TelePhoneNum = evt.Mobile, MsgTitle = evt.Title ?? "", MsgContent = evt.Content ?? "", OutTime = evt.OutTime.HasValue ? evt.OutTime.Value : DateTime.MaxValue, IsDeleted = true //代表未生效 }; #region 校验:短信发送频率控制 (本系统缓存控制) var smsCheckResult = CheckSendRate(evt.Mobile, evt.TemplateID); var Reason = GetSendRateMessage(smsCheckResult); if (!string.IsNullOrEmpty(Reason)) { model.MsgContent = Reason; db.UserShortMessageLogs.Add(model); db.SaveChanges(); return(EnumApiStatus.BizSMSOverclock.ToApiResultForApiStatus("您获取验证码太过频繁,请稍后再试")); } #endregion db.UserShortMessageLogs.Add(model); var i = db.SaveChanges(); if (i > 0) { var flag = SMSHelper.SendSMS(evt.Mobile, evt.TemplateID, BuildSMSParaStr(evt.MsgParms, evt.SMSVender), evt.SMSVender, out reason); if (!flag) { if (!string.IsNullOrEmpty(reason)) { model.MsgContent = reason; db.SaveChanges(); //return new ApiResult() { Status = EnumApiStatus.BizSMSOverclock, Msg = "该手机号短信发送频率太高,请稍候重试"}; } //接口返回失败的话 暂时都认为是超频。前端便于做是否重试控制 return(new ApiResult() { Status = EnumApiStatus.BizSMSOverclock, Msg = "您获取验证码太过频繁,请稍后再试" }); } else { //生效 model.IsDeleted = false; db.SaveChanges(); UpdateSendRate(evt.Mobile, smsCheckResult); if (evt.OutTime.HasValue) { StringCacheKey cacheKey = new StringCacheKey(StringCacheKeyType.SYS_SMSVerifyCode, $"{evt.Mobile}/{evt.MsgType}/{evt.Title}"); model.ToCache(cacheKey, evt.OutTime.Value - DateTime.Now); } return(true.ToApiResultForBoolean()); } } } } catch (Exception ex) { XuHos.Common.LogHelper.WriteError(ex); } return(reason.ToApiResultForObject(EnumApiStatus.BizError, reason)); }