1. 程式人生 > >通用程式碼:傳送簡訊並顯示倒計時

通用程式碼:傳送簡訊並顯示倒計時

<input type="button" value="傳送簡訊驗證碼" id="btnReSend" />
<script type="text/javascript">
	var wait =120;
	var setSecID = "";
	function countDown() {
		wait = wait - 1;
		if (wait == 0) {
			clearInterval(setSecID);
			SetBtnState("#btnReSend", true, "重新獲取驗證碼"); 
		} else {
			SetBtnState("#btnReSend", false, wait + "秒後重新獲取");
		}
	}

	$(function () {
		if ($("#btnReSend").val() == "60秒後重新獲取") {
			setSecID = setInterval("countDown();", 1000);
		}
		$("#btnReSend").click(function () {
			var phone = $("#txtValidCode").val();
			if (!(/^1\d{10}$/.test(phone))) {
				alert("對不起,手機號碼格式不正確");
				return;
			}
			SetBtnState("#btnReSend", false, "處理中,請稍候");
			$.ajax({
				url: "doBackStage/AddAccount.ashx",
				dataType: "json",
				data: {
					action: "sendSMS",
					phone: phone
				},
				success: function (res) {
					if (res) {
						clearInterval(setSecID);
						wait = 120;
						setSecID = setInterval("countDown();", 1000);
						alert("已向您手機發送註冊驗證碼,請注意查收");
					} else {
						alert(res);
					}
				}
			});
		});

	});
	function SetBtnState(btn, isEnable, text) {
		if (isEnable) {
			$(btn).removeAttr("disabled").val(text);
		} else {
			$(btn).attr({ "disabled": "disabled" }).val(text);
		}
	}
</script>

後臺:

<%@ WebHandler Language="C#" Class="AddAccount" %>

using System;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Web;
using BLL.Common;
using DAL.Access;

public class AddAccount : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        var action = context.Request.QueryString["action"];
        switch (action)
        {
            case "sendSMS":
                var phone = context.Request.QueryString["phone"];

                #region 第一步:驗證號碼格式

                if (!new Regex(@"^1\d{10}$", RegexOptions.IgnoreCase | RegexOptions.Compiled).IsMatch(phone))
                {
                    context.Response.Clear();
                    context.Response.Write("對不起,手機號碼格式不正確");
                    context.Response.End();
                }

                #endregion

                #region 第二步:判斷是否已經發送過

                SqlParameter[] parameters =
                {
                    new SqlParameter("@phone", SqlDbType.VarChar, 15)
                };
                parameters[0].Value = phone;
                var regLog = SqlHelper.GetDataTable(@"
                                        SELECT top 1 datediff(s,getdate(),regtime)+120,id 
                                        FROM UnitReg 
                                        WHERE regAccount = @phone and regtime > dateadd(s,-120,getdate())", parameters,
                    "U");
                if (regLog.Rows.Count > 0)
                {
                    context.Response.Clear();
                    context.Response.Write("已向您的手機發送了一條驗證簡訊,請注意查收," + regLog.Rows[0][0] + "秒後可以重新獲取");
                    context.Response.End();
                }

                #endregion

                #region 第三步:傳送簡訊

                var random = new Random().Next(100000, 999999).ToString();
                SqlParameter[] parms =
                {
                    new SqlParameter("@phone", SqlDbType.VarChar, 15),
                    new SqlParameter("@verifycode", SqlDbType.VarChar, 10),
                    new SqlParameter("@regip", SqlDbType.VarChar, 15)
                };
                parms[0].Value = phone;
                parms[1].Value = random;
                parms[2].Value = Validator.GetClientIP().Trim();
                var sql = @"INSERT INTO 
                                UnitReg ([regType],[regAccount],[verifycode],[regip],[regtime],[flag]) 
                                VALUES ('手機', @phone, @verifycode, @regip,getdate(),'0');SELECT SCOPE_IDENTITY() AS NewID";
                SqlHelper.ExSql(sql, parms, "U");
                SendSMS.Send(phone, string.Format("驗證碼為{0},您正在註冊成都人才網單位使用者,需要進行手機驗證", random));

                #endregion

                context.Response.Clear();
                context.Response.Write("true");
                context.Response.End();
                break;
        }
    }

    public bool IsReusable
    {
        get { return false; }
    }
}