/// <summary></summary> /// <param name="accountId"></param> /// <param name="phoneNumber"></param> /// <param name="validationType"></param> /// <returns></returns> public int Send(string accountId, string phoneNumber, string validationType) { if (string.IsNullOrEmpty(phoneNumber)) { throw new NullReferenceException(phoneNumber); } string objectType = VerificationObjectType.Mobile.ToString(); // 获取验证码信息 VerificationCodeInfo verificationCode = VerificationCodeContext.Instance.VerificationCodeService.Create(objectType, phoneNumber, validationType, IPQueryContext.GetClientIP()); // 获取验证模板信息 VerificationCodeTemplateInfo template = VerificationCodeContext.Instance.VerificationCodeTemplateService.FindOne(objectType, validationType); string messageContent = TemplateContentContext.Instance.TemplateContentService.GetHtml(template.TemplateContentName); string serialNumber = DateTime.Now.ToString("yyyyMMddHHmmssfff") + StringHelper.ToRandom("0123456789", 3); // 设置短信内容 ShortMessage message = new ShortMessage(); message.SerialNumber = serialNumber; message.PhoneNumber = phoneNumber; message.MessageContent = string.Format(messageContent, verificationCode.Code); if (string.IsNullOrEmpty(template.Options)) { template.Options = "{}"; } // 在参数中加入验证码对象 template.Options = template.Options.Insert(1, "\"code\":\"" + verificationCode.Code + "\","); // 设置短信模板代码 JsonData data = JsonMapper.ToObject(template.Options); message.MessageTemplateCode = JsonHelper.GetDataValue(data, "templateCode"); // message.MessageTemplateParam = "{\"code\":\"" + verificationCode.Code + "\",\"product\":\"" + JsonHelper.GetDataValue(data, "product") + "\"}"; message.MessageTemplateParam = data.ToJson(); // 发送短信 string returnCode = client.Send(message); // 保存结果至数据库 this.provider.Send(serialNumber, accountId, phoneNumber, message.MessageContent, returnCode); if (string.IsNullOrEmpty(returnCode)) { return(0); } return(Convert.ToInt32(returnCode)); }
/// <summary></summary> /// <param name="doc">Xml 文档对象</param> /// <returns>返回操作结果</returns> public string SendVerificationMail(XmlDocument doc) { if (SecurityConfigurationView.Instance.CaptchaMode == "ON") { // 验证码 string captcha = XmlHelper.Fetch("captcha", doc); if (HttpContext.Current.Session["captcha"] == null || captcha != HttpContext.Current.Session["captcha"].ToString()) { return("{\"message\":{\"returnCode\":1,\"value\":\"验证码错误。\"}}"); } } // 检查验证码发送时间 if (HttpContext.Current.Session["VerificationCodeSendTime"] != null && HttpContext.Current.Session["VerificationCodeSendTime"] is DateTime) { DateTime time = (DateTime)HttpContext.Current.Session["VerificationCodeSendTime"]; if (time.AddSeconds(VerificationCodeConfigurationView.Instance.SendInterval) > DateTime.Now) { return("{\"message\":{\"returnCode\":1,\"value\":\"发送太频繁,请稍后再试。\"}}"); } } // 邮件地址 string email = XmlHelper.Fetch("email", doc); // 验证类型 string validationType = XmlHelper.Fetch("validationType", doc); VerificationCodeInfo verificationCode = VerificationCodeContext.Instance.VerificationCodeService.Create("Mail", email, validationType, IPQueryContext.GetClientIP()); VerificationCodeTemplateInfo template = VerificationCodeContext.Instance.VerificationCodeTemplateService.FindOne("Mail", validationType); JsonData options = JsonMapper.ToObject(template.Options); string content = TemplateContentContext.Instance.TemplateContentService.GetHtml(template.TemplateContentName); EmailClientContext.Instance.Send(email, JsonHelper.GetDataValue(options, "subject"), string.Format(content, verificationCode.Code), EmailFormat.Html); HttpContext.Current.Session["VerificationCodeSendTime"] = DateTime.Now; return("{\"message\":{\"returnCode\":0,\"value\":\"发送成功。\"}}"); }