短信发送倒计时功能

  1.  html代码:

<input class="inpt-hm" type="text" id="mobile" name="mobile" placeholder="请输入手机号码">
<input class="yzma-put" type="text" placeholder="请输入验证码">
<input id="btnSendCode" type="button" id="btn" value="发送验证码" onclick="sendMessage()">

   2.  js代码:

var InterValObj; //timer变量,控制时间  
var count = 60; //间隔函数,1秒执行  
var curCount;//当前剩余秒数 
	
function sendMessage() {  
	 curCount = count;  
	        
	 var mobile = $("input.inpt-hm").val();
	 
	 if(mobile.length != 11 || mobile.substr(0,1)!=1){
		alert("手机格式不正确!");
		$("input.inpt-hm").select();
		return false;
	}else{
		//设置button效果,开始计时  
		$("#btnSendCode").attr("disabled", "true");  
		$("#btnSendCode").val(curCount + "秒后重新发送");
		//启动计时器,1秒执行一次	 
		InterValObj = window.setInterval(SetRemainTime, 1000); 		 
		$.ajax({
		      url:"/SendSMS.php",
		      data:{
		            mobile:mobile
		      },
		      dataType:'json',
		      type:'post',
		      success:function (data) {
		         console.table(data);
		         if(data['success'] == "false"){
		            alert("系统错误");
		         }		                
		      }
		 });	 	
		}             
	  
	}
//timer处理函数  
function SetRemainTime() {  
	 if (curCount == 0) {                  
	      window.clearInterval(InterValObj);//停止计时器  
	      $("#btnSendCode").removeAttr("disabled");//启用按钮  
	      $("#btnSendCode").val("重新发送验证码");  
	 }  
	 else {  
	      curCount--;  
	      $("#btnSendCode").val(curCount + "秒后重新发送");  
	 }  
}

   3.   SendSMS.php

<?php
/**
 * 启瑞云短信接口Demo(utf-8)
 */
class SendDemo
{
    const SENDURL = 'http://api.qirui.com:7891/mt';

    private $apiKey;
    private $apiSecret;

    /**
     * 构造方法
     * @param string $apiKey    接口账号
     * @param string $apiSecret 接口密码
     */
    public function __construct($apiKey, $apiSecret)
    {
        $this->apiKey    = $apiKey;
        $this->apiSecret = $apiSecret;
    }

    /**
     * 短信发送
     * @param string $phone   	手机号码
     * @param string $content 	短信内容
     * @param integer $isreport	是否需要状态报告
     * @return void
     */
    public function send($phone, $content, $isreport = 1)
    {
        $requestData = array(
            'un' => $this->apiKey,
            'pw' => $this->apiSecret,
            'sm' => $content,
            'da' => $phone,
            'rd' => $isreport,
            'dc' => 15,
            'rf' => 2,
            'tf' => 3,
            );

        $url = self::SENDURL . '?' . http_build_query($requestData);
        return $this->request($url);
    }

    /**
     * 请求发送
     * @return string 返回发送状态
     */
    private function request($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

}
//end
//实例化类,测试发送

//接口账号
$apiKey    = '1777270010';
//接口密码
$apiSecret = 'bcd9e627095d553da10d';
//接受短信的手机号码
$phone     = empty($_POST["mobile"])?"15872024172":$_POST["mobile"];
//短信内容(【签名】+短信内容),系统提供的测试签名和内容,如需要发送自己的短信内容请在启瑞云平台申请签名和模板
$yzm = rand(1000,9999);
$content   = '【首房在线】'.$phone.'您好,您的验证码是:'.$yzm.",请在3分钟内完成验证。";

session_start();
$_SESSION['yzm'] = $yzm;

$sms    = new SendDemo($apiKey, $apiSecret);
$result = $sms->send($phone, $content);

print_r($result );

发表评论?

2 条评论。

  1. I simply couldn’t depart your website before suggesting that I really loved the standard info a person supply for your visitors?
    Is going to be again incessantly to investigate cross-check new posts http://sapir.psych.wisc.edu/wiki/index.php/Free_As_Well_As_Healthy_Weight_Loss_Plans

发表评论


注意 - 你可以用以下 HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.