1:创建一个项目用来调用第三方的类,右键Nuget添加第三方的引用类库 qcloudsms_csharp
点击解决方案然后添加新建解决方案文件夹在里面添加用来调用第三方的项目。
2:把第三方的公共类放入到我们的项目里。
using qcloudsms_csharp;
using qcloudsms_csharp.httpclient;
using qcloudsms_csharp.json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyStudent.Remote
{
public class TenXunYunSMS
{
///
///
///
//appId
public int appId;
//appKey
public string appKey = "";
//短信模板ID
private int tmplateId = 379257;
//签名内容
private string smsSign = "7hhhcn";
///
/// 验证码
///
public int Code { get; set; }
///
/// 发送验证码
///
///
///
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;
}
}
}
3:创建一张短信发送记录表。
Create table SMSInfo(
Id int identity primary key,
Code int,
TelPhone bigint,
CreateTime datetime,
ExpireTime datetime
)
编写实体类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MyStudent.Mode
{
public class SMSInfo
{
public int Id { get; set; }
public int Code { get; set; }
public Int64 TelPhone { get; set; }
public DateTime CreateTime { get; set; }
public DateTime ExpireTime { get; set; }
}
}
4:数据访问层写插入短信信息表方法。
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyStudent.Core;
using MyStudent.Mode;
namespace Student.DAL
{
public class SMSInfoRepository
{
//添加验证码
public int AddSmsInfo(SMSInfo sMSInfo)
{
DBHelper dbh = new DBHelper();
int result = -1;
string sql = @"insert into [dbo].[SMSInfo]([Code],[TelPhone],[CreateTime],[ExpireTime])" +
"values(@Code,@TelPhone,@CreateTime,@ExpireTime)";
SqlParameter[] parameters =
{
new SqlParameter()
{
DbType = System.Data.DbType.Int32,
ParameterName = "@Code",
Value = sMSInfo.Code
},
new SqlParameter()
{
DbType = System.Data.DbType.Int64,
ParameterName = "@TelPhone",
Value = sMSInfo.TelPhone
},
new SqlParameter()
{
DbType = System.Data.DbType.DateTime,
ParameterName = "@CreateTime",
Value = DateTime.Now
},
new SqlParameter()
{
DbType = System.Data.DbType.DateTime,
ParameterName = "@ExpireTime",
Value =DateTime.Now.AddMinutes(5)
},
};
result = dbh.ExcuteNoQuery(sql, parameters);
return result;
}
//查询验证码
public int QuerySmsInfo(SMSInfo sMSInfo)
{
DBHelper dbh = new DBHelper();
int result = -1;
try
{
string sql = @"select count(1) from dbo.SMSInfo where Code=@Code and TelPhone=@TelPhone and ExpireTime>@ExpireTime";
SqlParameter[] parameters =
{
new SqlParameter()
{
DbType = System.Data.DbType.Int32,
ParameterName = "@Code",
Value = sMSInfo.Code
},
new SqlParameter()
{
DbType = System.Data.DbType.Int32,
ParameterName = "@TelPhone",
Value = sMSInfo.TelPhone
},
new SqlParameter()
{
DbType = System.Data.DbType.DateTime,
ParameterName = "@CreateTime",
Value = DateTime.Now
},
};
result = dbh.ExcuteScalar(sql, parameters);
return result;
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return result;
}
}
}
4:业务层:先引用第三方项目,先调用第三方类,发送验证码,然后将验证码存储到短信信息对象,最后调用数据访问层的插入短信的方法。
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyStudent.Mode;
using MyStudent.Remote;
using Student.DAL;
namespace MyStudent.BLL
{
public class SMSInfoService
{
public bool SendCode(string telphone)
{
//new第三方类
TenXunYunSMS tenXunYunSMS = new TenXunYunSMS();
try
{
//设置开发者账号
tenXunYunSMS.appId = Convert.ToInt32(ConfigurationManager.AppSettings["appId"]);
tenXunYunSMS.appKey = ConfigurationManager.AppSettings["appKey"];
//发送验证码
tenXunYunSMS.SetSMS(telphone);
}
catch (Exception)
{
return false;
}
SMSInfoRepository infoRepository = new SMSInfoRepository();
SMSInfo sMSInfo = new SMSInfo();
sMSInfo.Code = tenXunYunSMS.Code;
sMSInfo.TelPhone = Convert.ToInt64(telphone);
return infoRepository.AddSmsInfo(sMSInfo)>0;
}
}
5:控制器写一个JsonResult的发送验证码方法需要接收手机号 。
public JsonResult SendCode(string tel)
{
OperateResult result = new OperateResult();
SMSInfoService sMSInfoService = new SMSInfoService();
result.Success = sMSInfoService.SendCode(tel);
return Json(result);
}
6:控制器写一个JsonResult的校验验证码方法需要接收短信信息对象。
public JsonResult ValidateCode(SMSInfo sMSInfo)
{
OperateResult result = new OperateResult();
SMSInfoService sMSInfoService = new SMSInfoService();
result.Success = sMSInfoService.QuerySmsInfo(sMSInfo) > 0;
return Json(result);
}
7:页面点击获取验证码按钮:先禁用按钮,然后ajax post提交到控制器对应的发送验证码方法,传入手机号,然后success处理返回的结果。
//获取验证码
$("#verification").click(function () {
$(this).attr("disable", "disabled");
$(this).css("color", "black");
var time = 60;
var tel = $("#tel").val();
$(this).val(time + "s后可再发送");
var timer = setInterval(function () {
if (time > 0) {
time--;
$("#verification").val(time + "s后可发送");
} else {
$("#verification").removeAttr("disable").css("color", "white");
clearInterval(timer);
}
}, 1000);
$.ajax({
type: "post",
url: "/Common/SendCode?tel=" + tel,
success: function (result) {
if (result.Success) {
alert("发送成功");
window.location.herf = "/Login/Login"
} else {
alert("发送失败");
}
},
})
});
8:点击注册按钮,写一个校验验证码的方法,校验通过之后才能注册。