Example #1
0
        public async Task <IActionResult> Json_SendMobCode(string mob)
        {
            try
            {
                string contents = "";
                Random rad      = new Random();              //实例化随机数产生器rad;
                int    code     = rad.Next(100000, 1000000); //用rad生成大于等于100000,小于等于999999的随机数;
                var    sms      = new ah.Models.CHIS_DataTemp_SMS();
                sms.PhoneCode = mob;
                sms.VCode     = code.ToString();
                sms.CreatTime = DateTime.Now;
                MainDbContext.CHIS_DataTemp_SMS.Add(sms);
                MainDbContext.SaveChanges();
                mob = mob.Replace(" ", "");
                var s = new SMS();
                var h = MainDbContext.CHIS_DataTemp_SMS.OrderByDescending(m => m.CreatTime).FirstOrDefault(m => m.PhoneCode == mob);
                contents = $"{h.VCode}找回密码验证码【天使健康】";
                string rlt = await s.PostSmsInfo(mob, contents);

                if (rlt != "true")
                {
                    new Exception(rlt);
                }
                return(Json(new { rlt = true, msg = "" }));
            }
            catch (Exception e)
            { return(Json(new { rlt = false, msg = e.Message })); }
        }
Example #2
0
        public async Task <JsonResult> SendRegVCode(string regAccount)
        {
            int NO_REPEAT_SEC = 120;//在80秒内不重复发送

            try
            {
                regAccount = Ass.P.PStr(regAccount).Trim();
                if (regAccount.GetStringType().IsMobile)
                {
                    var now = DateTime.Now;
                    var h   = MainDbContext.CHIS_DataTemp_SMS.AsNoTracking().Where(m => m.PhoneCode == regAccount).OrderByDescending(m => m.SMSId).FirstOrDefault();
                    if (h != null && (now - h.CreatTime.Value).TotalSeconds < NO_REPEAT_SEC)
                    {
                        return(Json(new { rlt = true, msg = $"手机验证码已经发送,{NO_REPEAT_SEC}秒内不用重复请求。" }));
                    }
                    string contents = "";
                    var    random   = ah.Code.Utility.ComTools.GenerateRandomNumber(6, true);
                    var    sms      = new ah.Models.CHIS_DataTemp_SMS();
                    sms.PhoneCode = regAccount;
                    sms.VCode     = random.ToString();
                    sms.CreatTime = now;
                    MainDbContext.CHIS_DataTemp_SMS.Add(sms);
                    MainDbContext.SaveChanges();
                    var s = new SMS();
                    h        = MainDbContext.CHIS_DataTemp_SMS.OrderByDescending(m => m.CreatTime).FirstOrDefault(m => m.PhoneCode == regAccount);
                    contents = $"{h.VCode} , 您的注册手机验证码【天使健康】";
                    string rlt = await s.PostSmsInfo(regAccount, contents);

                    if (rlt != "true")
                    {
                        new Exception(rlt);
                    }
                    return(Json(new { rlt = true, msg = "手机验证码发送成功" }));
                }

                if (regAccount.GetStringType().IsEmail)
                {
                    var now = DateTime.Now;
                    var h   = MainDbContext.CHIS_DataTemp_SendMailVCode.AsNoTracking().Where(m => m.EmailAddress == regAccount).OrderByDescending(m => m.SendMailId).FirstOrDefault();
                    if (h != null && (now - h.CreatTime.Value).TotalSeconds < NO_REPEAT_SEC)
                    {
                        return(Json(new { rlt = true, msg = $"邮箱验证码已经发送,{NO_REPEAT_SEC}秒内不用重复请求。" }));
                    }

                    //6为随机数并存数据库
                    var random    = ah.Code.Utility.ComTools.GenerateRandomNumber(6, true);
                    var emailData = new CHIS_DataTemp_SendMailVCode
                    {
                        CreatTime    = now,
                        EmailAddress = regAccount,
                        VCode        = random,
                        VCodeProp    = null
                    };
                    MainDbContext.CHIS_DataTemp_SendMailVCode.Add(emailData);
                    MainDbContext.SaveChanges();
                    //向邮箱发送一份验证邮件
                    EmailHelper email = new EmailHelper();
                    string      sub   = "天使健康医生工作站-(验证码,不用回复)";
                    string      msg   = "您的邮箱验证码为:[code]".Replace("[code]", random);//  $"{random}本次操作验证码";
                    email.SendEmail(regAccount, msg, sub);
                    return(Json(new { rlt = true, msg = "" }));
                }

                throw new Exception("传入非法账户号");
            }
            catch (Exception e)
            { return(Json(new { rlt = false, msg = e.Message })); }
        }