예제 #1
0
        public CaptchaHelperModel SendAuthCode(CaptchaEnum captchaEnum, int accId, CaptchaPhoneEmailEnum typeEnum,
                                               string phoneOrEmail)
        {
            var redisCacheService = new RedisCacheService();

            if (string.IsNullOrWhiteSpace(phoneOrEmail))
            {
                throw new ArgumentNullException("phoneOrEmail", "手机号码邮箱地址为空");
            }


            //获取失败次数
            var strWrongTimesKey = GetWrongTimesKey(captchaEnum, accId);



            int iWrongTimes = redisCacheService.Get <int>(strWrongTimesKey);

            //判断验证码错误次数
            if (iWrongTimes > _checkWrongTimes)
            {
                return(new CaptchaHelperModel(false, "验证码错误次数过多,请1小时后重试或联系客服", typeEnum));
            }

            //获取验证码key
            var strCaptchaKey = GetCaptchaKey(captchaEnum, accId, phoneOrEmail);

            if (string.IsNullOrEmpty(strCaptchaKey))
            {
                return(new CaptchaHelperModel(false, "错误的手机号或邮箱", typeEnum));
            }


            //判断是否之前发过验证码
            int iCaptcha = redisCacheService.Get <int>(strCaptchaKey);

            if (iCaptcha == 0)
            {
                iCaptcha = Helper.GetInt32(Helper.GetRandomNum(), 111111);
            }

            var smsStr = string.Format("【生意专家】您本次获取的验证码为:{0},此验证码{1}分钟内有效。维护您的数据安全是生意专家义不容辞的责任。", iCaptcha,
                                       _outTimeMinutes);
            var mailSend = new EmailSend();
            var smsSend  = new SmsSend();
            var result   = typeEnum == CaptchaPhoneEmailEnum.Phone
                ? smsSend.SendSys(phoneOrEmail, smsStr, 13)
                : mailSend.SendVerifiEmail(accId, "", phoneOrEmail, iCaptcha.ToString());

            if (result)
            {
                _logger.Debug("发送验证码成功:" + iCaptcha);
                redisCacheService.Set(strCaptchaKey, iCaptcha, _outTimeMinutes * 60);
                return(new CaptchaHelperModel(true, "发送验证码成功", CaptchaPhoneEmailEnum.Email));
            }
            else
            {
                return(new CaptchaHelperModel(false, "发送失败", CaptchaPhoneEmailEnum.Email));
            }
        }