1.项目中创建一个第三方类库,添加一个TenXunYunSMS.cs类
添加后在引用的NuGet包管理器中的浏览搜索qcloudsms_csharp下载
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)
{
throw;
}
catch (HTTPException ex)
{
throw;
}
catch (Exception ex)
{
throw;
}
Code = code;
}
}
2.在数据库中创建一个表来存储用户接收到的验证码和得到创建的时间
//smscode 验证码 ,userTel 手机号,smsTime 创建时间,smsEfficacy 有效时间
create table SmsInfo(
smsId int IDENTITY PRIMARY KEY,
smscode int ,
TelPhone bigint,
smsTime datetime,
smsEfficacy datetime
)
3.在UI层最下面的Web.config里配置第三方提供的appId值和appKey值
4.在项目中添加一个SmsInfo实体类
public class SmsInfo
{
public int smsId { get; set; }
public int smscode { get; set; }
public Int64 TelPhone { get; set; }
public DateTime smsTime { get; set; }
public DateTime smsEfficacy { get; set; }
}
5.在工具类里创建一个SqlHelper类,这个类提供添加验证码和查询验证码的操作
//添加验证码的信息
public int ExcuteNoQuery(string sql, SqlParameter[] parameters) {
try
{
//1:创建连接
conn = new SqlConnection(config);
//2:打开连接
conn.Open();
//3;创建SqlCommand对象
SqlCommand comm = new SqlCommand(sql, conn);
//4:预处理
if (parameters != null)
{
comm.Parameters.AddRange(parameters);
}
//5:执行sql语句 (ExcuteNonQuery)
i= comm.ExecuteNonQuery();
//6:处理返回结果、
return i;
}
catch (Exception ex)
{
return i;
}
finally {
conn.Close();
}
}
//查询验证码的信息
public int ExecuteReader(string sql, SqlParameter[] parameters)
{
try
{
//1:创建连接
conn = new SqlConnection(config);
//2:打开连接
conn.Open();
//3;创建SqlCommand对象
SqlCommand comm = new SqlCommand(sql, conn);
//4:预处理
if (parameters != null)
{
comm.Parameters.AddRange(parameters);
}
//5:执行sql语句 (ExcuteNonQuery)
i =Convert.ToInt32(comm.ExecuteScalar());
//6:处理返回结果、
return i;
}
catch (Exception ex)
{
return i;
}
finally
{
conn.Close();
}
}
6.数据访问层SmsCode类调用工具SqlHelper类的两个方法
//添加
public int insetsmsInfo(SmsInfo s)
{
string sql = @"INSERT INTO [MiShop].[dbo].[Sms]
([smscode]
,[TelPhone]
,[smsTime]
,[smsEfficacy])
VALUES
(@smscode
, @TelPhone
, @smsTime
, @smsEfficacy)";
SqlParameter[] parameters = {
new SqlParameter()
{
DbType = DbType.Int32,
ParameterName = "@smscode",
Value = s.smscode
},
new SqlParameter()
{
DbType = DbType.Int64,
ParameterName = "@TelPhone",
Value = s.TelPhone
},
new SqlParameter()
{
DbType = DbType.DateTime,
ParameterName = "@smsTime",
Value = s.smsTime
},
new SqlParameter()
{
DbType = DbType.DateTime,
ParameterName = "@smsEfficacy",
Value =s.smsEfficacy
}
};
SqlHelper db = new SqlHelper();
return db.ExcuteNoQuery(sql,parameters);
}
//查询
public int selectsmss(string tel)
{
string sql = @"select COUNT(1) from dbo.Sms where TelPhone=@TelPhoneand
smsEfficacy>@smsEfficacy";
SqlParameter[] parameters = {
new SqlParameter()
{
DbType = DbType.Int64,
ParameterName = "@TelPhone",
Value =Convert.ToInt64(tel)
},
new SqlParameter()
{
DbType = DbType.DateTime,
ParameterName = "@smsEfficacy",
Value = DateTime.Now
}
};
SqlHelper db = new SqlHelper();
return db.ExecuteReader(sql, parameters);
}
7.在业务层的SmsGain类中调用数据访问层SmsCode类的两个方法
//添加验证码
public bool Sms_Gain(string tel) {
//第三方接口TenXunYunSMS的对象
TenXunYunSMS t = new TenXunYunSMS();
try
{
//给TenXunYunSMS对象的appId从Web.config配置中拿出
t.appId = Convert.ToInt32(ConfigurationManager.AppSettings["appId"]);
//给TenXunYunSMS对象的appKey 从Web.config配置中拿出
t.appKey = ConfigurationManager.AppSettings["appKey"];
t.SetSMS(tel);
}
catch (Exception)
{
return false;
}
//数据库添加信息
//实体类SmsInfo对象值
SmsInfo s = new SmsInfo();
s.smscode = t.Code;
s.TelPhone =Convert.ToInt64(tel);
s.smsTime = DateTime.Now;
s.smsEfficacy = DateTime.Now.AddMinutes(5);
//数据访问层的SSmsInfo
SmsCode ss = new SmsCode ();
return ss.insetsmsInfo(s)>0;
}
//查询验证码
public int Sms_Gains(string tel)
{
SmsCode ss = new SmsCode();
return ss.selectsmss(tel);
}
8.在UI层的Login控制器中调用业务层的两个方法
//是否获取到验证码
public JsonResult Sms_Gain(string tel)
{
//调用Operate 对象(Operate类只有一个属性bool类型的Success)
Operate op = new Operate();
//调用业务层的SmsGain类
SmsGain s = new SmsGain();
//调用业务层的SmsGain类的Sms_Gain(string tel)方法
op.Success =s.Sms_Gain(tel);
//返回结果
return Json(op);
}
//是否已有验证码
public JsonResult Sms_Gains(string tel)
{
//调用Operate 对象(Operate类只有一个属性bool类型的Success)
Operate op = new Operate();
//调用业务层的SmsGain类
SmsGain s = new SmsGain();
//调用业务层的SmsGain类的Sms_Gain(string tel)方法
op.Success = s.Sms_Gains(tel)>0;
//返回结果
return Json(op);
}
9.在UI层的页面里把值传递到控制台
$("#btn").click(function () {
var tel = $("#tel").val();
//使用ajax判断用户是否已经有了有效验证码
$.ajax({
type: "post",
url: "/Login/Sms_Gains?tel=" + tel,
success: function (result) {
if (!result.Success) {
//使用.ajax异部刷新技术
$.ajax({
type: "post",
//调用Login控制器中的Sms_Gain()
url: "/Login/Sms_Gain?tel=" + tel,
success: function (result) {
if (result.Success) {
alert("信息发送成功");
ygc();
} else {
alert("信息发送失败");
}
},
})
} else {
alert("信息已发送,不需要再次发送");
}
},
})
})
10.归纳总结
①得到短信TenXunYunSMS类
②web配置appId值和appKey值
③数据库创建验证码的表
④创建实体类(实体类与数据库验证码的表一一对应)
⑤工具类SqlHelper
⑥数据访问层SmsCode类调用SqlHelper类
⑦业务层SmsGain类调用SmsCode类
⑧UI层的控制器Login调用SmsGain
⑨UI层的网页页面向UI层Login传值