一 获取验证码部分
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); } } });
二 调用接口生成验证码到用户手机
#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("操作失败") }; } }
三 验证部分
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; } } });
四 调用接口验证
#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