asp.net mvc 接入美聖簡訊 驗證碼發送

來源:互聯網
上載者:User

標籤:***   validate   i++   isp   type   width   ram   簽名   put   

第1步:登入美聖簡訊控制台

http://www.rcscloud.cn/hy/HY_ZH/login

帳號:*******

密碼:*******

http://www.rcscloud.cn/common/API

第2步:添加帳號簽名和簡訊模板

第3步:下載模板

 

第4步:在項目中建立項目GmkCollege.RCSCloud並將App_Code中的兩個檔案放到項目中,注意修改命名空間

 

第5步:前端

      <tr class="margin-top">

           <td class="padding-top text-center">手機號</td>

           <td><input type="text" class="inputs" id="Phone" name="Phone"> </td>

           <td><input type="button" value="擷取驗證碼" id="sms" onclick="sendemail()"></td>

      </tr>

      <tr>

           <td class="padding-top text-center">驗證碼</td>

           <td><input type="text" class="inputs" id="Code" name="Code"></td>

       </tr>

第6步:js處理

$(function () {

    $("#sms").click(function () {

        sendCode($("#sms"));

    });

    v = getCookieValue("secondsremained");//擷取cookie值

    if (v > 0) {

        settime($("#sms"));//開始倒計時

    }

})

 

//發送驗證碼

function sendCode(obj) {

    var phoneNumber = $("#Phone").val();

    var result = isPhoneNum(phoneNumber);

    if (result) {

        //將手機利用ajax提交到背景發簡訊介面

        $.post("/College/Code", { Phone: phoneNumber }, function (data) {

            if (data == "ok") {

                alert("驗證碼發送成功!");

            } else {

                alert("驗證碼發送失敗,請重新發送!");

            }

        });

        addCookie("secondsremained", 60, 60);//添加cookie記錄,有效時間60s

        settime(obj);   //開始倒計時

    }

}

//開始倒計時

var countdown;

function settime(obj) {

    countdown = getCookieValue("secondsremained");

    if (countdown == 0) {

        obj.removeAttr("disabled");

        obj.val("擷取驗證碼");

        return;

    } else {

        obj.attr("disabled", true);

        obj.val("重新發送(" + countdown + ")");

        countdown--;

        editCookie("secondsremained", countdown, countdown + 1);

    }

    setTimeout(function () { settime(obj) }, 1000) //每1000毫秒執行一次

}

//校正手機號是否合法

function isPhoneNum(phoneNumber) {

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;

    if (!myreg.test(phoneNumber)) {

        alert(‘請輸入有效手機號碼!‘);

        return false;

    } else {

        return true;

    }

}

//發送驗證碼時添加cookie

function addCookie(name, value, expiresHours) {

    var cookieString = name + "=" + escape(value);

    //判斷是否設定到期時間,0代表關閉瀏覽器時失效

    if (expiresHours > 0) {

        var date = new Date();

        date.setTime(date.getTime() + expiresHours * 1000);

        cookieString = cookieString + ";expires=" + date.toUTCString();

    }

    document.cookie = cookieString;

}

//修改cookie的值

function editCookie(name, value, expiresHours) {

    var cookieString = name + "=" + escape(value);

    if (expiresHours > 0) {

        var date = new Date();

        date.setTime(date.getTime() + expiresHours * 1000); //單位是毫秒

        cookieString = cookieString + ";expires=" + date.toGMTString();

    }

    document.cookie = cookieString;

}

//根據名字擷取cookie的值

function getCookieValue(name) {

    var strCookie = document.cookie;

    var arrCookie = strCookie.split("; ");

    for (var i = 0; i < arrCookie.length; i++) {

        var arr = arrCookie[i].split("=");

        if (arr[0] == name) {

            return unescape(arr[1]);

            break;

        } else {

            return "";

            break;

        }

    }

}

第7步:在後台控制器中處理

//美聖融雲驗證碼發送

        public ActionResult ValidateCode()

        {

            string Code = GetRandomString(6);

            //發送模板簡訊

            string result = RCSCloudRestAPI.sendTplSms(

                "f3b70fc99e1040b99657da1f9411df05",

                Request["Phone"],

                "@[email protected]="+Code,

                ""

            );         

            //將驗證碼設定緩衝

            var CodeInfo = (Object)Code;

            CacheOpt.SetCache("Code", CodeInfo, Convert.ToInt32(60));

            return Json(result);

        }

        #region 產生6位驗證碼

        public string GetRandomString(int iLength)

        {

            string buffer = "0123456789";    // 隨機字元中也可以為漢字(任何)

            StringBuilder sb = new StringBuilder();

            Random r = new Random();

            int range = buffer.Length;

            for (int i = 0; i < iLength; i++)

            {

                sb.Append(buffer.Substring(r.Next(range), 1));

            }

            return sb.ToString();

        }

        #endregion

第8步:緩衝處理

 

 

    public class CacheOpt

    {

        /// <summary>

        /// 設定緩衝

        /// </summary>

        /// <param name="CacheKey"></param>

        /// <param name="objObject"></param>

        /// <param name="Seconds">超過多少秒後到期</param>

        public static void SetCache(string CacheKey, object objObject, long Seconds)

        {

            System.Web.Caching.Cache objCache = HttpRuntime.Cache;

            objCache.Insert(CacheKey, objObject, null, System.DateTime.Now.AddSeconds(Seconds), TimeSpan.Zero);

        }

 

        /// <summary>

        /// 擷取資料緩衝

        /// </summary>

        /// <param name="CacheKey">鍵</param>

        public static object GetCache(string CacheKey)

        {

            System.Web.Caching.Cache objCache = HttpRuntime.Cache;

            return objCache[CacheKey];

        }

    }

asp.net mvc 接入美聖簡訊 驗證碼發送

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.