Example #1
0
        public IActionResult GetSms(string mobile, string code)
        {
            CheckDataSourceParameter();

            if (SqlMap.Params.GetValue("checkvalid", true))
            {
                string validCode = GetValidCode(App.GetQuery("type", "root"), true);
                if (!code.HasValue() || validCode != code.ToUpper())
                {
                    throw new AceException("验证码输入错误");
                }
            }

            var chkSql = SqlMap.Params.GetValue("checksql", "");
            var chkErr = SqlMap.Params.GetValue("checkerr", "");

            if (chkSql.HasValue())
            {
                chkSql = AppCtx.AC.Replace(chkSql, true);
                if ((int)AppCtx.Session.ExecuteScalar(chkSql, null) > 0)
                {
                    throw new AceException(chkErr);
                }
            }

            var rndStr     = RandomHelper.GetRandomNumber(6);
            var key        = "sms_" + mobile;
            var keySending = "sms_sending_" + mobile;

            if (App.Cache.GetString(keySending) != null)
            {
                throw new AceException("不能频繁获取短信验证码");
            }

            var expired = SqlMap.Params.GetValue("sms_expired", 15);
            var sign    = SqlMap.Params.GetValue("sms_sign", "");
            var temp    = SqlMap.Params.GetValue("sms_temp", "");
            var times   = SqlMap.Params.GetValue("sms_retrytimes", 3);

            var success = false;
            var sms     = cloudService.GetSmsService();

            for (var i = 0; i < times; i++)
            {
                try
                {
                    sms.Send(mobile, sign, temp, new { code = rndStr }, null);
                    logger.LogDebug($"Send valid code \"{rndStr}\" to \"{mobile}\"");
                    success = true;
                    break;
                }
                catch (Exception ex)
                {
                    logger.LogError($"Send valid code to \"{mobile}\" with error: {ex.GetMessage()}");
                }
            }
            if (!success)
            {
                throw new AceException(SqlMap.Params.GetValue("sms_errservice", ""));
            }

            App.Cache.SetString(key, rndStr, opts =>
            {
                opts.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(expired);
            });
            App.Cache.SetString(keySending, "1", opts =>
            {
                opts.AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1);
            });

            return(Ok(null));
        }