Пример #1
0
        //号码预定
        public JsonResult SendOrder(string tel)
        {
            try
            {
                //1.判断是否已发送 是否过期
                //2.判断是否超过一天的发送次数
                var sms = smsBll.GetList("{}").FirstOrDefault(t => t.Tel == tel && t.Type == (int)SmsType.利新靓号 && t.CreateDate.Date == DateTime.Now.Date);
                if (sms != null)
                {
                    if (sms.CreateDate.AddMinutes(WebSiteConfig.SMS_EXPIRE_MIN) > DateTime.Now)
                        throw new Exception("请1分钟后再次发送");
                    if (sms.SendCount >= WebSiteConfig.SMS_MAX_COUNT)
                        throw new Exception("一天内只能发送" + WebSiteConfig.SMS_MAX_COUNT + "次");
                }

                //6位的随机验证码
                string code = SmsCore.GetNumberCode();
                var bo = SmsCore.SendSmsLx(tel, code);
                if (bo == false)
                    throw new Exception("短信发送失败");
                if (sms == null)
                {
                    sms = new SmsInfoEntity()
                    {
                        Captcha = code,
                        CreateDate = DateTime.Now,
                        Tel = tel,
                        SendCount = 1,
                        Status = bo ? 1 : 0,
                        Type = (int)SmsType.利新靓号
                    };
                    smsBll.SaveForm(null, sms);
                }
                else
                {
                    int day = sms.CreateDate.Subtract(DateTime.Now).Days;
                    sms.CreateDate = DateTime.Now;
                    sms.Status = bo ? 1 : 0;
                    sms.Captcha = code;
                    if (day == 0)
                    {
                        sms.SendCount += 1;
                    }
                    else
                    {
                        sms.SendCount = 1;
                    }
                    smsBll.SaveForm(sms.Id, sms);
                }
                return Json(new { success = true, message = "发送成功" });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    success = false,
                    message = ex.Message
                });
            }
        }
Пример #2
0
        //注册
        public static string SendJoin(string tel)
        {
            try
            {
                //1.判断是否已发送 是否过期
                //2.判断是否超过一天的发送次数
                using (HsfDBContext hsfDBContext = new HsfDBContext())
                {
                    var sms = hsfDBContext.smsinfo.FirstOrDefault(t => t.Tel == tel && t.Type == (int)SmsType.注册);//不加时间条件的话,第二条第一次再试还是超过的
                    if (sms != null)
                    {
                        if (sms.CreateTime.AddMinutes(WebSiteConfig.SMS_EXPIRE_MIN) > DateTime.Now)
                        {
                            throw new Exception("error:Please send it again in 1 minute.");//请1分钟后再次发送
                        }
                        int day = sms.CreateTime.Subtract(DateTime.Now).Days;
                        if (day == 0)
                        {
                            sms.SendCount += 1;//已经等于6
                        }
                        else
                        {
                            sms.SendCount = 1;
                        }
                        if (sms.SendCount >= WebSiteConfig.SMS_MAX_COUNT)
                        {
                            throw new Exception("error:It can only be sent " + WebSiteConfig.SMS_MAX_COUNT + " times a day.");//一天内只能发送" + WebSiteConfig.SMS_MAX_COUNT + "次
                        }
                    }

                    //6位的随机验证码
                    string code = SmsCore.GetNumberCode();
                    var    bo   = SmsCore.SendSms(tel, code);
                    if (bo == false)
                    {
                        throw new Exception("error:Failed to send SMS");//短信发送失败
                    }
                    if (sms == null)
                    {
                        sms = new smsinfo()
                        {
                            Captcha    = code,
                            CreateTime = DateTime.Now,
                            Tel        = tel,
                            SendCount  = 1,
                            Status     = bo ? 1 : 0,
                            Type       = (int)SmsType.注册
                        };
                        hsfDBContext.smsinfo.Add(sms);
                    }
                    else
                    {
                        //int day = sms.CreateTime.Subtract(DateTime.Now).Days;
                        sms.CreateTime = DateTime.Now;
                        sms.Status     = bo ? 1 : 0;
                        sms.Captcha    = code;
                        //if (day == 0)
                        //{
                        //    sms.SendCount += 1;//已经等于6
                        //}
                        //else
                        //{
                        //    sms.SendCount = 1;
                        //}
                    }
                    hsfDBContext.SaveChanges();
                    log.Debug($"send ok");
                    return("send ok");
                }
            }
            catch (Exception ex)
            {
                log.Debug($"error:send fail {ex.Message}");
                return($"error:send fail {ex.Message}");
            }
        }