短信验证码,验证码确认接口(以及通过Ajax调用接口)
最近在做一个功能,需求是开发一个可以发送短信验证码的webserver服务。短信平台是客户提供的。验证码为6位纯数字。用C#开发。
先贴一下接口代码
/// <summary>
/// 短信验证码
/// </summary>
/// <returns></returns>
public static string SMSverificationcode(string phonenumber)
{
string time = DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
Dictionary<string, object> dictRtn = new Dictionary<string, object>();
List<Dictionary<string, string>> roleInfoList = new List<Dictionary<string, string>>();
//连接db
IGSPDatabase db = Genersoft.Platform.Core.DataAccess.Database.GetDatabase(vsZTBH);
string SendData = "";
#region 验证手机号是否存在,如果存在获取员工工号
vsSql = "select * from idmidentityinfo where phone='" + phonenumber + "'";
WriteLog("vsSql: " + vsSql, "短信验证码");
DataSet userinto = new DataSet();
userinto = db.ExecuteDataSet(vsSql.ToString());
if (userinto.Tables[0].Rows.Count == 0)
{
string message = "该手机号不存在,请检查!";
//StringBuilder SendDataFormat = new StringBuilder();
Dictionary<string, string> roleDic = new Dictionary<string, string>();
dictRtn.Add("status", "1");
dictRtn.Add("message", message);
dictRtn.Add("time", time);
SendData = new JavaScriptSerializer().Serialize(dictRtn);
WriteLog("SendData: " + SendData, "短信验证码");
return SendData;
}
DataRow vHeaderRow = userinto.Tables[0].Rows[0];
string usercode = Convert.ToString(vHeaderRow["CODE"]);
#endregion
#region 获取验证码
string VerificationCode = CreateRandomNumber(6);
#endregion
#region 发送短信
string ddurl = $"此处为你的url";
HttpUtil http = new HttpUtil();
DDMessageJson msj = new DDMessageJson();
//DDmsg ddmsg = new DDmsg(resulturl);
msj.Type = "MS";
msj.title = "验证码推送";
msj.content = "您的验证码是" + VerificationCode + ",转发给他人可能导致账号被盗,请勿泄露,谨防被骗。";
msj.touser = usercode;
string ReturnData = http.Post(ddurl,Newtonsoft.Json.JsonConvert.SerializeObject(msj));
#endregion
WriteLog("ReturnData: " + ReturnData, "短信验证码");
#region 返回数据处理
JObject objret = JObject.Parse(ReturnData);
string status = objret["status"].ToString();
string msg = objret["msg"].ToString();
WriteLog("解析数据:【" + status + "】【" + msg + "】", "短信验证码");
if (status == "0")
{
Dictionary<string, string> roleDic = new Dictionary<string, string>();
dictRtn.Add("status", status);
dictRtn.Add("message", msg);
dictRtn.Add("time", time);
SendData = new JavaScriptSerializer().Serialize(dictRtn);
WriteLog("SendData: " + SendData, "短信验证码");
vsSql = ("INSERT INTO SMSLOG(SMSLOG_CODE,SMSLOG_phonenumber,symk_VerificationCode,symk_TIME) VALUES('"
+ usercode + "','"
+ phonenumber + "','"
+ VerificationCode + "',"
+ "to_date('" + time + "', 'yyyy-mm-dd hh24:mi:ss'))");
WriteLog("插入SQL:" + vsSql, "短信验证码");
db.ExecSqlStatement(vsSql);
db.Commit();
}
else
{
Dictionary<string, string> roleDic = new Dictionary<string, string>();
dictRtn.Add("status", status);
dictRtn.Add("message", msg);
dictRtn.Add("time", time);
SendData = new JavaScriptSerializer().Serialize(dictRtn);
WriteLog("SendData: " + SendData, "短信验证码");
}
#endregion
return SendData;
}
catch (Exception ex)
{
db.Rollback();
WriteLog("异常: " + ex.ToString(), "短信验证码");
Dictionary<string, string> roleDic = new Dictionary<string, string>();
dictRtn.Add("status", "1");
dictRtn.Add("message", ex.ToString());
dictRtn.Add("time", time);
SendData = new JavaScriptSerializer().Serialize(dictRtn);
WriteLog("SendData: " + SendData, "短信验证码");
return SendData;
}
finally
{
db.Close();
}
}
生成验证码的方法
/// <summary>
/// 生成指定位数的随机数字码
/// </summary>
/// <param name="length"></param>
/// <returns></returns>
public static string CreateRandomNumber(int length)
{
Random random = new Random();
StringBuilder sbMsgCode = new StringBuilder();
for (int i = 0; i < length; i++)
{
sbMsgCode.Append(random.Next(0, 9));
}
return sbMsgCode.ToString();
}
下面是前端界面
点击获取验证码接口后手机或者钉钉上会收到短信或者消息
短信:
钉钉:
前端是通过ajax请求服务,代码如下:
$.ajax({
data: JSON.stringify(param),
url: 这里是发布的webserver接口地址,
type: "POST",
contentType: "application/json;charset=utf-8",
dataType: "json",
cache: false,
async: true,
success: function (json) {
debugger;
if (json && json.d) {
var data = eval('(' + json.d + ')');
status = data['status'];
message = data['message'];
url = data['url'];
$("#identification").next(".gwrap-tip").html(message);
if (status == 0) {
window.parent.parent.location.href = url.text.content;
}
}
},
error: function (xml, status) {
debugger;
gsp.idm.errorHandler.ajaxError(xml, status, function () { });
}
});