注册时 手机验证码的js验证和后台验证,调用阿里大于短信验证平台,手机号注册

html 表单 

<form class="form-horizontal js-ajax-form" action="/user/login/do_mobile_forgot_password" method="post">
<tr>
<td width="235" align="right">手机号:</td>
<td><b>{$mobile}</b><input type="hidden" name="mobile" id="mobile" value="{$mobile}" /></td>
</tr>
<tr>
<td width="235" align="right">手机验证码:</td>
<td><input type="text" name="mobile_code" class="inp input-2 w210" /><a href="javascript:void(0);" id="user_getcode" class="btn btn-hui c666666 ml15 plc6 pc5 fz12 nb" target="_self" autocomplete="off">免费获取短信验证码</a></td>
</tr>
<tr>
<td align="right">校验码:</td>
<td><input type="text" class="inp input-2 w210" id="nomal_verify_code" name="verify" placeholder="请输入验证码" />
<img class="btn btn-hui c666666 ml15 plc6 pc5 fz12 nb" id="verifyCode" src="/api/checkcode/index?length=4&font_size=14&width=100&height=26&charset=1234567890&use_noise=1&use_curve=0" onclick="this.src='/api/checkcode/index?length=4&font_size=14&width=100&height=26&charset=1234567890&use_noise=1&use_curve=0&time='+Math.random();" style="cursor: pointer;" title="点击获取"/>
</td>
</tr>
JS事件
点击发送验证码时验证,发送成功按钮倒计时

//发送短信
$("#user_getcode").click(function(){
var mobile = $('#mobile').val();
var nomal_verify_code = $('#nomal_verify_code').val();
if (''== mobile) {
$('#regist_error').html('请输入手机号码');
}else {
$.ajax({
url: "/user/login/sms/mobile/" + mobile+'/verify/'+nomal_verify_code, success: function (result) {
// $('#user_getcode').html("ok");
// console.log(result);
if(result.info == '1'){
leftSeconds = 60;
msgSendToTip();
}else{
$('#regist_error').html(result.info);
if('图形验证码错误!' == result.info){
$('#verifyCode').attr('src','/api/checkcode/index?length=4&font_size=14&width=100&height=26&charset=1234567890&use_noise=1&use_curve=0&time='+Math.random());
}
}
}
});
}
});
function msgSendToTip() {
$("#user_getcode").unbind('click');
timeIntervalId = setInterval(function() {
if (leftSeconds <= 1) {
$('#user_getcode').removeClass('disable');
$('#user_getcode').removeClass('countdown');
$('#user_getcode').css({
"cursor" : "pointer"
});
$("#user_getcode").html("重新发送");
clearInterval(timeIntervalId);
$('#user_getcode').bind('click', getCodeClick);
leftSeconds = 10;
return;
}
leftSeconds--;
$('#user_getcode').addClass('disable');
$('#user_getcode').addClass('countdown');
$('#user_getcode').css({
"cursor" : "auto"
});
$("#user_getcode").html(leftSeconds + "秒后重发");
}, 1000);
}

控制器中写sms接口
function sms($mobile,$cid='Cb0j2c37XHy5'){
if(!checkMobile($mobile)){
$this->error('请填写正确的手机号!');
}
if(!sp_check_verify_code()){
$this->error("图形验证码错误!");
}
$ret = sendMessage($cid,$mobile);
if($ret === 0){
$this->success('1');
}elseif ($ret == 1) {
$this->error("短信发送失败!");
}
}

通用函数common/common/function.php中,写函数,生成验证码保存在session中,保存半小时不变,调用短信平台接口
/**
* 设置验证码
* @param 验证码变量名 $key
* @param 验证码长度 $length
* @param 全小写 $lower
* @return string 返回验证码
*/
function setVerificationCode($mobile){
$key = md5($mobile);
$value = $_SESSION[$key];
$arr = explode('_', $value);
// 判断是否存在
if (is_array($arr) && 2 == count($arr)){
$time = (int)$arr[1];
if ($time > 0 && (time() - $time) < 1800){ // 如果未超过半个小时,则重新发送
return $arr[0];
}
}
$code = rand(100000,999999);
$_SESSION[$key] = $code . '_' . time();
return $code;
}

/**
*发送手机验证码
* @param $cid 类型
* @param $mobile 手机号码
*/
function sendMessage($cid,$mobile){
//验证码
$p1 = setVerificationCode($mobile);
$p2 = 30;

$res = sendDayu($mobile,$cid,$p1,$p2);
if ($res) {
return 0;
}else{
return 1;
}

}
//阿里大于验证码
function sendDayu($mobile,$cid,$p1,$p2){
Vendor('Dayu.TopSdk');
$c = new TopClient;
$c->appkey = '11223344';
$c->secretKey = '121112121221221';
$c->format = 'json';
$req = new AlibabaAliqinFcSmsNumSendRequest;
$req->setSmsType("normal");
$req->setSmsFreeSignName("阿萨德");
$req->setSmsParam("{\"code\":\"$p1\"}");
$req->setRecNum($mobile);
$req->setSmsTemplateCode("SMS_53700052");
$resp = $c->execute($req);
if(isset($resp->result->success)){
return $resp->result->success;
}else{
return false;
}
}


posted @ 2017-05-24 17:57  jackduan1  阅读(3332)  评论(0编辑  收藏  举报