Beispiel #1
0
        /// <summary>
        /// 发送验证码
        /// </summary>
        /// <param name="receiver">接收者</param>
        /// <param name="type">类型</param>
        /// <param name="paramsList">参数列表</param>
        /// <param name="code">验证码(out 返回)</param>
        /// <param name="visitId">访问者ID</param>
        /// <returns></returns>
        public Guid SendVerifyCode(string receiver, string type, List <KeyValuePair <string, string> > paramsList, Guid visitId, out string code)
        {
            if (VerifyHelper.IsEmpty(receiver) || VerifyHelper.IsEmpty(type) || VerifyHelper.IsEmpty(visitId))
            {
                throw new DefaultException(EnumResultCode.参数错误, "receiver", "type", "visitId");
            }

            //内容
            var content = TemplateConfig.GetValue(type);

            //参数
            if (VerifyHelper.IsEmpty(paramsList))
            {
                paramsList = new List <KeyValuePair <string, string> >();
            }

            //code(必须,默认)
            code = RandHelper.GetRandNumber(6);
            var codeParamModel = paramsList.Where(x => x.Key.ToLower() == "code").FirstOrDefault();

            if (VerifyHelper.IsEmpty(paramsList))
            {
                paramsList.Add(new KeyValuePair <string, string>("code", code));
            }
            else
            {
                codeParamModel = new KeyValuePair <string, string>("code", code);
            }

            //替换
            if (!VerifyHelper.IsEmpty(paramsList))
            {
                paramsList.ForEach(x =>
                {
                    content = content.Replace("{" + x.Key + "}", x.Value);
                });
            }


            List <string> smsTypes = new List <string>()
            {
                KeyModel.Config.Template.KeyRegistMobile,
                KeyModel.Config.Template.KeyBindMobile,
                KeyModel.Config.Template.KeyForgetPwdMobile
            };

            List <string> emailTypes = new List <string>()
            {
                KeyModel.Config.Template.KeyRegistEmail,
                KeyModel.Config.Template.KeyBindEmail,
                KeyModel.Config.Template.KeyForgetPwdEmail
            };

            bool isSuccess = false;

            //发送
            if (smsTypes.Contains(type))
            {
                isSuccess = SendSms(receiver, content);
            }
            if (emailTypes.Contains(type))
            {
                isSuccess = SendEmail(receiver, content);
            }

            if (!isSuccess)
            {
                throw new DefaultException(EnumResultCode.验证码发送失败);
            }

            var model = new VerifyCodeModel();

            model.TaskId   = GuidHelper.NewId();
            model.Receiver = receiver;
            model.Code     = code;
            model.VisitId  = visitId;
            model.SendTime = DateTime.Now;

            //保存
            RedisHelper.Set <VerifyCodeModel>(string.Format(KeyModel.Session.FormatVerifyCode, model.TaskId), model, DateTime.Now.AddMinutes(10));

            return(model.TaskId);
        }