Beispiel #1
0
        /// <summary>
        /// 判断是否能够发送验证码,默认间隔60秒发一次
        /// </summary>
        /// <param name="smsVerifyCode"></param>
        /// <returns></returns>
        private bool IsSendTooFast(SmsVerifyCode smsVerifyCode)
        {
            if (smsVerifyCode == null)
            {
                return(false);
            }
            long now = DateUtility.ConvertDateTimeLong(DateTime.UtcNow);

            if (now < smsVerifyCode.NextSendTime)
            {
                return(true);
            }
            return(false);
        }
Beispiel #2
0
        /// <summary>
        /// 判断验证码是否超时
        /// </summary>
        /// <param name="smsVerifyCode"></param>
        /// <returns></returns>
        private bool IsVerifyCodeTimeOut(SmsVerifyCode smsVerifyCode)
        {
            if (smsVerifyCode == null)
            {
                return(false);
            }
            long now = DateUtility.ConvertDateTimeLong(DateTime.UtcNow);

            if (now > smsVerifyCode.TimeOut)
            {
                return(true);
            }
            return(false);
        }
Beispiel #3
0
        /// <summary>
        /// 验证验证码是否正确
        /// </summary>
        /// <param name="destMobile"></param>
        /// <param name="verifyCode"></param>
        /// <param name="codeType"></param>
        /// <param name="isIngoreUsed"></param>
        /// <returns></returns>
        public bool CheckVerifyCode(string destMobile, string verifyCode, int codeType = 0, bool isIngoreUsed = true)
        {
            SmsVerifyCode smsVerifyCode = DataBase.Get <SmsVerifyCode>(e => e.Mobile == destMobile && e.Code == verifyCode && e.CodeType == codeType);

            if (smsVerifyCode == null)
            {
                return(false);
            }
            if (!isIngoreUsed && smsVerifyCode.IsUsed)
            {
                return(false);
            }
            bool isTimeOut = IsVerifyCodeTimeOut(smsVerifyCode);

            if (isTimeOut)
            {
                return(false);
            }
            smsVerifyCode.IsUsed = true;
            DataBase.Update(smsVerifyCode);
            return(true);
        }
Beispiel #4
0
        /// <summary>
        /// 判断发送的验证码是否达到上限(每天)
        /// </summary>
        /// <param name="smsVerifyCode"></param>
        /// <returns></returns>
        private bool IsSendVerifyCodeLimited(SmsVerifyCode smsVerifyCode)
        {
            if (smsVerifyCode == null)
            {
                return(false);
            }
            DateTime now      = DateTime.Now.Date;
            DateTime sendDate = DateTime.Parse(smsVerifyCode.CurrentDate);

            if (now > sendDate)
            {
                return(false);
            }
            if (now == sendDate)
            {
                if (smsVerifyCode.SendAmount >= MiddleTier.BusinessConfig.VerifyCodeLimited)
                {
                    return(true);
                }
            }
            return(false);
        }
Beispiel #5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="destMobile"></param>
        /// <param name="codeType">0-注册验证码</param>
        /// <returns>0-发送成功 1-1分钟内不能重复发送 2-今日发送达到上限 3-发送失败</returns>
        public int SendVerifyCode(string destMobile, int codeType)
        {
            string        code          = RandomUtil.GenerateVerfiyCode();
            SmsVerifyCode smsVerifyCode = DataBase.Get <SmsVerifyCode>(e => e.Mobile == destMobile && e.CodeType == codeType);

            if (smsVerifyCode != null)
            {
                //判断有没有连续发送
                bool isSendTooFast = IsSendTooFast(smsVerifyCode);
                if (isSendTooFast)
                {
                    return(1);
                }
                //判断今天是否已经达到发送上限
                bool isOverLimited = IsSendVerifyCodeLimited(smsVerifyCode);
                if (isOverLimited)
                {
                    return(2);
                }
                string nowCurrentData = DateTime.Now.ToString("yyyy-MM-dd");
                if (nowCurrentData == smsVerifyCode.CurrentDate)
                {
                    smsVerifyCode.SendAmount += 1;
                }
                else
                {
                    smsVerifyCode.SendAmount  = 1;
                    smsVerifyCode.CurrentDate = nowCurrentData;
                }
                smsVerifyCode.Code         = code;
                smsVerifyCode.IsUsed       = false;
                smsVerifyCode.UpdateDate   = DateTime.Now;
                smsVerifyCode.TimeOut      = DateUtility.ConvertDateTimeLong(DateTime.UtcNow.AddSeconds(MiddleTier.BusinessConfig.VerifyCodeTimeOut));
                smsVerifyCode.NextSendTime = DateUtility.ConvertDateTimeLong(DateTime.UtcNow.AddSeconds(MiddleTier.BusinessConfig.VerifyCodeSendInterval));
                DataBase.Update <SmsVerifyCode>(smsVerifyCode);
            }
            else
            {
                smsVerifyCode              = new SmsVerifyCode();
                smsVerifyCode.Code         = code;
                smsVerifyCode.Mobile       = destMobile;
                smsVerifyCode.CodeType     = codeType;
                smsVerifyCode.CreateDate   = DateTime.Now;
                smsVerifyCode.CurrentDate  = DateTime.Now.ToString("yyyy-MM-dd");
                smsVerifyCode.IsUsed       = false;
                smsVerifyCode.SendAmount   = 1;
                smsVerifyCode.TimeOut      = DateUtility.ConvertDateTimeLong(DateTime.UtcNow.AddSeconds(MiddleTier.BusinessConfig.VerifyCodeTimeOut));
                smsVerifyCode.NextSendTime = DateUtility.ConvertDateTimeLong(DateTime.UtcNow.AddSeconds(MiddleTier.BusinessConfig.VerifyCodeSendInterval));
                smsVerifyCode.UpdateDate   = DateTime.Now;
                DataBase.Save <SmsVerifyCode>(smsVerifyCode);
            }
            //提交到短信网关
            try
            {
                SendSms(destMobile, code);
            }
            catch
            {
                return(3);
            }
            return(0);
        }