驗證碼相關



後臺可能用到的:

  ////隨機驗證碼(6位純數字)  

        //private string GenerateCheckCode()
        //{
        //    //創建整型型變量    
        //    int number;
        //    //創建字符型變量    
        //    char code;
        //    //創建字符串變量並初始化爲空    
        //    string checkCode = String.Empty;
        //    //創建Random對象    
        //    Random random = new Random();
        //    //使用For循環生成4個數字    
        //    for (int i = 0; i < 6; i++)
        //    {
        //        //生成一個隨機數    
        //        number = random.Next();
        //        //將數字轉換成爲字符型    
        //        code = (char)('0' + (char)(number % 10));

        //        checkCode += code.ToString();
        //    }
        //    //返回字符串    
        //    return checkCode;

        //} 





前臺:


  <script type="text/javascript">

       
       var wait = 10;//驗證碼再次發送的時間限定
       $("#btnGetNo").disabled = false;

       function time(o) {
          
         
           if (wait == 0) {
               o.removeAttribute("disabled");
               o.value = "再次獲取";
               wait = 10;
           } else {
               o.setAttribute("disabled", true);
               o.value = "重新發送(" + wait + ")";
               wait--;
               setTimeout(function () {
                   time(o)
               },
               1000)
           }
       }

       function GetNo()
       {
           if ($("#txtNum").val() == "")
           {
               var numbers = random();
               $("#txtNum").val(numbers);
               alert($("#txtNum").val());
               returnclock()
           }

           alert("驗證碼已發送至您輸入的手機號!有效期5分鐘。如未收到,60s後可再次獲取。");
           time(document.getElementById("btnGetNo"));
           RemainTime();
       }


       //獲取6位隨機驗證碼
       function random() {
           var num = "";
           for (i = 0; i < 6; i++) {
               num = num + Math.floor(Math.random() * 10);
           }
           return num;
       }
       //驗證碼有效期倒計時
       var iTime = 30;//設置驗證碼有效時間
       function RemainTime() {
           var iSecond;
           var sSecond = "", sTime = "";
           if (iTime >= 0) {
               iSecond = parseInt(iTime % 30);
               if (iSecond >= 0) {
                   sSecond = iTime + "秒";
               }
               sTime = "<span style='color:darkorange;font-size:13px;'>" + sSecond + "</span>";
               if (iTime == 0) {
                  // alert($("#txtNum").val());
                   $("#txtNum").val("");
                 //  alert($("#txtNum").val());
                   sTime = "<span style='color:red;font-size:12px;'>驗證碼已過期</span>";
               }
               else {
                   Account = setTimeout("RemainTime()", 1000);
               }
               iTime = iTime - 1;
           }
           $("#endtime").html(sTime);
       }
       
       //重置驗證碼過期時間
       function returnclock()
       {
           iTime = 30;
       }
       
       function loginSubmit() {
           //alert(123);
           var postdata = {
               tel: $("#txtTel").val(),
               num: $("#txtNum").val(),
               innum: $("#txtCode").val(),

               //示例數據
               //tel: "123456789",  //電話號碼
              // num: "123",        //30min內有效地驗證碼
              // innum: "123",     //用戶實際輸入的驗證碼
           }

           $.ajax({
               type: "post",
               //url: "/account/AjaxAuthorizeUser?phone=" + @ViewBag.txtNum + "&smsCodeRand=" + @ViewBag.txtTel +"&inputCode=" + $("#txtCode").val(),
               url: "/account/AjaxAuthorizeUser",
               data: JSON.stringify(postdata),
               dataType: "json",
               processData: false,
               contentType: "application/json; charset=utf-8",
               success: function (data) {
                   errorCode.getMsg(data, function () {
                       if (data.success) {
                           // document.location.href = "/admin/base";
                           alert("登陸成功!");
                       } else {
                           // $("#msgtip").html("驗證碼錯誤").css("color", "red");
                           $("#txtCode").val("");
                           $("#txtCode").select();
                           alert("驗證碼錯誤!");
                       }
                   }, function (e) {
                       $("#msgtip").html(e.msg + (data.msg == '' ? '(' + data.msg + ')' : '')).css("color", "red");
                   });
               }
           });
       }

</script>




<body>
     <form id="form1" runat="server">
            <div>
                <h2>登錄</h2>
                <h4>手機號:</h4>
                <input id="txtTel" type="text" value="" /><input type="button" id="btnGetNo" οnclick="GetNo()" value="獲取驗證碼" />
                <h4>驗證碼:</h4>
                <input id="txtCode" type="text" value="" /><input type="button" id="btnSubmit" οnclick="loginSubmit()" value="提交" />               
        </div>
          <span><i></i><b id="endtime">aaa</b></span>
    </form>
    <input id="txtNum" type="hidden" value="@ViewBag.txtNum" />
    <input id="tel" type="hidden" value="@ViewBag.txtTel"/>
</body>







  public ActionResult AjaxAuthorizeUser(string tel, string num, string innum)
        {
            var telephone = tel ?? "";
            var numbers = num ?? "";
            var innumbers = innum ?? "";
            ViewBag.txtNum = numbers;
            ViewBag.txtTel = telephone;

            var success = false;

            if (tel != "" && numbers != "" && innumbers != "")
            {
                if (innumbers == numbers)
                {
                    var datenow = DateTime.Now;
                    var datexpires = datenow.AddDays(1);

                    //查看是否有該用戶
                    var user = UserDal.F_User_GetMemberInforByTel(tel);
                    //某有則去創建
                    if (user == null)
                    {
                        int isCreate = UserDal.F_User_CreateClient_ByTel(tel);
                        user = UserDal.F_User_GetMemberInforByTel(tel);
                    }
                    //獲取相關用戶信息,保存至cookie中
                    var ticket = new FormsAuthenticationTicket(
                        1,
                        user.Id.ToString(CultureInfo.InvariantCulture) + ":" + user.ClientName,
                        datenow,
                        datexpires,
                        true,
                        user.RoleName.ToString(CultureInfo.InvariantCulture)
                        );
                    var cookie = new HttpCookie(AdminAuthorizeAttribute.FormsCookieName, FormsAuthentication.Encrypt(ticket))
                    {
                        Expires = datexpires
                    };
                    Response.Cookies.Add(cookie);


                    success = true;
                }
            }
            return Content(JsonConvert.SerializeObject(new
            {
                return_code = 0,
                success
            }));
        }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章