Asp.Net限时发送手机验证码

html代码

<p>
                        <strong>手机验证码:</strong>
                        <asp:TextBox ID="code" runat="server" CssClass="box" style="width:90px" maxlength="4"></asp:TextBox>
                        <input type="button" id="btncode" class="diarnu_net_btn" value="发送验证码" />
</p>

提交的时候只需验证Session["phoneCode"]code.text的值就行

js代码

var secs=100;
$("#btncode").click(function(){
    var _phone='18000055568';
    this.value=" 正在发送中 ";
    this.disabled=true;
    $.post("ajax.aspx",{type:"sendCode",phone:_phone},function(data){
        if(data=="1")
        {
            //成功
            for(var i=0;i<=secs;i++) 
            {  
                window.setTimeout("update(" + i + ")", i*1000);
            }
        }else{
            alert("验证码发送失败,请检查手机是否输入有误!");
            document.getElementById("btncode").value=" 发送验证码 ";
            document.getElementById("btncode").disabled=false;
        }
    })
})
function update(num) {  
   if(num == secs) {  
       document.getElementById("btncode").disabled=false;
       document.getElementById("btncode").click();
   }
   else {  
       printnr = secs-num;
       document.getElementById("btncode").value=" "+printnr+"秒后重发 ";
   }  
} 

ajax.aspx代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
public partial class ajax_getdata : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            switch (Request.Form["type"])
            {
                case "sendCode": sendCode();
                    break;
                default: Response.Write("");
                    break;
            }
        }
    }
    private void sendCode()
    {string to = Request.Form["phone"];
        string ret = null;

        CCPRestSDK.CCPRestSDK api = new CCPRestSDK.CCPRestSDK();
        //ip格式如下,不带https://
        bool isInit = api.init("app.cloopen.com", "8883");//sandboxapp.cloopen.com
        api.setAccount("8a216da85624qwwedriov157nfjdjkufd", "7bb62789d7efndfd54f6");//主账号,主账号令牌
        api.setAppId("8a216dueiu4389d57efjdsfu");//AppID

        try
        {
            if (isInit)
            {
                string strcode = new Random().Next(1000, 9999).ToString();
                if (MyCLib.StrClass.GetSession("phoneCode") != "")
                {
                    strcode = MyCLib.StrClass.GetSession("phoneCode");
                }
                Session["phoneCode"] = strcode;
                string[] data = { strcode, "20" };//验证码,分钟
                Dictionary<string, object> retData = api.SendTemplateSMS(to, "1", data);//短信接收号码, 短信模板id, 内容数据
                string statusCode = retData["statusCode"].ToString();//statusCode为"000000"表示请求发送成功。statusCode不是"000000",表示请求发送失败
                string statusMsg = retData["statusMsg"].ToString();

                if (statusCode == "000000")
                {
                    ret = "1";
                }
                else {
                    ret = statusMsg;
                }
            }
            else
            {
                ret = "初始化失败";
            }
        }
        catch (Exception exc)
        {
            ret = exc.Message;
        }
        finally
        {
            Response.Write(ret);
        }
    }
}

发送手机验证码方法请看我上一篇博文http://www.cnblogs.com/webapi/p/5711764.html

posted @ 2016-07-28 09:16  WebApi  阅读(2860)  评论(0编辑  收藏  举报
CopyRight © 博客园 WebAPI