Пример #1
0
        public TraceEntity Send(SMSEntity entity)
        {
            TraceEntity result;

            try
            {
                //注入实例
                instance = container.Resolve <ISMS>(entity.IOC_Class_Alias);
            }
            catch (Exception e)
            {
                Common.LogIt(e.ToString());
                throw;
            }

            result = instance.Send(entity);
            return(result);
        }
Пример #2
0
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="sender">短信发送者对象,需要在控制器内注入得到后再传过来</param>
        /// <param name="mobile">手机号码</param>
        /// <param name="content">短信内容</param>
        /// <param name="sendFor">发送目的,你为什么要短信?比如注册、忘记密码,同时也可以让注册和忘记密码短信互不干扰</param>
        /// <param name="sendFor">短信验证码,如果是发的短信验证码,就记录一下</param>
        protected bool SmsSend(ISMS sender, string mobile, string content, string sendFor, string smsCode = "")
        {
            var sendRes = sender.Send(mobile, content);
            var log     = new SMSLog()
            {
                AddTime    = DateTime.Now,
                Code       = smsCode,
                Content    = content,
                Mobile     = mobile,
                Platform   = sendRes.Platform,
                ResultStr  = sendRes.ResStr,
                SendFor    = sendFor,
                Success    = sendRes.Success,
                UpdateTime = DateTime.Now
            };

            db.SMSLog.Add(log);
            db.SaveChanges();
            return(sendRes.Success);
        }
Пример #3
0
        /// <summary>
        /// 发送验证码短信
        /// </summary>
        /// <param name="mobile"></param>
        /// <param name="sendFor"></param>
        /// <returns></returns>
        public async Task <JsonResultObj> SendVerifyCode(string mobile, string sendFor)
        {
            //手机号不能为空
            if (string.IsNullOrWhiteSpace(mobile))
            {
                return(JsonResultError("请输入手机号!"));
            }

            //验证手机号合法性
            if (!Regex.IsMatch(mobile, Constant.REGEX_PHONE))
            {
                return(JsonResultError("请输入正确的手机号码!"));
            }
            ///短信发送目的允许的白名单
            var smsSendForAllowList = new string[] { "注册", "忘记密码" };

            if (!smsSendForAllowList.Contains(sendFor))
            {
                return(JsonResultError("短信发送目的非法!"));
            }

            var smsLog = await db.SMSLog
                         .OrderByDescending(p => p.Id)
                         .FirstOrDefaultAsync(p => p.Mobile == mobile && p.SendFor == sendFor);

            //如果是正式环境的话,那么验证码是随机生成4位数,如果是测试环境,验证码就是1
#if DEBUG
            int smsCode = 1;
#else
            int smsCode = new Random().Next(1000, 9999);
#endif
            var msg = "您的验证码是:" + smsCode + ",请不要把验证码透露给他人。如非本人操作,可不用理会!";

            //如果没有发过验证码的话,那极好的,马上发
            if (smsLog == null)
            {
                var res = smsSender.Send(mobile, msg);
                if (res.Success)
                {
                    db.SMSLog.Add(new SMSLog {
                        AddTime    = DateTime.Now,
                        Code       = smsCode.ToString(),
                        Content    = msg,
                        Mobile     = mobile,
                        Platform   = smsSender.GetPlatform(),
                        ResultStr  = res.ResStr,
                        SendFor    = sendFor,
                        Success    = res.Success,
                        UpdateTime = DateTime.Now
                    });
                    await db.SaveChangesAsync();

                    return(JsonResultSuccess("验证码发送成功!", 90));
                }
            }
            else
            {
                //如果发过验证码的话,要判断时间间隔是否够90秒
                var ts = DateTime.Now - smsLog.AddTime;
                if (ts.TotalSeconds < 90)
                {
                    return(JsonResultError("您已经获取过验证码,请" + (90 - (int)ts.TotalSeconds) + "秒后再获取!", (90 - (int)ts.TotalSeconds)));
                }
                else
                {
                    var res = smsSender.Send(mobile, msg);
                    if (res.Success)
                    {
                        db.SMSLog.Add(new SMSLog {
                            AddTime    = DateTime.Now,
                            Code       = smsCode.ToString(),
                            Content    = msg,
                            Mobile     = mobile,
                            Platform   = smsSender.GetPlatform(),
                            ResultStr  = res.ResStr,
                            SendFor    = sendFor,
                            Success    = res.Success,
                            UpdateTime = DateTime.Now
                        });
                        await db.SaveChangesAsync();

                        return(JsonResultSuccess("验证码发送成功!", 90));
                    }
                }
            }
            return(JsonResultError("请确定你输入的信息正确,确认正确后重新输入,若再次出现改错误,请联系客服,谢谢...."));
        }