1:创建一个项目用来调用第三方的类,右键Nuget添加第三方的引用类库 qcloudsms_csharp
/// <summary>
/// 第三方
/// </summary>
public class TenXunYunSMS
{
//appId
public int appId;
//appKey
public string appKey = "";
//短信模板ID
private int tmplateId = 379257;
//签名内容
private string smsSign = "7hhhcn";
/// <summary>
/// 验证码
/// </summary>
public int Code { get; set; }
/// <summary>
/// 发送验证码
/// </summary>
/// <param name="phone"></param>
/// <returns></returns>
public void SetSMS(string phone)
{
Random random = new Random();
int code = random.Next(100000, 999999);
try
{
SmsSingleSender ssender = new SmsSingleSender(appId, appKey);
var result = ssender.sendWithParam("86", phone,
tmplateId, new[] { code.ToString() }, smsSign, "", ""); // 签名参数未提供或者为空时,会使用默认签名发送短信
}
catch (JSONException ex)
{
}
catch (HTTPException ex)
{
}
catch (Exception ex)
{
}
Code = code;
}
}
2.把第三方的公共类放入到我们的项目里
3.创建一张短信发送记录表
create table SMSInfo(
Id INT IDENTITY PRIMARY KEY,
Tel bigint not null,
Code int not null,
CreateDate date not null,
ExpiredDate date not null,
)
4:数据访问层写插入短信信息表方法
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MiShop.Core;
using MiShop.Model;
namespace MiShop.DAL
{
/// <summary>
/// 数据访问层
/// </summary>
public class SMSInfoRepository
{
//是否插入发送信息
public int InsertSMSInfo(SMSInfo sMS)
{
string sql = @"INSERT INTO SMSInfo(Tel,Code,CreateDate,ExpiredDate)values(@Tel,@Code,@CreateTime,@ExpiredDate)";
SqlHelpe sqlHelpe = new SqlHelpe();
SqlParameter[] parameters = {
new SqlParameter()
{
DbType = System.Data.DbType.Int64,
ParameterName ="@Tel",
Value =sMS.Tel
},
new SqlParameter()
{
DbType = System.Data.DbType.Int64,
ParameterName ="@Code",
Value = sMS.Code
},
new SqlParameter()
{
DbType = System.Data.DbType.DateTime,
ParameterName ="@CreateTime",
Value =DateTime.Now
},
new SqlParameter()
{
DbType = System.Data.DbType.DateTime,
ParameterName ="@ExpiredDate",
Value = DateTime.Now.AddMinutes(5)
}
};
return sqlHelpe.ExecuteNonQuery(sql, parameters);
}
}
}
5:业务层:先引用第三方项目,先调用第三方类,发送验证码,然后将验证码存储到短信信息对象,最后调用数据访问层的插入短信的方法。
using MiShop.DAL;
using MiShop.Model;
using MiShop.Remote;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MiShop.BLL
{
/// <summary>
/// 业务层,实例化数据访问层方法
/// </summary>
public class SMSInfoService
{
public bool InsertSMSInfo(SMSInfo sMS)
{
//发送验证码
TenXunYunSMS tenXunYunSMS = new TenXunYunSMS();
tenXunYunSMS.appId = Convert.ToInt32(ConfigurationManager.AppSettings["appId"]);
tenXunYunSMS.appKey = Convert.ToString(ConfigurationManager.AppSettings["appKey"]);
//调用第三方类中的方法
tenXunYunSMS.SetSMS(Convert.ToString(sMS.Tel));
//赋值
sMS.Code = tenXunYunSMS.Code;
//new数据访问层对象,调用其中的添加方法
SMSInfoRepository sMSInfoRepository = new SMSInfoRepository();
return sMSInfoRepository.InsertSMSInfo(sMS)>0;
}
}
}
6:控制器写一个JsonResult的发送验证码方法需要接收手机号
public ActionResult SendCode(string tel)
{
OperateResult oper = new OperateResult();
SMSInfoService sms = new SMSInfoService();
oper.Success = sms.SendCode(tel);
return Json(oper);
}
7.控制器写一个JsonResult的校验验证码方法需要接收短信信息对象
public ActionResult ValidateCode(SMSInfo sms1)
{
OperateResult oper = new OperateResult();
SMSInfoService sms = new SMSInfoService();
oper.Success = sms.QuerySmsInfo(sms1)>0;
return Json(oper);
}
8:页面点击获取验证码按钮:先禁用按钮,然后ajax post提交到控制器对应的发送验证码方法,传入手机号,然后success处理返回的结果。
function sd() {
//禁用
$(this).attr("disable", "disabled");
$(this).css("color", "black");
//倒计时开始
var time = 60;
$(this).val(time + "S秒后发送");
var timer = setInterval(function () {
if (time > 0) {
time--;
$("#codebtn").val(time + "S秒后发送");
} else {
$("#codebtn").removeAttr("disable").css("color", "blue");
clearInterval(timer);
}
},1000)
}
$("#codebtn").click(function () {
var telphone = $("#UserPhone").val();
$.ajax({
type: "post",
url: "/Common/SendCode?tel=" + telphone,
success: function (smstel) {
if (smstel.Success) {
alert("发送成功");
sd();
} else {
alert("发送失败");
}
}
9:点击注册按钮,写一个校验验证码的方法,校验通过之后才能注册。