1. 程式人生 > >【轉】前端驗證碼倒計時、後臺發送驗證碼、創藍短信接口

【轉】前端驗證碼倒計時、後臺發送驗證碼、創藍短信接口

statistic pla use code arr 是否 fun utf nsf

前端代碼:倒計時

<!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

版權聲明:本文為博主原創文章,可以轉載 https://blog.csdn.net/hua950327/article/details/78064801

【轉】前端驗證碼倒計時、後臺發送驗證碼、創藍短信接口