1. 程式人生 > >前端驗證碼倒計時、後臺傳送驗證碼、創藍簡訊介面

前端驗證碼倒計時、後臺傳送驗證碼、創藍簡訊介面

前端程式碼:倒計時

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>


    <style>
        .yanzm_b_btn {
            width: 98px;
            height: 40px;
            float: left;
            line-height: 40px;
            text-align: center;
            border-radius: 5px;
            background: #f0f0f0;
            color: #aeaeae;
            font-size: 14px;
            margin-left: 10px;
            border: none;
            margin-bottom: 30px;
        }
    </style>
    <script src="http://cdn.static.runoob.com/libs/jquery/1.10.2/jquery.min.js">
    </script>
</head>
<body>
    <input class="register_b_shouji" type="text" placeholder="請輸入手機號" name="E_Mobile" id="E_Mobile">
    <input class="yanzm_b_btn" type="button" value="傳送驗證碼" onclick="GetCodemo(this)"/>
</body>
</html>
<script>
    var wait = 60;
    function GetCodemo(o){
        //傳送驗證碼
        if(wait == 60){
            //傳送驗證碼
            var mobile = $("#E_Mobile").val();
            if(mobile!=""){
                //請求後臺獲取資料、
                $.post('getMobileCode',{mobile:mobile,type:'reg'},function(data){
                    if(data.status==1){
                        //傳送成功
                    }else{
                        //傳送失敗
                    }
                },'json');
            }else{
                $("#E_Mobile").focus();
                return false;
            }
        }
        if(wait ==0){
            o.removeAttribute('disabled');//禁用
            o.value= '重新發送';
            wait = 60;
        }else{
            o.setAttribute('disabled',true);
            o.value= "已傳送("+wait+")";wait--;
            setTimeout(function(){GetCodemo(o)},1000);
        }
    }


</script>

後端傳送驗證碼程式碼:

/**
     * 傳送驗證碼
     */
    public function getMobileCode(){
        header("content-type:text/html; charset=utf-8");
        $Mobile = $_POST ["mobile"];      //使用者修改的手機號


        $type = trim($_POST["type"]); // 定義用來發送簡訊
        $type = empty($type)?"reg":$type; //簡訊模版程式碼
        if (!empty($type) && strlen($Mobile)==11){
            $Template = M("pagetemplate")->where(array("E_Type"=>$type))->cache(true,6000)->find();   //判斷型別,傳送驗證碼有多個地方使用到,比如找回密碼,註冊等
            if(empty($Template)) $this->jsonReturn(0, "簡訊型別異常!", '');
            if(!empty($Template['ID'])){
                $Code    = getCode(5);//驗證碼
                $sendstr = str_replace("0000", "", $Template["E_Template"]);  //傳送驗證碼文字、替換、例子:你正在註冊某某商城,驗證碼為0000,[某某商城]
                $result  = sendSMS($Mobile, $sendstr, 'true');  //呼叫創藍簡訊方法
                $result = $this->execResult($result);  //處理返回值
                if($result[1] == "0") {  //返回的是一個數組、狀態碼 0 是成功
                    $seReCode = $Mobile . "," . $Code;
                    $_SESSION['MobileCode'] = $seReCode;
                    $this->jsonReturn(1, "傳送成功!", '');
                }else{
                    $this->jsonReturn(0, "傳送失敗!", '');
                }
            }else{
                $this->jsonReturn(0, "異常、非法操作!", '');
            }
        }else{
            $this->jsonReturn(0, "異常、非法手機號!", '');
        }
    }


/**
* 查詢額度
*
* 查詢地址
*/
protected function queryBalance() {
$chuanglan_config = $this->GetInterfacecon ( 'message' );
// 查詢引數
$postArr = array (
'account' => $chuanglan_config ["ConS"] ['USERID'] ['val'],
'pswd' => $chuanglan_config ["ConS"] ['PWD'] ['val'] 
);
$result = $this->curlPost ( $chuanglan_config ["ConS"] ['URLQ'] ['val'], $postArr );
return $result;
}

/**
* 處理返回值
*/
protected function execResult($result) {
$result = preg_split ( "/[,\r\n]/", $result );
return $result;
}

/**
* 通過CURL傳送HTTP請求
*
* @param string $url
*         //請求URL
* @param array $postFields
*         //請求引數
* @return mixed
*/
protected function curlPost($url, $postFields) {
$postFields = http_build_query ( $postFields );
$ch = curl_init ();
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_HEADER, 0 );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $postFields );
$result = curl_exec ( $ch );
curl_close ( $ch );
// dump ( $result );
return $result;
}


/**
* 傳送簡訊
*
* @param string $mobile
*         手機號碼
* @param string $msg
*         簡訊內容
* @param string $needstatus
*         是否需要狀態報告
* @param string $product
*         產品id,可選
* @param string $extno
*         擴充套件碼,可選
*/
protected function sendSMS($mobile, $msg, $needstatus = 'false', $product = '', $extno = '') {

// 創藍介面引數
$postArr = array (
'account' => '',//賬號
'pswd' => '',//密碼
'msg' => $msg, //傳送內容
'mobile' => $mobile, //手機號
'needstatus' => $needstatus,
'product' => $product,
'extno' => $extno 
);
$result = $this->curlPost ( $chuanglan_config ["ConS"] ['URL'] ['val'], $postArr );
return $result;
}

官方文件:https://www.253.com/api-docs-5.html,狀態碼地址:https://www.253.com/api-docs-1.html