博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js 短信验证码功能
阅读量:4941 次
发布时间:2019-06-11

本文共 8347 字,大约阅读时间需要 27 分钟。

一 获取验证码部分

function getCode() {            if (!(/^1[3|4|5|8][0-9]\d{
8}$/).test($("#txtUserPhone").val())) { tip($("#txtUserPhone"), "error", "请填写正确的手机号码"); return; } $.ajax({ async: false, type: "POST", url: "/LoanUserInfo/SendVerifyCode.do", cache: false, timeout: 60 * 60 * 1000, dataType: "json", data: { UserPhone: encodeURIComponent($("#txtUserPhone").val()) }, success: function (result) { if (result != null) { if (result == "Success") { V("receiveAgain").innerHTML = ""; V("receiveAgain").innerHTML = '60秒后再获取'; timer = setInterval(everySecond, 1000); } else { jQuery("#btnCode").text("获取验证码"); tip($("#txtCode"), "error", result); } } else { jQuery("#btnCode").text("获取验证码"); tip($("#txtCode"), "error", result); } } });
View Code

 

二 调用接口生成验证码到用户手机

#region 发送手机验证码接口        ///         /// 发送手机验证码接口        ///         /// 
public ActionResult SendVerifyCode() { var inModel = new UserInfoIn(); try { this.UpdateModel(inModel); #region 参数验证 if (string.IsNullOrWhiteSpace(inModel.UserPhone)) { return new SlJsonResult() { Content = SlJson.ToJson("参数错误,请刷新后再试") }; } #endregion string strV = FormsAuthentication.HashPasswordForStoringInConfigFile("jr-finance-web" + "financeadmin" + "ui90ker2s" + inModel.UserPhone, "md5"); string smsUrl = string.Format(Config.SendMobileLoginValid + "&v={0}&mobilephone={1}&ip={2}", strV, inModel.UserPhone, Common.Common.GetUserIp()); XElement root = XElement.Load(smsUrl).Element("common"); string result = root.Element("return_result") == null ? "" : root.Element("return_result").Value; if (result == "100") { return new SlJsonResult() { Content = SlJson.ToJson("Success") }; } else { return new SlJsonResult() { Content = SlJson.ToJson(root.Element("error_reason").Value == null ? "" : root.Element("error_reason").Value) }; } } catch (Exception) { return new SlJsonResult() { Content = SlJson.ToJson("操作失败") }; } }
View Code

三 验证部分

 

var timer;        function V(id) {            return document.getElementById(id);        }        //手机验证码倒计时        function everySecond() {            if (V('tick').innerHTML == 1) {                V("receiveAgain").innerHTML = "";                clearInterval(timer);                V("receiveAgain").innerHTML = '点击重新获取';            }            else {                V('tick').innerHTML = V('tick').innerHTML - 1;            }        } if (!(/^[0-9]{6}$/).test($("#txtCode").val())) {                tip($("#txtCode"), "error", "请填写");                return false;            } else {                tip($("#txtCode"), "correct", "");            }            //验证码验证            $.ajax({                async: false,                type: "POST",                url: "/LoanUserInfo/VerifyMobileLoginValid.do?Random=" + RndNum(100),                cache: false,                timeout: 60 * 60 * 1000,                dataType: "json",                data: {                    UserPhone: encodeURIComponent($("#txtUserPhone").val()),                    Code: encodeURIComponent($("#txtCode").val())                },                success: function (result) {                    if (result != null && result.Message == "@(SlStandardMessage.Success)") {                        jQuery("#btnCode").attr("disabled", true);                        $("#PassUserID").val(result.PassUserID);                        $("#PassUserName").val(result.PassUserName);                    }                    else {                        jQuery("#btnCode").attr("disabled", false);                        jQuery("#btnCode").text("获取验证码");                        tip($("#txtCode"), "error", result.Message);                        return false;                    }                }            });
View Code

 

四 调用接口验证

#region 新版验证手机验证码接口        ///         /// 新版验证手机验证码接口        ///         /// 
public ActionResult VerifyMobileLoginValid() { var inModel = new UserInfoIn(); try { this.UpdateModel(inModel); #region 参数验证 if (string.IsNullOrWhiteSpace(inModel.UserPhone) || string.IsNullOrWhiteSpace(inModel.Code)) { return new SlJsonResult() { Content = SlJson.ToJson(new { Message = "参数错误,请刷新后再试", PassUserID = "", PassUserName = "" }) }; } #endregion string strV = FormsAuthentication.HashPasswordForStoringInConfigFile("jr-finance-web" + "financeadmin" + "ui90ker2s" + inModel.UserPhone, "md5"); string smsUrl = string.Format( Config.VerifyMobileLoginValid + "&v={0}&mobilephone={1}&verificationcode={2}&ip={3}&port={4}&host=http://{5}", strV, inModel.UserPhone, inModel.Code, Common.Common.GetUserIp(), Common.Common.GetUserPort(), HttpContext.Request.Url.Host.ToString() ); XElement root = XElement.Load(smsUrl).Element("common"); string result = root.Element("return_result") == null ? "" : root.Element("return_result").Value; if (result == "100") { Common.Common.Login(root.Element("sfut_cookie").Value); long PassUserID = SlConvert.TryToInt64(root.Element("userid").Value == null ? "" : root.Element("userid").Value); string PassUserName = root.Element("username").Value == null ? "" : root.Element("username").Value; Common.Common.CreateCookie("PassUserID", HttpUtility.UrlEncode(PassUserID.ToString())); Common.Common.CreateCookie("PassUserName", HttpUtility.UrlEncode(PassUserName)); return new SlJsonResult() { Content = SlJson.ToJson(new { Message = SlStandardMessage.Success, PassUserID = root.Element("userid").Value == null ? "" : root.Element("userid").Value, PassUserName = root.Element("username").Value == null ? "" : root.Element("username").Value, }) }; } else { return new SlJsonResult() { Content = SlJson.ToJson(new { Message = root.Element("error_reason").Value, PassUserID = "", PassUserName = "" }) }; } } catch (Exception) { return new SlJsonResult() { Content = SlJson.ToJson(new { Message = "操作失败", PassUserID = "", PassUserName = "" }) }; } } #endregion
View Code

 

转载于:https://www.cnblogs.com/eric-gms/p/4054388.html

你可能感兴趣的文章
C#中泛型之Dictionary
查看>>
强连通分量
查看>>
使用Code First模式开发如何更新数据库(转载)
查看>>
sqoop导出工具
查看>>
Codeforces Round #376 (Div. 2)
查看>>
Codeforces 607D Power Tree 线段树 (看题解)
查看>>
写在人生的路上——2016年上半年总结
查看>>
员工选票系统-java
查看>>
C语言、C语言的起源以及类似C语言的编程语言的历史简直不要太漫长,我简单总结列表如下:...
查看>>
sp1.3-1.4 Neural Networks and Deep Learning
查看>>
JavaScript易错知识点整理
查看>>
Biological Clocks
查看>>
2018-10-11
查看>>
国内NLP的那些人那些会
查看>>
SQL 将一个表中的所有记录插入到一个临时表中
查看>>
nmea协议
查看>>
js 中对象的特性
查看>>
hdoj3714【三分】
查看>>
嵌入式开发入门(4)—驱动入门之时序图分析【20121211修改,未完】
查看>>
Python 使用字符串
查看>>