Beispiel #1
0
        /// <summary>
        /// 检测验证码是否正确
        /// </summary>
        public ServiceInvokeDTO CheckCaptcha(CaptchaCodeType type, string phone, string code)
        {
            log.Debug(Constant.DEBUG_START);
            ServiceInvokeDTO result = null;
            try
            {
                string dbCode = captchaRecordDAL.GetCode(type, phone, Config.CaptchaExpireTime);
                if (!string.IsNullOrEmpty(dbCode) && dbCode.Equals(code))
                {
                    result = new ServiceInvokeDTO(InvokeCode.SYS_INVOKE_SUCCESS);
                }
                else
                {
                    result = new ServiceInvokeDTO(InvokeCode.SYS_CAPTCHA_ERROR);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex);
                throw ex;
            }
            log.Debug(Constant.DEBUG_END);

            return result;
        }
Beispiel #2
0
        /// <summary>
        /// 根据验证码类型、手机号、有效时间来获取验证码
        /// </summary>
        /// <param name="type">验证码类型</param>
        /// <param name="phone">手机号</param>
        /// <param name="secondSpan">验证码有效时间</param>
        public string GetCode(CaptchaCodeType type, string phone, int secondSpan)
        {
            string code = string.Empty;
            const string sql = @"SELECT Code FROM CaptchaRecord WHERE IsDeleted = 0
                               AND CodeType = @CodeType AND Phone = @Phone
                               AND TIMESTAMPDIFF(SECOND, AddTime, NOW()) <= @SecondSpan ORDER BY AddTime DESC LIMIT 1";

            using (DbConnection connection = ConnectionManager.OpenConnection)
            {
                code = connection.Query<string>(sql, new { CodeType = type, Phone = phone, SecondSpan = secondSpan }).SingleOrDefault<string>();
            }
            return code;
        }
Beispiel #3
0
        /// <summary>
        /// 根据验证码类型、用户IP地址、手机号、间隔时间获取验证码
        /// </summary>
        /// <param name="codeType">验证码类型</param>
        /// <param name="ip">用户IP地址</param>
        /// <param name="phone">手机号</param>
        /// <param name="secondSpan">同IP获取验证码间隔时间</param>
        public CaptchaRecord GetCode(CaptchaCodeType codeType, string ip, string phone, int secondSpan)
        {
            CaptchaRecord captcha = null;
            const string sql = @"SELECT * FROM CaptchaRecord WHERE IsDeleted = 0 AND CodeType = @CodeType
                               AND IP = @IP AND Phone = @Phone
                               AND TIMESTAMPDIFF(SECOND, AddTime, NOW()) <= @SecondSpan ORDER BY AddTime DESC LIMIT 1";

            using (DbConnection connection = ConnectionManager.OpenConnection)
            {
                captcha = connection.Query<CaptchaRecord>(sql, new { CodeType = codeType, IP = ip, Phone = phone, SecondSpan = secondSpan }).SingleOrDefault<CaptchaRecord>();
            }
            return captcha;
        }
Beispiel #4
0
        /// <summary>
        /// 调用云片网络短信发送平台API地址发送短信
        /// </summary>
        private ServiceInvokeDTO InvokeYPToSendSms(string phone, string code, CaptchaCodeType codeType)
        {
            log.Debug(Constant.DEBUG_START);

            ServiceInvokeDTO result = null;

            // 调用第三方接口发送短信
            RestClient client = new RestClient(YUNPIAN_HOST);
            RestRequest request = new RestRequest("sms/send.json", Method.POST);
            request.Timeout = API_REQUEST_TIME_OUT;
            request.AddParameter("apikey", Config.SmsApiKey);
            request.AddParameter("mobile", phone);

            // 短信内容
            string content = string.Empty;
            switch (codeType)
            {
                case CaptchaCodeType.RegCode: content = string.Format(Config.SmsRegTemplate, code); break;
                case CaptchaCodeType.GetPwdCode: content = string.Format(Config.SmsGetPasswordTemplate, code); break;
                default: break;
            }
            request.AddParameter("text", content);

            IRestResponse response = client.Execute(request);
            if (response.ErrorException == null)
            {
                YPSmsResultDTO ypResult = JsonConvert.DeserializeObject<YPSmsResultDTO>(response.Content);
                if (ypResult.code == Constant.YP_INVOKE_SUCCESS_CODE)
                {
                    result = new ServiceInvokeDTO(InvokeCode.SYS_INVOKE_SUCCESS);
                }
                else
                {
                    log.Error(ypResult.msg);
                    result = new ServiceInvokeDTO(InvokeCode.SMS_YUNPIAN_ERROR);
                }
            }
            else
            {
                log.Error(response.ErrorException);
                result = new ServiceInvokeDTO(InvokeCode.SMS_YUNPIAN_ERROR);
            }

            log.Debug(Constant.DEBUG_END);

            return result;
        }