一 獲取驗證碼部分web
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 = '<a class="qhyzm"><span id="tick" style="cursor:pointer;">60</span>秒後再獲取</a>'; timer = setInterval(everySecond, 1000); } else { jQuery("#btnCode").text("獲取驗證碼"); tip($("#txtCode"), "error", result); } } else { jQuery("#btnCode").text("獲取驗證碼"); tip($("#txtCode"), "error", result); } } });
二 調用接口生成驗證碼到用戶手機ajax
#region 發送手機驗證碼接口 /// <summary> /// 發送手機驗證碼接口 /// </summary> /// <returns></returns> 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("操做失敗") }; } }
三 驗證部分json
var timer; function V(id) { return document.getElementById(id); } //手機驗證碼倒計時 function everySecond() { if (V('tick').innerHTML == 1) { V("receiveAgain").innerHTML = ""; clearInterval(timer); V("receiveAgain").innerHTML = '<a class="qhyzm" id="btnCode" onclick="getCode()" style="cursor:pointer;">點擊從新獲取</a>'; } 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; } } });
四 調用接口驗證cookie
#region 新版驗證手機驗證碼接口 /// <summary> /// 新版驗證手機驗證碼接口 /// </summary> /// <returns></returns> 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